如何创建messagebox作为powershell.exe的参数?

如何创建messagebox作为powershell.exe的参数?,powershell,Powershell,如何向powershell.exe提供参数以生成消息框?这里的关键短语是powershell.exe的参数,不是来自.ps1脚本,也不是来自powershell提示符本身。我目前有此功能,但它会产生错误: powershell.exe -Command "[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms"); [System.Windows.Forms.MessageBox]::Show("Test!!

如何向powershell.exe提供参数以生成消息框?这里的关键短语是powershell.exe的参数,不是来自.ps1脚本,也不是来自powershell提示符本身。我目前有此功能,但它会产生错误:

powershell.exe -Command "[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms"); [System.Windows.Forms.MessageBox]::Show("Test!!!")"
我还尝试过不使用
-Command
调用表达式
,以及使用和不使用双引号

创建的错误:

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

这是一个报价问题。在参数及其内容中使用相同的双引号
”会弄乱内容。作为一种解决方法,请在Powershell命令中使用单引号,并在整个
-command
参数周围使用双引号。如下所示

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!!!')"
也就是说,
addtype-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!!!')"

如果您需要坚持使用双引号,请将所有内部双引号加三倍,例如在
powershell.exe-Command“…[System.Windows.Forms.MessageBox]::Show(“'Test!!!$psuirture”“”)
中。