2017-11-14 3 views
1

メッセージボックスを生成するためにpowershell.exeに引数を与える方法を教えてください。ここで重要なのはpowershell.exeの引数であり、.ps1スクリプトからではなく、Powershellプロンプト自体からではありません。私は現在、これを持っているが、それは誤りを生産している:powershell.exeの引数としてmessageboxを作成するには?

powershell.exe -Command "[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms"); [System.Windows.Forms.MessageBox]::Show("Test!!!")" 

私も周囲の二重引用符でとせずに、-CommandなしとInvoke-Expressionで試してみました。作成した

エラー:

At line:1 char:51 
+ [System.Reflection.Assembly]::LoadWithPartialName(System.Windows.Form ... 
+             ~ 
Missing ')' in method call. 
At line:1 char:51 
+ ... eflection.Assembly]::LoadWithPartialName(System.Windows.Forms); [Syst ... 
+            ~~~~~~~~~~~~~~~~~~~~ 
Unexpected token 'System.Windows.Forms' in expression or statement. 
At line:1 char:71 
+ ... flection.Assembly]::LoadWithPartialName(System.Windows.Forms); [Syste ... 
+                 ~ 
Unexpected token ')' in expression or statement. 
At line:1 char:114 
+ ... stem.Windows.Forms); [System.Windows.Forms.MessageBox]::Show(Test!!!) 
+                 ~ 
Missing ')' in method call. 
At line:1 char:114 
+ ... stem.Windows.Forms); [System.Windows.Forms.MessageBox]::Show(Test!!!) 
+                 ~~~~~~~ 
Unexpected token 'Test!!!' in expression or statement. 
At line:1 char:121 
+ ... stem.Windows.Forms); [System.Windows.Forms.MessageBox]::Show(Test!!!) 
+                   ~ 
Unexpected token ')' in expression or statement. 
    + CategoryInfo   : ParserError: (:) [], ParentContainsErrorRecordException 
    + FullyQualifiedErrorId : MissingEndParenthesisInMethodCall 

答えて

1

これは、引用の問題です。同じ二重引用符を使用すると、引数とその内容の両方で"が内容を壊します。回避策として、Powershellコマンド内の一重引用符を使用し、-Commandパラメータ全体を二重引用符で囲みます。言われて、Add-Type -AssemblyNameはアセンブリをロードするためにきれいな方法イマオされていることを

powershell.exe -Command "[System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms'); [System.Windows.Forms.MessageBox]::Show('Test!!!')" 

、とても好きです。同様に、

powershell.exe -Command "add-type -assemblyname System.Windows.Forms; [System.Windows.Forms.MessageBox]::Show('Test!!!')" 
+0

二重引用符**を必要とする場合は、すべて二重引用符を使用します。 'powershell.exe -Command '... [System.Windows.Forms.MessageBox] :: Show(" "Test !!! $ PSUICulture" "") "'と同様です。 – JosefZ

関連する問題