我正在編寫一個腳本,該腳本使用給定的命令行參數遠程啓動程序。出於某種原因,我的一個字符串中的逗號(格式爲--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 - 標記小,中,大」,您應該會看到相同的行爲。 –