【发布时间】:2020-02-27 22:54:21
【问题描述】:
由于我的项目及其部署方式的限制,我需要在批处理文件中运行 powershell 命令并从 powershell 脚本中回显一个变量。此脚本通过 RS-232 从电视中检索型号,并以 HEX 格式返回型号。
我编写了脚本,并小心翼翼地将其压缩为一行运行。我什至可以在命令行中使用'powershell -command "[myCondensedScript]"',效果很好
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%
输出很简单
echo +
预期的输出是
563332330
我不确定变量缺少什么,因为我对变量和批处理文件不是很熟练。我已经广泛搜索并发现此页面有点帮助https://ss64.com/nt/for.html 我只是不明白我如何将 %%a 分配给我想要的确切变量。任何帮助将不胜感激!
【问题讨论】:
标签: windows powershell batch-file variables command-line