2017-09-22 32 views
0

私はたくさんのものを持つバッチファイルを持っています。私はユーザーのための情報と1つのアラートウィンドウがあります。コマンド/バッチでPowerShell Net MessageBoxを実行する方法

Windows Proではメッセージングコマンドを使用していますが、正常に動作します。 Windowsのホームで

にはメッセージがないので、私が代わりにPowerShellを使用するようにアイデアを得た:PowerShellで正常に動作します

[System.Windows.Forms.MessageBox]::Show("my text") 

-However、私はバッチでそれを使用するか、Cmdを直接それを実行しようとしたとき、私はテキストのみを取得:

C:\Windows\System32>powershell {[System.Windows.Forms.MessageBox]::Show("\""my text"\"")} 
    [System.Windows.Forms.MessageBox]::Show("my text") 

または私はエラーを取得:

C:\Windows\System32>powershell -command [System.Windows.Forms.MessageBox]::Show("my text") 
At line:1 char:41 
+ [System.Windows.Forms.MessageBox]::Show(my text) 
+           ~ 
Missing ')' in method call. 
At line:1 char:41 
+ [System.Windows.Forms.MessageBox]::Show(my text) 
+           ~~ 
Unexpected token 'my' in expression or statement. 
At line:1 char:48 
+ [System.Windows.Forms.MessageBox]::Show(my text) 
+            ~ 
Unexpected token ')' in expression or statement. 
    + CategoryInfo   : ParserError: (:) [], ParentContainsErrorRecordException 
    + FullyQualifiedErrorId : MissingEndParenthesisInMethodCall 

または

 C:\Windows\System32>powershell -command "& {[System.Windows.Forms.MessageBox]::Show('my text')}" 
Unable to find type [System.Windows.Forms.MessageBox]. 
At line:1 char:4 
+ & {[System.Windows.Forms.MessageBox]::Show('my text')} 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : InvalidOperation: (System.Windows.Forms.MessageBox:TypeName) [], 
    RuntimeException 
    + FullyQualifiedErrorId : TypeNotFound 

動作させるにはどうすればよいですか?

(PowerShellのにスクリプト全体を書き換えることなく、つまり)

答えて

0

あなたはそれを呼び出すことができる前にタイプをロードする必要があります。これを行うことができます:

powershell -command "[reflection.assembly]::LoadWithPartialName('System.Windows.Forms')|out-null;[windows.forms.messagebox]::Show('my message')" 
1

TheMadTechnicianが述べたように、最初にロードする必要があります。

これは彼らと同じ答えが行だけのカップルの上に効果的である:my text周り

@Echo Off 
PowerShell -Command^ 
"[Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')|Out-Null;"^ 
"[System.Windows.Forms.MessageBox]::Show(\"my text\")" 
Pause 

...としながら、二重引用符は必要ありません、私はあなたのエスケープを表示するためにそれらを使用しました。

関連する問題