[英]Execute a PowerShell script from a Windows command prompt
I have the current version of 64 bit Windows 10 installed. 我已经安装了64位Windows 10的当前版本。
I can open a Windows PowerShell window and enter the following command to execute my PowerShell script. 我可以打开Windows PowerShell窗口并输入以下命令来执行我的PowerShell脚本。 The script execute without error.
脚本执行无错误。
PS C:\Users\david\Desktop\test> ./messagebox.ps1
I want to execute the same script from a Windows Command Prompt window. 我想从Windows命令提示符窗口执行相同的脚本。 When I enter the follow command, I get the displayed error messages.
当我输入跟随命令时,我得到显示的错误消息。
C:\Users\david\Desktop\test>powershell -ExecutionPolicy Bypass -file messagebox.ps1
At C:\Users\david\Desktop\test\messagebox.ps1:81 char:14
+ Class Form : System.Windows.Forms.Form
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
Unable to find type [System.Windows.Forms.Form].
At C:\Users\david\Desktop\test\messagebox.ps1:102 char:21
+ return [System.Windows.Forms.MessageBox]::Show($messsage, ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unable to find type [System.Windows.Forms.MessageBox].
At C:\Users\david\Desktop\test\messagebox.ps1:108 char:21
+ return [System.Windows.Forms.MessageBox]::Show($messsage, ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unable to find type [System.Windows.Forms.MessageBox].
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : TypeNotFound
The script includes the following lines which I thought would include the correct assembly. 该脚本包括以下几行,我认为这将包括正确的汇编。
$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
You did not post enough code to actually reproduce the issue, but this works for me: 您没有发布足够的代码来实际重现此问题,但这对我有用:
Add-Type -AssemblyName System.Windows.Forms | Out-Null
[System.Windows.Forms.MessageBox]::Show("Hello World")
I assume you can extend this to whatever version of Show() you need. 我假设您可以将其扩展到所需的Show()任何版本。
See also PowerShell Magazine 另请参阅PowerShell杂志
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.