zoukankan      html  css  js  c++  java
  • PowerShell 显示气球提示框 2

      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即可以看到上面的提示效果。

  • 相关阅读:
    重复造轮子感悟 – XLinq性能提升心得
    分享动态拼接Expression表达式组件及原理
    拦截Response.Redirect的跳转并转换为Js的跳转
    高仿QQ即时聊天软件开发系列之三登录窗口用户选择下拉框
    高仿QQ即时聊天软件开发系列之二登录窗口界面
    高仿QQ即时聊天软件开发系列之一开端
    关于404二级目录或三级目录不显示图片的方法
    如何在cmd查看文件内容的MD5值
    VMware虚拟机下载与安装
    dede自定义表单放首页出错的解决办法
  • 原文地址:https://www.cnblogs.com/rusking/p/10701517.html
Copyright © 2011-2022 走看看