14

PowerShell でコマンドを実行したいのですが、このコマンドではセミコロンが使用されています。その後、PowerShell はそれを複数のコマンドとして解釈します。PowerShell でセミコロンを無視し、独自のコマンドとしてコマンドを実行するにはどうすればよいですか?

例:

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

もう一つの例:

Invoke-Expression "test`;test2"

2 番目の応答例:

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
4

6 に答える 6

29

コマンド ラインでセミコロンをエスケープするだけです。

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

私はこれを常に tf.exe ユーティリティで行います:

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

参考までに、この問題は Connect で多くの投票がありました--%PowerShell v3 は、新しい演算子でこの問題に対処します。

$env:TargetFolder = $TargetFolder
msbuild $WebProject --% /t:Build;PipelinePreDeployCopyAllFilesToOneFolder /p:Configuration=Debug;_PackageTempDir=%TargetFolder%
于 2012-04-13T04:03:39.117 に答える
3

セミコロンを無視する最も簡単な方法は? 二重引用符ではなく一重引用符を使用するだけです。

PowerShell では、使用する引用の種類が重要です。二重引用符を使用すると、PowerShell で文字列を展開できます (つまり、変数 $something = someprogram.exe があり、"$something" を実行すると、PowerShell は "someprogram.exe" を置き換えます)。

文字列の置換/変数の展開が必要ない場合は、単一引用符を使用してください。PowerShell は、一重引用符で囲まれた文字列をリストどおりに実行します。

文字列展開を使用する場合の別のオプションは、代わりにヒア文字列を使用することです。ヒア文字列は通常の文字列と同じですが、次のように @ 記号で始まり、@ 記号で終わります。

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

これは、ファンシーなキャラクターを使用して機能させることができるため、両方の世界の最善のシナリオですが、単一引用符では得られない変数展開を取得できます。

于 2015-07-08T14:44:52.190 に答える
1

コメント付きの使用法とパラメーターを使用してネイティブ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
于 2012-04-13T05:59:55.023 に答える
1

Start-Process を使用して MSbuild を実行し、残りを -Argument の値として渡してみてください。

于 2012-04-12T20:47:01.487 に答える
0

の代わりにStart-Process、call 演算子を使用して cmd.exe を使用して同様に呼び出すのと同じように、コマンドを呼び出すことができます&

& msbuild /t:Build;PipelinePreDeployCopyAllFilesToOneFolder /p:Configuration=Debug;_PackageTempDir=$TargetFolder $WebProject
于 2012-04-13T03:12:29.723 に答える