Monitor-NIC
Monitors a NIC (DevideID 1 in this example) events (network cable unplugged, NIC disabled, etc.)
<# .SYNOPSIS Monitor-NIC.ps1 - Monitors a NIC connectivity events .DESCRIPTION Monitor-NIC - Monitors a NIC (DevideID 1 in this example) events (network cable unplugged, NIC disabled, etc.) .NOTES File Name : Monitor-NIC.ps1 Author : Fabrice ZERROUKI - fabricezerrouki@hotmail.com #> $Interval=5 Write-Host "" while (1 -eq 1) { $query=Get-WmiObject -Class Win32_NetworkAdapter -ComputerName localhost | Where { $_.DeviceID -eq 1 } $name=$query | % {$_.name} $status=$query | % {$_.NetConnectionStatus} if ($status -eq 0) { $EventTime=(Get-Date).ToShortTimeString() $EventDate=(Get-Date).ToShortDateString() Write-Host "`"$name`" nic has been disabled today ($EventDate) at $EventTime." -ForegroundColor DarkYellow; } elseif ($status -eq 2) { Write-Host "Network cable is connected on `"$name`" nic." -ForegroundColor DarkGreen; } elseif ($status -eq 5) { $EventTime=(Get-Date).ToShortTimeString() $EventDate=(Get-Date).ToShortDateString() Write-Host "`"$name`" nic has been disabled today ($EventDate) at $EventTime." -ForegroundColor DarkYellow; } elseif ($status -eq 7) { $EventTime=(Get-Date).ToShortTimeString() $EventDate=(Get-Date).ToShortDateString() Write-Host "Network cable has been disconnected today ($EventDate) on `"$name`" nic at $EventTime." -ForegroundColor DarkYellow; } else { $EventTime=(Get-Date).ToShortTimeString() $EventDate=(Get-Date).ToShortDateString() Write-Host "An unidentified problem has been detected today ($EventDate) on `"$name`" nic at $EventTime." -ForegroundColor DarkYellow; Write-Host "Status code is 0x$status" } sleep($Interval) }