Tray.ps1 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. $ErrorActionPreference = 'Stop'
  2. Add-Type -A System.Windows.Forms
  3. Add-Type –memberDefinition '[DllImport("user32.dll")]public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);' -name W32 -namespace MyTools –passThru
  4. [MyTools.W32]::ShowWindow((Get-Process –id $pid).MainWindowHandle, 0)
  5. $global:form = New-Object System.Windows.Forms.Form -Property @{
  6. Name = "form"
  7. Text = "form"
  8. Width = 1
  9. Height = 1
  10. Visible = $false
  11. ShowInTaskbar = $false
  12. WindowState= 'Minimized'
  13. }
  14. $global:shell = New-Object -Com "Wscript.Shell"
  15. $global:timer = New-Object System.Windows.Forms.Timer -Property @{Interval = 6000}
  16. $global:timer.add_tick({
  17. # action starts here
  18. $global:shell.SendKeys("WOLOLOT")
  19. $global:nicon.ShowBalloonTip(1000,'MyAction',"My action fired",'Info')
  20. })
  21. $global:cmenu = New-Object System.Windows.Forms.ContextMenuStrip
  22. $global:cmenu.Items.Add('Pause',$null,{
  23. if ($this.Text -eq "Pause"){
  24. $global:timer.Stop()
  25. $this.Text = "Start"
  26. }else{
  27. $global:timer.Start()
  28. $this.Text = "Pause"
  29. }
  30. }) | out-null
  31. $global:cmenu.Items.Add('Exit',$null,{
  32. $global:timer.Stop()
  33. $global:timer.Dispose()
  34. $global:nicon.Dispose()
  35. $global:form.Close()
  36. }) | out-null
  37. $global:nicon = New-Object System.Windows.Forms.NotifyIcon -Property @{
  38. Icon = [System.Drawing.Icon]::ExtractAssociatedIcon((Join-Path $env:windir "System32\SystemSettingsBroker.exe"))
  39. Visible = $true
  40. ContextMenuStrip = $global:cmenu
  41. }
  42. $global:timer.Start()
  43. $global:form.ShowDialog() | out-null