简体   繁体   English

脚本Powershell之后当前用户的弹出消息

[英]Popup message for current user after script powershell

I created PowerShell script wich install an application on computer (windows 7). 我创建了PowerShell脚本,以便在计算机上安装应用程序(Windows 7)。 This script is in GPO and deployed with GPO at logon users. 该脚本位于GPO中,并与GPO一起部署在登录用户处。 This worked fine, but I want that at the end of installation, my powershell script send at the current logged user on computer a message like "Reboot your computer please". 这个工作正常,但是我希望在安装结束时,我的powershell脚本向计算机上当前登录的用户发送一条消息,例如“请重新启动计算机”。

I tested many things but I don'tview popup, maybe because my script are execute with admin rights (not with user rights). 我测试了很多东西,但是我没有查看弹出窗口,也许是因为我的脚本是使用管理员权限(而不是用户权限)执行的。

Test : 测试:

#$wshell = New-Object -ComObject Wscript.Shell
#$wshell.Popup("Operation Completed",0,"Done",0x1)


[Windows.Forms.MessageBox]::Show(“My message”, , [Windows.Forms.MessageBoxButtons]::OK, [Windows.Forms.MessageBoxIcon]::Information)

Your script may be popping up the message but then closing the PowerShell console immediately after, removing the popup. 您的脚本可能会弹出消息,但之后立即关闭PowerShell控制台,并删除弹出窗口。 Try waiting on the result of the popup before closing the PowerShell instance: 在关闭PowerShell实例之前,请尝试等待弹出结果:

$wshell = New-Object -ComObject Wscript.Shell
$result = $wshell.Popup("Operation Completed",0,"Done",0x1)

You need to load the assembly providing the MessageBox class first, and you cannot omit the message box title if you want to specify buttons and/or icons. 您需要首先加载提供MessageBox类的程序集,并且如果要指定按钮和/或图标,则不能省略消息框标题。

Add-Type -Assembly 'System.Windows.Forms'

[Windows.Forms.MessageBox]::Show(“My message”, "", [Windows.Forms.MessageBoxButtons]::OK, [Windows.Forms.MessageBoxIcon]::Information)
#                                              ^^

You can use an empty string or $null here, but simply not providing a value (like you could do in VBScript) is not allowed. 您可以在此处使用空字符串或$null ,但不允许仅不提供值(就像在VBScript中那样)。

As a side-note, I'd recommend avoiding typographic quotes in your code. 作为附带说明,我建议您在代码中避免使用印刷引号。 Although PowerShell will tolerate them most of the time, they might cause issues sometimes. 尽管PowerShell在大多数情况下都能容忍它们,但有时它们可​​能会引起问题。 Always use straight quotes to be on the safe side. 为了安全起见,请始终使用直引号。


Edit: Since you're running the script via a machine policy it cannot display message boxes to the logged-in user, because it's running in a different user context. 编辑:由于您是通过计算机策略运行脚本的,因此它无法向登录的用户显示消息框,因为它是在不同的用户上下文中运行的。 All you can do is have a user logon script check whether the software is installed, and then display a message to the user. 您所能做的就是让用户登录脚本检查是否已安装软件,然后向用户显示一条消息。 This works, because a user logon script running in the user's context. 之所以可行,是因为在用户上下文中运行的用户登录脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM