【问题标题】:Make PowerShell ignore semicolon使 PowerShell 忽略分号
【发布时间】:2012-04-25 05:43:35
【问题描述】:

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

例子:

Invoke-Expression "msbuild /t:Build;PipelinePreDeployCopyAllFilesToOneFolder /p:Configuration=Debug;_PackageTempDir=$TargetFolder $WebProject"

另一个例子:

Invoke-Expression "test`;test2"

第二个示例响应:

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

【问题讨论】:

  • 第二种情况iex "test;test2" - 不需要转义分号。

标签: powershell


【解决方案1】:

尝试使用 Start-Process 运行 MSbuild,然后使用 -Argument 将其余部分作为值传递。

【讨论】:

  • 是的,它有效。但我无法检索 MSbuild 退出代码。
  • @wallybh 要使用 Start-Process 获取退出代码,您必须做两件事,使用 -PassThru 参数并将输出分配给变量并检查其 ExitCode 属性并使用 -Wait 参数。
【解决方案2】:

作为Start-Process 的替代方案,您可以只调用该命令,就像使用调用运算符&amp; 使用cmd.exe 调用它一样:

& msbuild /t:Build;PipelinePreDeployCopyAllFilesToOneFolder /p:Configuration=Debug;_PackageTempDir=$TargetFolder $WebProject

【讨论】:

  • 太棒了!有了这个和转义字符,我解决了我的问题。谢谢!
  • @wallybh 很酷,您也应该能够像 "/t:Build;PipelinePreDeployCopyAllFilesToOneFolder" 这样引用参数,而不必转义分号。
  • 逃跑吧?
  • 不使用转义只是意味着“PRE2”被调用,因为它被视为单独的命令
  • 这意味着msbuild /p:Configuration=TEST;TEST2` 也因 MSBUILD 失败:错误 MSB1006:属性无效。开关:TEST2
【解决方案3】:

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

msbuild /t:Build`;PipelinePreDeployCopyAllFilesToOneFolder /p:Configuration=Debug`;_PackageTempDir=$TargetFolder $WebProject

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

tf.exe status . /r /workspace:WORK`;johndoe

仅供参考,这是issue has been heavily voted up on Connect。 PowerShell v3 使用新的 --% 运算符解决了这个问题:

$env:TargetFolder = $TargetFolder
msbuild $WebProject --% /t:Build;PipelinePreDeployCopyAllFilesToOneFolder /p:Configuration=Debug;_PackageTempDir=%TargetFolder%

【讨论】:

    【解决方案4】:

    以下是我使用注释用法和参数调用本机 EXE 文件的方式示例:

    # Gen-CACert.ps1
    clear-host
    
    $scriptBlock = {.\Makecert -n `"CN=PowerShell Authorite de certification`"  <# Sujet du certificat (conforme à la norme X50 #>`
                               -a sha1                                          <# Algorithme utilisé #>`
                               -eku 1.3.6.1.5.5.7.3.3                           <# Option du certificat (signature de code) #>`
                               -r                                               <# Certificat auto signé #>`
                               <# -ss `"$($args[0])`"                              Dossier de stockage du certificat #>`
                               -ss `"root`"                                     <# Dossier de stockage du certificat #>`
                               -sr localMachine                                 <# Magasin de stockage localmachine ou currentuser (defaut) #>`
                               -sv `"$($args[0]).pvk`"                          <# Nom du fichier contenant la clef privée #>`
                               `"$($args[0]).cer`"}                             <# Nom du fichier certificat #>
    
    $PoshCARoot = "PoshCARoot"
    Invoke-Command -ScriptBlock $scriptBlock  -ArgumentList $PoshCARoot
    

    【讨论】:

      【解决方案5】:

      忽略分号的最简单方法是什么?只需使用单引号而不是双引号!

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

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

      如果您想使用字符串扩展,另一种选择是使用 here-string。这里的字符串就像一个普通的字符串,但是它以 @ 符号开始和结束在它自己的单独的行上,如下所示:

      $herestring = @"
      Do some stuff here, even use a semicolon ;
      "@
      

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

      【讨论】:

        【解决方案6】:

        您可以使用逗号作为分隔符:

        msbuild /t:Build,PipelinePreDeployCopyAllFilesToOneFolder /p:Configuration=Debug,_PackageTempDir=$TargetFolder $WebProject
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-10-27
          • 1970-01-01
          • 1970-01-01
          • 2018-05-04
          • 2015-09-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多