使用命令提示符从Powershell运行消息框

3
我想实现以下内容:
我需要从批处理文件中运行psh脚本,但不是从文件中运行,而是通过命令行。我已经尝试了在ps控制台中的以下代码,它可以正常工作,但是当从cmd中传递时却不能正常工作。
powershell.exe -ExecutionPolicy Bypass -Command {[System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms'); [System.Windows.Forms.MessageBox]::Show('We are proceeding with next step.')}

请问有谁能帮忙解决这个问题吗?谢谢。

4个回答

4

将{brackets}替换为“引号”:

@powershell.exe -ExecutionPolicy Bypass -Command "[System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms'); [System.Windows.Forms.MessageBox]::Show('We are proceeding with next step.')"

(还增加了@用于关闭回显)

1

很棒的回答John。我想用稍微不同的方法来扩展它。

如果您在以下链接的评论部分查看Scripting Guys页面上关于此问题的内容,您将找到对单个命令消息弹出窗口的很好参考。

基于JohnLBevan的答案和Dan Hayward在下面页面的评论1中的代码,我组装并测试了一个非常快速的命令行,可以抑制命令的回显,并且PowerShell输出不会出现在控制台窗口中。

@powershell.exe -ExecutionPolicy Bypass -Command "(new-object -ComObject wscript.shell).Popup(Popup message', 0, 'Popup Title')" 1> nul 2>&1

Scripting Guy: PowerTip: 使用PowerShell显示弹出窗口


0

使用using语句:

powershell "using assembly system.windows.forms; using namespace system.windows.forms; [messagebox]::show('hi there')"

0

如果我没记错的话,你需要以特定的线程公寓模式运行PowerShell才能使Win Forms正常运行,因此你需要在powershell.exe命令行上使用相关开关显式地指定线程模式。


不一定。如果你有非常长时间运行的代码,作为最佳实践可能需要这样做...但同样也适用于WinForms甚至.NET总体上。 - Austin T French

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接