从CMD使用

时间:2018-12-26 19:58:27

标签: powershell cmd

正常运行的基准命令:

以下命令正在CMD提示符下执行并成功运行。

"C:\Program Files\PowerShell\6.0.4\pwsh.exe" -NoLogo -NoProfile -NonInteractive -Command "&{D:\FooBar\myscript.ps1 -DependentAssembliesDirectoryPath 'D:\FooBar' -OutputPath 'D:\Baz Qux\output' -DocumentVersion 'whatever' -VisualStudioXmlDocumentationPaths 'D:\Baz Qux\input\my.xml' -AssemblyPaths  'D:\Baz Qux\input\my.exe','D:\Baz Qux\input\my1.dll','D:\Baz Qux\input\my2.dll','D:\Baz Qux\input\my3.dll' -MajorOpenApiSpecificationVersion 3 -MinorOpenApiSpecificationVersion 0 -Format YAML -DocumentDescriptionFilePath 'D:\Baz Qux\input\my.md'}; EXIT $LASTEXITCODE"

但是,当在myscript.ps1的路径中引入空格时,该命令将不再起作用。这是预期的,因为我需要正确地引用路径。我无法找出正确的报价方式。

无效的引用该命令的尝试:

我认为这可以基于引用命令中其他路径的技术来实现,但这不起作用。

"C:\Program Files\PowerShell\6.0.4\pwsh.exe" -NoLogo -NoProfile -NonInteractive -Command "&{'D:\Foo Bar\myscript.ps1' -DependentAssembliesDirectoryPath 'D:\Foo Bar' -OutputPath 'D:\Baz Qux\output' -DocumentVersion 'whatever' -VisualStudioXmlDocumentationPaths 'D:\Baz Qux\input\my.xml' -AssemblyPaths  'D:\Baz Qux\input\my.exe','D:\Baz Qux\input\my1.dll','D:\Baz Qux\input\my2.dll','D:\Baz Qux\input\my3.dll' -MajorOpenApiSpecificationVersion 3 -MinorOpenApiSpecificationVersion 0 -Format YAML -DocumentDescriptionFilePath 'D:\Baz Qux\input\my.md'}; EXIT $LASTEXITCODE"

此命令会导致很多错误,例如

At line:1 char:119
+ ... ionsDocumentGeneration.ps1' -DependentAssembliesDirectoryPath 'D:\Ope ...
+                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unexpected token '-DependentAssembliesDirectoryPath' in expression or statement.

At line:1 char:153
+ ... rectoryPath 'D:\Foo Bar' -OutputPath ...
+                 ~~~~~~~~~~~~
Unexpected token ''D:\Foo Bar'' in expression or statement.

同样,由于很难看出差异,因此有效的基准命令和第二个命令的差异是将&{D:\FooBar\myscript.ps1更改为&{'D:\Foo Bar\myscript.ps1'以在路径中引入空格并尝试报价。

请注意

我无法在PowerShell中调用该命令,因为该命令不受我的控制。必须在cmd.exe提示符下调用它。

1 个答案:

答案 0 :(得分:2)

问题是您需要在PowerShell中使用&才能调用用引用和/或通过变量

因此,替换:

"&{'D:\Foo Bar\myscript.ps1' ... }; exit $LASTEXITCODE"

具有:

"& { & 'D:\Foo Bar\myscript.ps1' ... }; exit $LASTEXITCODE"

也就是说,没有理由将*.ps1脚本的调用包装在脚本块调用(& { ... })中,因此您可以将命令简化为:

"& 'D:\Foo Bar\myscript.ps1' ...; exit $LASTEXITCODE"