Winforms 无法通过批处理文件使用windows消息执行PowerShell脚本
我有一个简单的PowerShell脚本,它使用windows.forms使用GUI显示和获取数据。 当我使用PowerShell运行它时,它可以工作,但不能通过CMD运行。 以下是.ps1示例:Winforms 无法通过批处理文件使用windows消息执行PowerShell脚本,winforms,powershell,batch-file,cmd,Winforms,Powershell,Batch File,Cmd,我有一个简单的PowerShell脚本,它使用windows.forms使用GUI显示和获取数据。 当我使用PowerShell运行它时,它可以工作,但不能通过CMD运行。 以下是.ps1示例: [System.Windows.MessageBox]::Show('message','Step 1','YesNoCancel','Question') 要执行的批处理文件: powershell "&{start-process powershell -ArgumentList ' -n
[System.Windows.MessageBox]::Show('message','Step 1','YesNoCancel','Question')
要执行的批处理文件:
powershell "&{start-process powershell -ArgumentList ' -noprofile -file c:\temp\gui.ps1' -verb RunAs} exit $LASTEXITCODE" < NUL
powershell“&{start process powershell-ArgumentList'-noprofile-file c:\temp\gui.ps1'-verb RunAs}退出$LASTEXITCODE”
我得到的错误是:找不到类型[System.Windows.MessageBox]。
我知道需要加载使用不同上下文和程序集运行的CMD,所以我尝试从CMD命令加载它,但仍然存在相同的错误
powershell "&{[System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms'); start-process powershell -ArgumentList ' -noprofile -file c:\temp\gui.ps1' -verb RunAs} exit $LASTEXITCODE" < NUL
powershell”&{[System.Reflection.Assembly]:LoadWithPartialName('System.Windows.Forms');启动进程powershell-ArgumentList'-noprofile-文件c:\temp\gui.ps1'-verb RunAs}退出$LASTEXITCODE”
错误:
有什么想法吗?你可以试试这样的方法:
PowerShell-NoProfile-Command“&{Start Process PowerShell-ArgumentList'-NoProfile-ExecutionPolicy-Bypass-File”“C:\temp\gui.ps1”“-动词RunAs}”;退出$LastExitCode
这是完全未经测试的您可以尝试以下方法:
PowerShell-NoProfile-Command“&{Start Process PowerShell-ArgumentList'-NoProfile-ExecutionPolicy-Bypass-File”“C:\temp\gui.ps1”“-动词RunAs}”;退出$LastExitCode
这是完全未经测试的好吧,我知道了。
问题在于PowerShell脚本,
我使用了[System.Windows.MessageBox]而不是[System.Windows.表单.MessageBox]
之前:
之后:
[System.Windows.Forms.MessageBox]::Show('message','Step 1','YesNoCancel','Question')
它在不更改批处理文件的情况下工作
谢谢,好的,我想好了。
问题在于PowerShell脚本,
我使用了[System.Windows.MessageBox]而不是[System.Windows.表单.MessageBox]
之前:
之后:
[System.Windows.Forms.MessageBox]::Show('message','Step 1','YesNoCancel','Question')
它在不更改批处理文件的情况下工作
谢谢,尝试在批处理文件中运行以下命令:
powershell -command [System.Windows.Forms.MessageBox]::Show('message','Step 1','YesNoCancel','Question')
我希望这会有所帮助,如果您有任何其他问题,请在下面进行评论。尝试在批处理文件中运行以下命令:
powershell -command [System.Windows.Forms.MessageBox]::Show('message','Step 1','YesNoCancel','Question')
我希望这会有所帮助,如果您有任何其他问题,请在下面进行评论。我已使用更完整的命令更新了我的答案,请尝试一下。我已使用更完整的命令更新了我的答案,请尝试一下。未解释为什么原始命令在powershell控制台中工作,而不是在从批处理执行的powershell解释中工作。未解释为什么原始命令在powershell控制台中工作,而不是在从批处理执行的powershell解释中工作。