![](/img/trans.png)
[英]How do I call an elevated PowerShell from Windows command prompt to execute commands and a PowerShell script file?
[英]Execute a PowerShell script from a Windows command prompt
我已经安装了64位Windows 10的当前版本。
我可以打开Windows PowerShell窗口并输入以下命令来执行我的PowerShell脚本。 脚本执行无错误。
PS C:\Users\david\Desktop\test> ./messagebox.ps1
我想从Windows命令提示符窗口执行相同的脚本。 当我输入跟随命令时,我得到显示的错误消息。
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
该脚本包括以下几行,我认为这将包括正确的汇编。
$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杂志
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.