从 Windows 命令提示符执行 PowerShell 脚本



我安装了当前版本的64位Windows 10。

我可以打开Windows PowerShell窗口并输入以下命令来执行我的PowerShell脚本。脚本执行时没有错误。

PS C:UsersdavidDesktoptest> ./messagebox.ps1

我想从 Windows 命令提示符窗口执行相同的脚本。当我输入以下命令时,我收到显示的错误消息。

C:UsersdavidDesktoptest>powershell -ExecutionPolicy Bypass -file messagebox.ps1
At C:UsersdavidDesktoptestmessagebox.ps1:81 char:14
+ Class Form : System.Windows.Forms.Form
+              ~~~~~~~~~~~~~~~~~~~~~~~~~
Unable to find type [System.Windows.Forms.Form].
At C:UsersdavidDesktoptestmessagebox.ps1:102 char:21
+             return [System.Windows.Forms.MessageBox]::Show($messsage, ...
+                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unable to find type [System.Windows.Forms.MessageBox].
At C:UsersdavidDesktoptestmessagebox.ps1:108 char:21
+             return [System.Windows.Forms.MessageBox]::Show($messsage, ...
+                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unable to find type [System.Windows.Forms.MessageBox].
+ CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : TypeNotFound

该脚本包括以下行,我认为这些行将包含正确的程序集。

$n = new-object System.Reflection.AssemblyName("System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
[System.AppDomain]::CurrentDomain.Load($n) | Out-Null

您没有发布足够的代码来实际重现该问题,但这对我有用:

Add-Type -AssemblyName System.Windows.Forms | Out-Null
[System.Windows.Forms.MessageBox]::Show("Hello World")

我假设您可以将其扩展到您需要的任何版本的 Show((。

另请参阅PowerShell杂志

最新更新