Make PowerShell ignore semicolon

后端 未结 6 2426
清歌不尽
清歌不尽 2021-02-19 06:54

I want to execute a cmd on PowerShell and this command uses semicolons. Then PowerShell interprets it as multiple commands. How do I make PowerShell ignore the semicolons and ex

相关标签:
6条回答
  • 2021-02-19 06:59

    The easiest way to ignore a semicolon? Simply use a single quote versus double quote!

    In PowerShell, the type of quote you use matters. A double quote will let PowerShell do string expansion (so if you have a variable $something = someprogram.exe, and run "$something", PowerShell substitutes in "someprogram.exe").

    If you don't need string substitution/variable expansion then just use single-quotes. PowerShell will execute single-quoted strings exactly as listed.

    Another option if you want to use string expansion is to use a here-string instead. A here string is just like a regular string, however it begins and ends with an @ sign on its own separate line, like so:

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

    This is a best-of-both-worlds scenario, as you can use your fancy characters and have them work, but still get Variable expansion, which you do not get with single-quotes.

    0 讨论(0)
  • 2021-02-19 06:59

    Try using Start-Process to run MSbuild then pass the rest as a value with -Argument.

    0 讨论(0)
  • 2021-02-19 07:06

    Just escape the semicolon on the command line:

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

    I do this all the time with the tf.exe utility:

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

    FYI, this issue has been heavily voted up on Connect. PowerShell v3 addresses this issue with the new --% operator:

    $env:TargetFolder = $TargetFolder
    msbuild $WebProject --% /t:Build;PipelinePreDeployCopyAllFilesToOneFolder /p:Configuration=Debug;_PackageTempDir=%TargetFolder%
    
    0 讨论(0)
  • 2021-02-19 07:08

    You can use comma as the separator:

    msbuild /t:Build,PipelinePreDeployCopyAllFilesToOneFolder /p:Configuration=Debug,_PackageTempDir=$TargetFolder $WebProject
    
    0 讨论(0)
  • 2021-02-19 07:09

    As an alternative to Start-Process, you can just call the command as you would similarly call it using cmd.exe using the call operator &:

    & msbuild /t:Build;PipelinePreDeployCopyAllFilesToOneFolder /p:Configuration=Debug;_PackageTempDir=$TargetFolder $WebProject
    
    0 讨论(0)
  • 2021-02-19 07:21

    Here is an example of the way I use to call native EXE files with commented usage and parameters:

    # 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
    
    0 讨论(0)
提交回复
热议问题