使PowerShell忽略分号

wal*_*ybh 10 powershell

我想在PowerShell上执行cmd,此命令使用分号.然后PowerShell将其解释为多个命令.如何使PowerShell忽略分号并执行我的命令如何使用唯一命令?

例:

Invoke-Expression "msbuild /t:Build;PipelinePreDeployCopyAllFilesToOneFolder /p:Configuration=Debug;_PackageTempDir=$TargetFolder $WebProject"
Run Code Online (Sandbox Code Playgroud)

另一个例子:

Invoke-Expression "test`;test2"
Run Code Online (Sandbox Code Playgroud)

第二个例子回应:

The term 'test' is not recognized as the name of a cmdlet, function, script file, or operable program. Check
the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:6
+ teste <<<< ;teste2
    + CategoryInfo          : ObjectNotFound: (teste:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

The term 'test2' is not recognized as the name of a cmdlet, function, script file, or operable program. Chec
k the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:13
+ teste;teste2 <<<<
    + CategoryInfo          : ObjectNotFound: (teste2:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
Run Code Online (Sandbox Code Playgroud)

Kei*_*ill 24

只需在命令行中转义分号:

msbuild /t:Build`;PipelinePreDeployCopyAllFilesToOneFolder /p:Configuration=Debug`;_PackageTempDir=$TargetFolder $WebProject
Run Code Online (Sandbox Code Playgroud)

我一直使用tf.exe实用程序执行此操作:

tf.exe status . /r /workspace:WORK`;johndoe
Run Code Online (Sandbox Code Playgroud)

仅供参考,此问题已在Connect上投了大量资金.PowerShell v3使用new --%运算符解决了这个问题:

$env:TargetFolder = $TargetFolder
msbuild $WebProject --% /t:Build;PipelinePreDeployCopyAllFilesToOneFolder /p:Configuration=Debug;_PackageTempDir=%TargetFolder%
Run Code Online (Sandbox Code Playgroud)


Fox*_*loy 5

忽略分号的最简单方法?只需使用单引号与双引号即可!

在 PowerShell 中,您使用的引用类型很重要。双引号将让 PowerShell 进行字符串扩展(因此,如果您有变量 $something = someprogram.exe,并运行“$something”,PowerShell 将替换为“someprogram.exe”)。

如果您不需要字符串替换/变量扩展,则只需使用单引号。PowerShell 将完全按照列出的方式执行单引号字符串。

如果您想使用字符串扩展,另一个选择是使用此处字符串。这里的字符串就像常规字符串一样,但是它在自己的单独行上以 @ 符号开头和结尾,如下所示:

$herestring = @"
Do some stuff here, even use a semicolon ;
"@
Run Code Online (Sandbox Code Playgroud)

这是一个两全其美的场景,因为您可以使用您喜欢的字符并让它们工作,但仍然可以获得变量扩展,这是单引号所无法获得的。


And*_*ndi 0

作为替代方法Start-Process,您可以直接调用该命令,就像使用 cmd.exe 使用调用运算符调用它一样&

& msbuild /t:Build;PipelinePreDeployCopyAllFilesToOneFolder /p:Configuration=Debug;_PackageTempDir=$TargetFolder $WebProject
Run Code Online (Sandbox Code Playgroud)

  • @wallybh 很酷,您还应该能够像这样“/t:Build;PipelinePreDeployCopyAllFilesToOneFolder”一样引用参数,而不必转义分号。 (4认同)