# we use private variables only. No need for global scope $balloon = New-Object System.Windows.Forms.NotifyIcon $cleanup = { # this gets executed when the user clicks the balloon tip dialog
# take the balloon from the event arguments, and dispose it $event.Sender.Dispose() # take the event handler names also from the event arguments, # and clean up Unregister-Event-SourceIdentifier$event.SourceIdentifier Remove-Job-Name$event.SourceIdentifier $name2 = "M" + $event.SourceIdentifier Unregister-Event-SourceIdentifier$name2 Remove-Job-Name$name2 } $showBalloon = { # this gets executed when the user clicks the tray icon $event.Sender.ShowBalloonTip(5000) }
# use unique names for event handlers so you can open multiple balloon tips $name = [Guid]::NewGuid().Guid
# subscribe to the balloon events $null = Register-ObjectEvent-InputObject$balloon-EventName BalloonTipClicked -Source$name-Action$cleanup $null = Register-ObjectEvent-InputObject$balloon-EventName MouseClick -Source"M$name"-Action$showBalloon
# use the current application icon as tray icon $path = (Get-Process-id$pid).Path $balloon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)
# configure the balloon tip $balloon.BalloonTipIcon = $Icon $balloon.BalloonTipText = $Text $balloon.BalloonTipTitle = $Title
# make the tray icon visible $balloon.Visible = $true # show the balloon tip $balloon.ShowBalloonTip(5000) }
非常古老的系统可能还没有配置为使用 WinRM 远程处理,并且可能仍然需要 DCOM。以下是演示如何使用 Get-CimInstance 和 DOM 来查询非常古老的机器的示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# change computer name to a valid remote system that you # can access remotely $computername = 'server12'
# use DCOM for older systems that do not run with WinRM remoting $option = New-CimSessionOption-Protocol Dcom $session = New-CimSession-ComputerName$computername-SessionOption$option