-
Notifications
You must be signed in to change notification settings - Fork 0
/
Start-ComDeviceNotifier.ps1
100 lines (83 loc) · 3.06 KB
/
Start-ComDeviceNotifier.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<#
.SYNOPSIS
Runs an event subsriber to notify when a new COM device is available
.DESCRIPTION
When the script is running, when you plug (or unplug) a COM device,
a notification will pop, telling you a COM device has been plug and what is name is
.INPUTS
None.
.OUTPUTS
None.
.NOTES
Uses the module BurntToast to display notification within the notification system of Windows 10
.NOTES
The COM Port logo comes from the serialport project website, MIT Licensed
https://github.com/serialport/website/blob/master/website/static/img/node-serialport-logo-small.svg
#>
[CmdletBinding()]
param (
# All Device Events
[Parameter()]
[switch]
$AllComDeviceEvent
)
Import-Module BurntToast
$BTHeader = New-BTHeader -Id "ComDeviceNotifier" -Title "Com Device Notifier"
$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
$NotificationSplat = @{
Header = $BTHeader
AppLogo = $(Join-Path -Path $scriptPath -ChildPath "comlogo.png" )
Silent = $true
}
New-BurntToastNotification @NotificationSplat -Text "ComDeviceNotifier Started"
$Action = [scriptblock] {
param (
#
[Parameter(Position = 0)]
[object]
$UselessCimIndicationWatcher,
# Parameter help description
[Parameter(Position = 1)]
[Microsoft.Management.Infrastructure.CimCmdlets.CimIndicationEventInstanceEventArgs]
$EventInstanceEventArgs
)
switch ($EventInstanceEventArgs.NewEvent.PSTypeNames[0]) {
"Microsoft.Management.Infrastructure.CimInstance#root/CIMV2/__InstanceCreationEvent" {
#'Device Arrival'
$EventInstanceEventArgs.NewEvent.TargetInstance.Name -match '(?<Name>COM\d)'
$ComPort = $Matches.Name
$NotificationSplat.Text = "$ComPort appeared"
}
"Microsoft.Management.Infrastructure.CimInstance#ROOT/cimv2/__InstanceDeletionEvent" {
# 'Device Removal'
$EventInstanceEventArgs.NewEvent.TargetInstance.Name -match '(?<Name>COM\d)'
$ComPort = $Matches.Name
$NotificationSplat.Text = "$ComPort disappeared"
}
default {
if ($AllComDeviceEvent) {
$EventInstanceEventArgs.NewEvent.TargetInstance.Name -match '(?<Name>COM\d)'
$ComPort = $Matches.Name
$NotificationSplat.Text = "Something happened to $ComPort. You can probably ignore this"
}
}
}
New-BurntToastNotification @NotificationSplat
}
$query = "Select * FROM __InstanceOperationEvent within 1 where targetInstance isa 'Win32_PnPEntity' and TargetInstance.PNPClass like 'Ports'"
Register-CimIndicationEvent -Query $query -Action $Action -SourceIdentifier "ComDeviceNotifier" | Out-Null
function Stop-ComDeviceNotifier {
param ( )
Unregister-Event -SourceIdentifier "ComDeviceNotifier"
New-BurntToastNotification @NotificationSplat -Text "ComDeviceNotifier Stopped"
}
try {
while ($true) {
Wait-Event -Timeout 1
}
}
catch {
}
finally {
Stop-ComDeviceNotifier
}