簡體   English   中英

Powershell參數列表和雙+空格

[英]Powershell argumentlist and double+ spaces

有人能夠解釋這個嗎? 我正在運行以下Powershell命令,並且2個“test”之間的雙空格被轉換為單個空格。 使用PS 3.0。

Start-Process Powershell -ArgumentList "-Command Write-Host test  test;pause"
Start-Process Powershell -ArgumentList "-Command Write-Host 'test  test';pause"
Start-Process Powershell -ArgumentList "Write-Host 'test  test';pause"

下面的腳本調用(只包含$ String參數和Write-Host $ String;暫停)工作正常,但在同一會話中執行:

& C:\Test\Test.ps1 -String 'test  test'

但是下面用單個替換雙空格:

Start-Process Powershell -ArgumentList "& C:\Test\Test.ps1 -String 'test  test'"

我需要能夠在當前會話之外運行另一個PS腳本,並將參數傳遞給可能包含這樣的雙空格的腳本。 謝謝!

據我所知,這種情況正在發生,因為在發送到PowerShell之前,雙引號沒有放在參數周圍。 這將導致shell在PowerShell看到它們之前折疊空間(因為它解析參數)。

所以我嘗試了這個測試:

Start-Process powershell.exe -ArgumentList "-NoExit -Command Write-Verbose '1 2  3   .' -Verbose"

它像你的一樣失敗了,沒有空格。

所以在新推出的PowerShell窗口中,我運行了這個:

gwmi Win32_Process -Filter "ProcessId = '$([System.Diagnostics.Process]::GetCurrentProcess().Id)'" | select -ExpandProperty CommandLine

結果:

"C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe" -NoExit -Command Write-Verbose '1 2  3   .' -Verbose 

看看命令周圍沒有雙引號? shell似乎並不關心單引號,因此命令中的每個空格都會讓shell認為它是一個新參數(它基本上是在空格上分裂,所以多個連續的空格都會折疊,除非它看到一個雙引號字符串,被視為單個參數)。

您可以通過自己嵌入引號來修復它。

Start-Process Powershell -ArgumentList "`"Write-Host 'test  test';pause`""

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM