我正在编写一个脚本,该脚本使用给定的命令行参数远程启动程序。出于某种原因,我的一个字符串中的逗号(格式为--Tags Small,Medium,Large)会在Invoke-Command和应用程序读取参数之间的某个时间内对空格进行更改。Invoke-Command看起来将我的逗号转换为空格
PowerShell的:
param(
[string]$N = "remoteHost",
[string]$U = "user",
[string]$P = "pass",
[string]$App = "App.exe",
[string]$Arg = "--Tags Small,Medium,Large"
)
Write-Host "Connecting to" $N "..."
$sec = ConvertTo-secureString $P -asPlainText -Force
$session=New-PSSession -ComputerName $N -Credential (New-Object System.Management.Automation.PSCredential ($U,$sec))
$cmd = $App + " " + $Arg
Write-Host $cmd
$sb = ([scriptblock]::Create($cmd))
Write-Host $cmd
Invoke-Command -Session $session -ScriptBlock $sb
Write-Host "Disconnecting..."
Remove-PSSession -Session $session
两个$ CMD和$ SB写主机的显示我的期望:
"App.exe --Tags Small,Medium,Large"
但在 “APP.EXE” 应用程序,它接收ARGS:
"--Tags Small Medium Large"
如果我通过命令行使用完全相同的字符串运行App.exe,它会看到逗号,因为我认为c颠覆正在发生在PowerShell中。
在应用程序和最后一个写主机之间唯一的事情就是Invoke-Command,所以我在想它以某种方式将逗号转换为空格。我的问题是:
- 为什么要转换逗号?
- 解决这个问题的最佳方法是什么?
所以,我看到的是不是实际上有逗号,那只是形式如何PowerShell的scriptblocks?令人沮丧的巧合。我测试了这个修复程序,它运行了 –
逗号在那里。但是当你调用scriptblock并且命令被实际解析时它们会被移除。如果您只需在PowerShell控制台中输入“App.exe - 标记小,中,大”,您应该会看到相同的行为。 –