Set-Events.ps1

###############################################################################
# This may need editing to suit. Get the actual modem name fom the command Get-
# WmiObject Win32_NetworkAdapterConfiguration | Format-List Description and it
# should be somewhere in the list.
# Trap any HUAWEI 4G modem USB insertion events (SourceId = Online):
###############################################################################

$action1 = {    #NOTE: if we use __InstanceCreationvent we may get a bunch of events instead of just one.
   $ID = '5003'
   $e = $Event.SourceEventArgs.NewEvent
   $name  = $e.TargetInstance.Description
   $desc = 'HUAWEI modem device inserted. SIM Internet CONNECT event.'
   $eventLog = "Internet Explorer"
   If ($name.startsWith('Remote NDIS based Internet Sharing Device') -or `
      ($name -eq 'HUAWEI Mobile Connect - 3G Network Card')) {  
      Write-EventLog -Logname $eventLog -Source 'HUAWEI' -EntryType Information -EventID $ID -Message $desc
      [Console]::Beep(800,500)
   }
}

$query1 = "SELECT * FROM __InstanceModificationEvent WITHIN 4 WHERE TargetInstance ISA `
  'Win32_NetworkAdapterConfiguration' AND TargetInstance.IPEnabled = TRUE AND `
     TargetInstance.IPEnabled <> PreviousInstance.IPEnabled"


###############################################################################
#This ACTION will trap any modem USB removal events (SourceId = Offline):
###############################################################################

$action2 = {         #Huawei E3372 registers no VolumeName so removing any USB will trigger.
   $eventLog = "Internet Explorer"
   $ID = '5004'                        
   $e = $Event.SourceEventArgs.NewEvent
   $name  = $e.TargetInstance.Description
   $desc = "HUAWEI modem device removed. SIM Internet DISCONNECT event." 
   If ($name.startsWith('Remote NDIS based Internet Sharing Device') -or `
      ($name -eq 'HUAWEI Mobile Connect - 3G Network Card')) {
      Write-EventLog -Logname $eventLog -Source 'HUAWEI' -EntryType Information -EventID $ID -Message $desc
      $GLOBAL:huawei = "OFFLINE"
      [Console]::Beep(800,500)
   }
}

$query2 = "SELECT * FROM __InstanceModificationEvent WITHIN 4 WHERE TargetInstance ISA `
  'Win32_NetworkAdapterConfiguration' AND TargetInstance.IPEnabled = FALSE AND `
     TargetInstance.IPEnabled <> PreviousInstance.IPEnabled"


$events = (Get-Eventsubscriber -Force)
[Int]$count = 0
$events | foreach-Object {     
   if ($_.SourceIdentifier -eq 'Online' -or $_.SourceIdentifier -eq 'Offline') {
      $count++  #This will be 2 if both are registered.
   }
}               #Below can also be 'Windows PowerShell ISE Host'. Edit to suit.
if ($host.Name -eq 'ConsoleHost' -and $count -eq 0) {
   Write-Host "Registering subscriptions to HUAWEI Modem 'Online' and 'Offline' USB events."
   Register-WmiEvent -Query $query1 -SourceIdentifier 'Online'  -SupportEvent -Action $action1 `
     | Out-Null
   Register-WmiEvent -Query $query2 -SourceIdentifier 'Offline' -SupportEvent -Action $action2 `
     | Out-Null
}