Trying to get a PS script to monitor and log NIC connection drops

Summary

The goal of this project is to create a Powershell GUI that monitors and logs NIC connection drops. The script should record the NIC name, event ID, description, time dropped, and time established in a .csv file. The file should be saved in a folder named NIC_Drops located in the root of the C drive.

Root Cause

The issue with the current script is that it doesn’t register events when the NIC connection drops. This could be due to several reasons, including:

  • Insufficient permissions to access the NIC events
  • Incorrect event filtering that prevents the script from capturing the desired events
  • Inadequate error handling that causes the script to fail when encountering errors

Why This Happens in Real Systems

In real-world systems, NIC connection drops can occur due to various reasons such as:

  • Network congestion
  • Hardware failures
  • Software issues
  • Configuration problems
    These issues can cause the NIC to drop connections, resulting in downtime and loss of productivity.

Real-World Impact

The impact of NIC connection drops can be significant, including:

  • Loss of business critical data
  • Downtime and loss of productivity
  • Increased support requests and troubleshooting efforts
  • Decreased user satisfaction and confidence in the system

Example or Code

# Get the current date and time
$currentTime = Get-Date

# Define the path to the log file
$csvPath = "C:\NIC_Drops\$env:COMPUTERNAME-$currentTime.ToString('yyyy-MM-dd-HH-mm-ss').csv"

# Create the log file if it doesn't exist
if (-not (Test-Path $csvPath)) {
    New-Item -Path $csvPath -ItemType File | Out-Null
}

# Define the log entries
$logEntries = @()

# Get the NIC adapters
$adapters = Get-NetAdapter -ErrorAction Stop | Where-Object { $_.Status -ne $null }

# Loop through the adapters and check for connection drops
foreach ($adapter in $adapters) {
    $name = $adapter.Name
    $status = $adapter.Status.ToString()
    $admin = $adapter.AdminStatus.ToString()
    $desc = $adapter.InterfaceDescription

    # Check if the adapter is disconnected or down
    if ($status -in @('Disconnected', 'Down')) {
        # Create a log entry
        $logEntry = [PSCustomObject]@{
            Timestamp = $currentTime.ToString('yyyy-MM-dd HH:mm:ss')
            AdapterName = $name
            Status = $status
            AdminStatus = $admin
            Description = $desc
            Notes = "Link lost or adapter down"
        }

        # Add the log entry to the list
        $logEntries += $logEntry
    }
}

# Export the log entries to the csv file
$logEntries | Export-Csv -Path $csvPath -NoTypeInformation -Encoding UTF8

How Senior Engineers Fix It

Senior engineers fix this issue by:

  • Reviewing the script and identifying the root cause of the problem
  • Implementing proper error handling to prevent the script from failing
  • Using the correct event filtering to capture the desired events
  • Testing the script thoroughly to ensure it works as expected

Why Juniors Miss It

Junior engineers may miss this issue due to:

  • Lack of experience with Powershell and NIC events
  • Inadequate understanding of error handling and event filtering
  • Insufficient testing and troubleshooting of the script
  • Failure to review the script thoroughly and identify potential issues