如何在cmd/batch中执行PowerShell Net MessageBox
我有一个包含很多东西的批处理文件。I有一个警报窗口,其中包含用户的信息 在Windows Pro上,我使用Msg命令,它运行良好 在Windows Home上没有Msg,因此我想到改用PowerShell:如何在cmd/batch中执行PowerShell Net MessageBox,powershell,batch-file,cmd,messagebox,Powershell,Batch File,Cmd,Messagebox,我有一个包含很多东西的批处理文件。I有一个警报窗口,其中包含用户的信息 在Windows Pro上,我使用Msg命令,它运行良好 在Windows Home上没有Msg,因此我想到改用PowerShell: [System.Windows.Forms.MessageBox]::Show("my text") 这在PowerShell中运行良好 -但是,当我尝试在批处理中使用它或直接在Cmd中执行它时,我只得到以下文本: C:\Windows\System32>powershell {
[System.Windows.Forms.MessageBox]::Show("my text")
这在PowerShell中运行良好
-但是,当我尝试在批处理中使用它或直接在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
或
我该怎么做才能让它工作
(无需将整个脚本重写到PowerShell,也就是说)您需要先加载类型,然后才能调用它。您可以这样做:
powershell -command "[reflection.assembly]::LoadWithPartialName('System.Windows.Forms')|out-null;[windows.forms.messagebox]::Show('my message')"
您需要先加载类型,然后才能调用它。您可以这样做:
powershell -command "[reflection.assembly]::LoadWithPartialName('System.Windows.Forms')|out-null;[windows.forms.messagebox]::Show('my message')"
正如MadTechnician所述,您可能需要先加载它 这实际上与他们的答案完全相同,只是有几行:
@Echo关闭
PowerShell-命令^
“[Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')| Out Null;”^
“[System.Windows.Forms.MessageBox]::显示(\'my text\”)
暂停
…虽然在
我的文本
周围不需要双引号,但我已经用它们向您展示了转义。正如广告技术人员所说,您可能需要先加载它
这实际上与他们的答案完全相同,只是有几行:
@Echo关闭
PowerShell-命令^
“[Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')| Out Null;”^
“[System.Windows.Forms.MessageBox]::显示(\'my text\”)
暂停
…虽然在我的文本
周围不需要双引号,但我已经用它们向您展示了转义