如何在 cmd/batch 中执行 PowerShell Net MessageBox
How to execute PowerShell Net MessageBox in cmd/batch
我有一个包含很多内容的批处理文件。我有一个带有用户信息的警报 Window。
在 Windows Pro 上,我正在为它使用 Msg 命令,它工作正常。
在 Windows 主页上没有 Msg,所以我想到了使用 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
或
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,即)
您需要加载类型才能调用它。你可以这样做:
powershell -command "[reflection.assembly]::LoadWithPartialName('System.Windows.Forms')|out-null;[windows.forms.messagebox]::Show('my message')"
如 TheMadTechnician 所述,您可能需要先加载它。
这实际上与他们的答案相同,只是多了几行:
@Echo Off
PowerShell -Command^
"[Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')|Out-Null;"^
"[System.Windows.Forms.MessageBox]::Show(\"my text\")"
Pause
…虽然 my text
周围的双引号不是必需的,但我用它们来向您展示转义。
我有一个包含很多内容的批处理文件。我有一个带有用户信息的警报 Window。
在 Windows Pro 上,我正在为它使用 Msg 命令,它工作正常。
在 Windows 主页上没有 Msg,所以我想到了使用 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
或
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,即)
您需要加载类型才能调用它。你可以这样做:
powershell -command "[reflection.assembly]::LoadWithPartialName('System.Windows.Forms')|out-null;[windows.forms.messagebox]::Show('my message')"
如 TheMadTechnician 所述,您可能需要先加载它。
这实际上与他们的答案相同,只是多了几行:
@Echo Off
PowerShell -Command^
"[Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')|Out-Null;"^
"[System.Windows.Forms.MessageBox]::Show(\"my text\")"
Pause
…虽然 my text
周围的双引号不是必需的,但我用它们来向您展示转义。