2016-09-30 9 views
0

通常、PowerShellコマンドはセミコロンで連鎖できます。以下は2つのメモ帳を開きます。特別なPowerShell 4コマンドプロンプトでコマンドをチェーンする方法は?

PS> notepad; notepad 

あなたは、より複雑な文を、連鎖することができます

PS> Add-Type -AssemblyName System.IO.Compression; ` 
> $src = "C:\aFolder"; $zip="C:\my.zip"; ` 
> [io.compression.zipfile]::CreateFromDirectory($src, $zip) 

チェインドPowerShellコマンド もCMDコマンドラインから呼び出すことができます。

C:\> powershell notepad; notepad 

This postには、.Net 2.0がお使いのOSのアクティブなフレームワークであっても、.Net 4.0 PowerShellプロンプトを作成する方法が記載されています。 .cmdスクリプトを作成して実行します。今あなたは.Net 4.0環境にいます。

連鎖は、その4.0プロンプトで動作します。

C:\> ps4.cmd 
PS> notepad; notepad 

も標準CMDプロンプトから動作します:

C:\> ps4 notepad; notepad 

This postは、4.0を参照するために必要な明示的なパス名(へAdd-Typeを行う方法を説明しps4プロンプトからのアセンブリ):

Add-Type -Path "C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.IO.Compression.FileSystem\v4.0_4.0.0.0__b77a5c561934e089\System.IO.Compression.FileSystem.dll" 

C:\> ps4 
PS> Add-Type -Path "C:\x\System.IO.Compression.FileSystem.dll"; ` 
> $src = "C:\x\xl"; $zip="C:\x\xl.zip"; ` 
> [io.compression.zipfile]::CreateFromDirectory($src, $zip) 

問題:PS4プロンプトで連鎖した場合にも、働くPS4は(ポストの下部に全額エラー)標準のコマンドプロンプトで起動したときに上記のステートメントの連鎖が失敗します

C:\> ps4 Add-Type -Path "C:\x\System.IO.Compression.FileSystem.dll"; $src = "C:\x\xl"; $zip="C:\x\xl.zip"; [io.compression.zipfile]::CreateFromDirectory($src, $zip) 

しかし、上記のすべての方法が機能します。どうして?どうすればこの作品を作れますか?デイジーチェーン接続コマンドがpowershell.exeに渡されたときに、文字列の周り

The term 'C:\x\xl' 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:73 
+ Add-Type -Path C:\x\System.IO.Compression.FileSystem.dll; $src = C:\x\xl <<<< ; $zip=C:\x\xl.zip; 
[io.compression.zipfile]::CreateFromDirectory($src, $zip) 
    + CategoryInfo   : ObjectNotFound: (C:\x\xl:String) [], CommandNotFoundException 
    + FullyQualifiedErrorId : CommandNotFoundException 

The term 'C:\x\xl.zip' 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:91 
+ Add-Type -Path C:\x\System.IO.Compression.FileSystem.dll; $src = C:\x\xl; $zip=C:\x\xl.zip <<<< ; 
[io.compression.zipfile]::CreateFromDirectory($src, $zip) 
    + CategoryInfo   : ObjectNotFound: (C:\x\xl.zip:String) [], CommandNotFoundException 
    + FullyQualifiedErrorId : CommandNotFoundException 

Exception calling "CreateFromDirectory" with "2" argument(s): "The path is not 
of a legal form." 
At line:1 char:138 
+ Add-Type -Path C:\x\System.IO.Compression.FileSystem.dll; $src = C:\x\xl; $zip=C:\x\xl.zip; 
[io.compression.zipfile]::CreateFromDirectory <<<< ($src, $zip) 
    + CategoryInfo   : NotSpecified: (:) [], MethodInvocationException 
    + FullyQualifiedErrorId : DotNetMethodException

答えて

1

二重引用符が削除されます。パスにスペースが含まれていないので、それは引用符を必要としないのでAdd-Typeは、この影響を受けません。

Add-Type -Path C:\x\System.IO.Compression.FileSystem.dll # <- this works 

はしかし、代入演算にPowerShellは、それ以外の場合は解釈する、引用符で囲むように文字列を要求しますその文字列をコマンドとして実行してみてください:

$src = C:\x\xl # <- this would try to run a (non-existent) command 'C:\x\xl' 
       # and assign its output to the variable instead of assigning 
       # the string "C:\x\xl" to the variable 

これは、あなたが観察した最初の2つのエラーの原因です。 3つ目のエラーは、2つのパス変数が適切に初期化されていないため、後続のフォルトです。

あなたは二重引用符エスケープすることにより、この動作を回避することができるはずです。

ps4 Add-Type -Path \"C:\x\System.IO.Compression.FileSystem.dll\"; $src = \"C:\x\xl\"; $zip=\"C:\x\xl.zip\"; [io.compression.zipfile]::CreateFromDirectory($src, $zip) 

するか、後者はクォート文字としてCMDによって認識されていないので、れたまま渡されているので(単一引用符でそれらを置き換えることにより、

ps4 Add-Type -Path 'C:\x\System.IO.Compression.FileSystem.dll'; $src = 'C:\x\xl'; $zip='C:\x\xl.zip'; [io.compression.zipfile]::CreateFromDirectory($src, $zip) 

しかし、むしろこの問題を回避作業よりも、私が作成し、適切な実行しているお勧めします。)クォート文字として認識しPowerShellの、にありますあなたはこの問題を完全に避けることができるようにPowerShellスクリプト、:

#requires -version 4 
[CmdletBinding()] 
Param(
    [Parameter(Mandatory=$true)] 
    [string]$Path, 
    [Parameter(Mandatory=$true)] 
    [string]$Zipfile, 
) 

Add-Type -Assembly 'System.IO.Compression.FileSystem' | Out-Null 
[IO.Compression.ZipFile]::CreateFromDirectory($Path, $Zipfile) 

スクリプトは次のように実行されます:あなたはRemoteSignedまたはUnrestrictedに実行ポリシーを変更してものデフォルトのハンドラを変更した場合

powershell.exe -File zip.ps1 -Path "C:\x\xl" -Zipfile "C:\x\xl.zip" 

.ps1ファイルの場合は、次のようなスクリプトを実行することもできます。

zip.ps1 -Path "C:\x\xl" -Zipfile "C:\x\xl.zip" 
+0

awesome!あなたのソリューションは動作します:二重引用符をエスケープするか、またはシングルを使用します。私は適切なスクリプトを使いたいのですが、どうすればいいのか分かりません。私の使用例は:私はVBからの単一のステートメントでプロセス全体を実行する必要があります。複数のステートメントでPS4.cmdを呼び出すと、独立したセッションになります.1つの呼び出しでアセンブリを追加すると、後続の呼び出しに接続できなくなります。また、私はVBコードでコマンドを埋め込む必要がありますので、 ';で連鎖することはVBでスクリプトを埋め込むよりも便利で、スクリプトをバッチファイルに保存してからスクリプトを実行する方が便利です。どうも! –

+0

私はあなたの答えをここにリンクしました:http://stackoverflow.com/a/39800779/209942 –

関連する問題