Powershell 中的 MessageBox 未置于最前面

MessageBox in Powershell not brought to front

我使用的应用程序允许我在网络中的设备上 运行 Powershell 脚本,我需要用 MessageBox 提示用户。

我的脚本可以很好地创建 MessageBox,但我的问题是它总是显示在我的应用程序后面。我在网上尝试了一个解决方案,该解决方案建议使用 属性 Topmost=true 创建一个新表单并将其作为第一个参数传递,但它似乎没有用。有什么可以立即看出我做错了吗?

Add-Type -AssemblyName PresentationCore,PresentationFramework

$top = new-Object System.Windows.Forms.Form -property @{Topmost=$true}
$Result = [System.Windows.Forms.MessageBox]::Show($top, $MessageBody,$MessageTitle,$ButtonType,$MessageIcon)

已生成 windows 需要父代。如果他们不这样做,他们将落后于其他 windows(如您所见)。

以下内容本身与 PowerShell 无关...但它是您 know/do 到达您想去的地方所需要的...

Why isn't MessageBox TopMost?

并且可能

In Powershell how do I bring a messagebox to the foreground, and change focus to a button in the message box

您不需要使用 [System.Windows.Forms.Form] 对象,您可以使用 $this 变量作为第一个参数在一行中完成:

$Result = [System.Windows.Forms.MessageBox]::Show($this, $MessageBody, $MessageTitle, $ButtonType, $MessageIcon)

$this 方法无效。我不知道你们是从哪里获得信息的,也不知道你们是否知道如何在 Powershell 中编写代码,但你们提供的是错误信息。我测试了 $this 方法,我绝对可以向您保证,它在 PowerShell 中无法以任何方式工作。

这是在 PowerShell 中真正有效的唯一方法:

Add-Type -AssemblyName Microsoft.VisualBasic
[Microsoft.VisualBasic.Interaction]::MsgBox('MyMessage','OKOnly,SystemModal,Information', 'HeaderText')

是 SystemModal 参数强制 MsgBox 占主导地位,并且无论如何始终保持在最前面。

还有另一种记录方法,Wscript.Shell 方法,其他人声称它有效,请参见下文。

New-Object -ComObject Wscript.Shell

$wshell.Popup("MyMessage",0,"MyHeader",0 + 16 + 4096)

Where first 0 = No timeout, second 0 = OKOnly, 16 = Critical, 4096 = SystemModal

它也不起作用,因为我仍然能够在显示 MsgBox 时返回到我以前的表单并对其进行更改,这是我不想要的。