https://www.itninja.com/blog/view/reboot-required-toast-notifications-for-windows-machinesshell
[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.windows
執行該腳本的顯示效果:單擊提示信息時,會顯示另一個提示框,點Yes時,會重啓電腦;選擇No時,每隔兩小時會再次提醒。該提示信息只顯示5秒,由於Windows系統設置默認只顯示5秒。bash
可是關閉PowerShell ISE時,該提示圖標就自動退出了。ide
解決方法:將上面的腳本保存爲NotifyReboo.ps1, 而後新建bat腳本test.bat,內容以下:ui
@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,而後在後臺運行。 spa
SET CurrentPath=%~dp0 這裏是設置當前路徑。須要把兩個腳本放在同一個目錄 下。而後,運行test.bat便可以看到上面的提示效果。orm