我想在批量但有条件的情况下使用 Start-Process
使用PowerShell运行程序 .
我测试了它,然后使用 Start-Process
命令运行 cmd /c
.
命令:
powershell -Command "Start-Process `cmd /c` -Verb runas"
它没用 .
所以我在PowerShell上测试了它 . 尝试相同的命令,它返回此错误:
Start-Process : A positional parameter cannot be found that accepts argument
'runas'.
At line:1 char:14
+ Start-Process <<<< 'cmd /c' -Verb runas
+ CategoryInfo : InvalidArgument: (:) [Start-Process], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.StartProcessCommand
如何使用 powershell
命令在主PowerShell上批量处理此工作?
1 回答
反引号是PowerShell的转义字符 . 在一份声明中
反引号逃脱了
cmd
的c
和/c
之后的空格 . 前者没有问题,因为它只是使c
成为文字c
(无论如何) . 但是,转义/c
和-Verb
之间的空间会有效地将整个序列/c -Verb
转换为单个字符串 . 基本上它's the same as if you'运行此语句:由于Start-Process只接受2个位置参数(
-FilePath
和-ArgumentList
),因此必须通过命名参数传递所有其他参数 . 因此,没有位置参数接受参数runas
的错误 .像这样运行命令:
要么
或者像这样(来自CMD):
它会像你期望的那样工作 .
Edit
由于最初显然不够清楚:
-ArgumentList
参数的参数是以逗号分隔的列表(数组) .