https://www.itninja.com/blog/view/reboot-required-toast-notifications-for-windows-machines
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") [void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | out-null [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") | out-null $TimeStart = Get-Date $TimeEnd = $timeStart.addminutes(360) Do360 { $TimeNow = Get-Date if ($TimeNow -ge $TimeEnd) { Unregister-Event -SourceIdentifier click_event -ErrorAction SilentlyContinue Remove-Event click_event -ErrorAction SilentlyContinue [void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") [void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") Exit } else { $Balloon = new-object System.Windows.Forms.NotifyIcon $Balloon.Icon = [System.Drawing.SystemIcons]::Information $Balloon.BalloonTipText = "A reboot is required in order to complete ESSO AccessAgent updating. Please reboot at your earliest convenience." $Balloon.BalloonTipTitle = "Reboot Required" $Balloon.BalloonTipIcon = "Warning" $Balloon.Visible = $true; $Balloon.ShowBalloonTip(20000); $Balloon_MouseOver = [System.Windows.Forms.MouseEventHandler]{ $Balloon.ShowBalloonTip(20000) } $Balloon.add_MouseClick($Balloon_MouseOver) Unregister-Event -SourceIdentifier click_event -ErrorAction SilentlyContinue Register-ObjectEvent $Balloon BalloonTipClicked -sourceIdentifier click_event -Action { Add-Type -AssemblyName Microsoft.VisualBasic If ([Microsoft.VisualBasic.Interaction]::MsgBox(‘Would you like to reboot your machine now?‘, ‘YesNo,MsgBoxSetForeground,Question‘, ‘System Maintenance‘) -eq "NO") { } else { shutdown -r -f } } | Out-Null Wait-Event -timeout 7200 -sourceIdentifier click_event > $null Unregister-Event -SourceIdentifier click_event -ErrorAction SilentlyContinue $Balloon.Dispose() } } Until ($TimeNow -ge $TimeEnd)
it prompts every 2 hours for 6 hours. The highlighted 360 is for the over all length and 7200 is the time (in seconds) between prompts.
执行该脚本的显示效果:单击提示信息时,会显示另外一个提示框,点Yes时,会重启电脑;选择No时,每隔两小时会再次提醒。该提示信息只显示5秒,因为Windows系统设置默认只显示5秒。
但是关闭PowerShell ISE时,该提示图标就自动退出了。
解决方法:将上面的脚本保存为NotifyReboo.ps1, 然后新建bat脚本test.bat,内容如下:
@ECHO OFF if "%1"=="hide" goto CmdBegin start mshta vbscript:createobject("wscript.shell").run("""%~0"" hide",0)(window.close)&&exit :CmdBegin SET CurrentPath=%~dp0 powershell.exe -sta -executionpolicy bypass -file "%CurrentPath%RebootNotify.ps1" Exit
也就是用test.bat脚本去调用NotifyReboot.ps1,然后在后台运行。
SET CurrentPath=%~dp0 这里是设置当前路径。需要把两个脚本放在同一个目录 下。然后,运行test.bat即可以看到上面的提示效果。
原文:https://www.cnblogs.com/rusking/p/10701517.html