我正在尝试从Explorer启动我的脚本。我已经找到了解决方案,如果脚本没有任何参数,它就可以工作。
$file = [System.IO.Directory]::GetCurrentDirectory() + "\Trees.ps1"
Start-Process powershell -verb runas -ArgumentList "-ExecutionPolicy UnRestricted -File `"$($file)`""但是,如果我添加额外的add参数,就像第一个一样,它就会停止工作。即。这段代码抛出“字符串缺少终止符:‘”。错误。
$file = [System.IO.Directory]::GetCurrentDirectory() + "\Trees.ps1"
$Context = [System.IO.Directory]::GetCurrentDirectory()
Start-Process powershell -verb runas -ArgumentList "-ExecutionPolicy UnRestricted -Context '"$($Context)'" -File `"$($file)`""我正在使用这种预期变量的方法:
[Parameter(Mandatory=$True)]
[string]$Context,如何在ArgumentList中传递多个带空格的变量?我怀疑我应该以其他方式传递文件内容的参数,而不是仅仅传递文件名,但是找不到解决方案。
发布于 2021-01-29 22:31:59
除了西奥提到的倒钩问题,-context应该效仿-file。
-File
Runs the specified script in the local scope ("dot-sourced"), so that the
functions and variables that the script creates are available in the
current session. Enter the script file path and any parameters.
File must be the last parameter in the command, because all characters
typed after the File parameter name are interpreted
as the script file path followed by the script parameters.所以你的命令行应该是
Start-Process powershell -verb runas -ArgumentList "-ExecutionPolicy UnRestricted -File `"$($file)`" -Context $($Context)"发布于 2021-01-29 22:47:22
由于您使用的是外部流程调用,因此需要在双引号内使用,而不是单引号。您可以简单地通过添加另一个双引号("")来转义双引号。
Start-Process powershell -verb runas -ArgumentList "-ExecutionPolicy UnRestricted -File ""$file"" -Context ""$Context"""https://stackoverflow.com/questions/65955995
复制相似问题