![](/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.