![](/img/trans.png)
[英]How can I run a PowerShell script with white spaces in the path from the command line?
[英]Powershell - Run command from a path with spaces
我正在嘗試從存儲在變量中的路徑執行過程(變量的值是Get-Location命令的結果)。 僅當目錄沒有空格時,此方法才有效。 我發現了許多方法來對空間進行轉義,但是這些方法僅在您手動分配路徑時才有效。 還有其他辦法嗎?
這是代碼:
$userCred = Get-Credential "Domain\$admAccount"
$currentDir = Get-Location
Start-Process "pwd_user.exe" -WorkingDirectory $currentDir -Credential $userCred
我試過了:
"$currentDir"
$currentDir -replace ' ', '` '
&$currentDir
"&$currentDir"
這些都不起作用。
WorkingDirectory的默認值為當前目錄。 我不明白為什么首先需要這個變量。 至於變量,我想您可以嘗試擴展Path屬性。
Start-Process "pwd_user.exe" -WorkingDirectory $currentDir.Path -Credential $userCred
對於不成功的替換,您將需要擴展屬性。
$currentDir.Path -replace ' ', '` '
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.