Due to restrictions around my project and how this is being deployed, I need to run a powershell command in a batch file and echo a variable from the powershell script. This scripts retrieves a model number from a TV over RS-232 and returns the model in HEX.
I wrote the script and meticulously condensed it to one line that runs. I can even 'powershell -command "[myCondensedScript]"' in the command line and it works great
for /F %%a in ('powershell.exe -command "$port = New-Object System.IO.Ports.SerialPort COM1, 9600, none, 8, one;[byte[]] $reqSerial = 1,48,65,48,65,48,54,2,67,50,49,55,3,112,13;[byte[]] $reqModel = 0x01,0x30,0x41,0x30,0x41,0x30,0x36,0x02,0x43,0x32,0x31,0x37,0x03,0x70,0x0D;[byte[]] $response = '';[byte[]] $readData = '';$port.DTREnable = $True;$port.Open();$port.Write($reqModel, 0, $reqModel.Count);$x=0;do {$x++;$readData = $port.ReadChar();If($x -eq 13 -or $x -eq 14 -or $x -eq 15 -or $x -eq 16 -or $x -eq 17 -or $x -eq 18 -or $x -eq 19 -or $x -eq 20 -or $x -eq 21) {$response += $readData;};}while($response.Length -ne 9);$port.Close();$hexResponse = $response.forEach([char]);$hexResponseString = [string]$hexResponse;$hexResponseString = $hexResponseString.replace(' ','');$finalHex = $hexResponseString+0;Write-Host $finalHex;"^; (Get-Variable -ValueOnly -Name finalHex^).value') do set HexCode=%%a
echo %HexCode%
The output is simply
echo +
The expected output would be
563332330
I'm not sure what I'm missing with the variable, as I'm not terribly skilled with variables and batch files. I have googled extensively and found this page to be somewhat helpful https://ss64.com/nt/for.html I just don't understand how I assign %%a to the exact variable I want. Any help would be greatly appreciated!