首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Powershell - Start-Process ArgumentList仅接受带空格的单个变量

Powershell - Start-Process ArgumentList仅接受带空格的单个变量
EN

Stack Overflow用户
提问于 2021-01-29 22:06:50
回答 2查看 339关注 0票数 0

我正在尝试从Explorer启动我的脚本。我已经找到了解决方案,如果脚本没有任何参数,它就可以工作。

代码语言:javascript
复制
$file = [System.IO.Directory]::GetCurrentDirectory() + "\Trees.ps1"
Start-Process powershell -verb runas -ArgumentList "-ExecutionPolicy UnRestricted -File `"$($file)`""

但是,如果我添加额外的add参数,就像第一个一样,它就会停止工作。即。这段代码抛出“字符串缺少终止符:‘”。错误。

代码语言:javascript
复制
$file = [System.IO.Directory]::GetCurrentDirectory() + "\Trees.ps1"
$Context = [System.IO.Directory]::GetCurrentDirectory()
Start-Process powershell -verb runas -ArgumentList "-ExecutionPolicy UnRestricted -Context '"$($Context)'" -File `"$($file)`""

我正在使用这种预期变量的方法:

代码语言:javascript
复制
[Parameter(Mandatory=$True)]
[string]$Context,

如何在ArgumentList中传递多个带空格的变量?我怀疑我应该以其他方式传递文件内容的参数,而不是仅仅传递文件名,但是找不到解决方案。

EN

回答 2

Stack Overflow用户

发布于 2021-01-29 22:31:59

除了西奥提到的倒钩问题,-context应该效仿-file

代码语言:javascript
复制
-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.

所以你的命令行应该是

代码语言:javascript
复制
Start-Process powershell -verb runas -ArgumentList "-ExecutionPolicy UnRestricted -File `"$($file)`" -Context $($Context)"
票数 1
EN

Stack Overflow用户

发布于 2021-01-29 22:47:22

由于您使用的是外部流程调用,因此需要在双引号内使用,而不是单引号。您可以简单地通过添加另一个双引号("")来转义双引号。

代码语言:javascript
复制
Start-Process powershell -verb runas -ArgumentList "-ExecutionPolicy UnRestricted -File ""$file"" -Context ""$Context"""
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65955995

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档