forked from Azure/azure-support-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindows_GhostedNIC_Check_Time_warning.ps1
More file actions
129 lines (109 loc) · 6.4 KB
/
Copy pathWindows_GhostedNIC_Check_Time_warning.ps1
File metadata and controls
129 lines (109 loc) · 6.4 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
NOTE: Cleanup may take time depending on the number of ghosted NICs detected.
- Up to 30 minutes if ~600 ghosted NICs are found.
- Up to 60+ minutes if over 1000 ghosted NICs are found.
<#
NOTE: Cleanup may take time depending on the number of ghosted NICs detected.
- Up to 30 minutes if ~600 ghosted NICs are found.
- Up to 60+ minutes if over 1000 ghosted NICs are found.
Disclaimer:
The sample scripts are not supported under any Microsoft standard support program or service.
The sample scripts are provided AS IS without warranty of any kind.
Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability
or of fitness for a particular purpose.
The entire risk arising out of the use or performance of the sample scripts and documentation remains with you.
In no event shall Microsoft, its authors, or anyone else involved in the creation, production,
or delivery of the scripts be liable for any damages whatsoever (including, without limitation,
damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss)
arising out of the use of or inability to use the sample scripts or documentation,
even if Microsoft has been advised of the possibility of such damages.
.SYNOPSIS
Detects ghosted (disconnected) and valid network interface cards (NICs) on Windows.
Author: Marcus Ferreira marcus.ferreira[at]microsoft[dot]com
Version: 0.2 (Modified by Copilot for enhanced messaging)
.DESCRIPTION
This script scans the Windows registry for network adapters on PCI and VMBUS buses,
compares them with currently active network adapters, and identifies ghosted NICs.
Useful for troubleshooting network issues or cleaning up old NICs.
.NOTES
Requires administrator privileges.
Tested on Windows Server 2016+.
.EXAMPLE
Run as administrator:
PS> .\Windows_GhostedNIC_Check_Time_warning.ps1
#>
# Ensure script runs with administrator privileges
If (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Write-Error "This script must be run as Administrator."
exit 1
}
Write-Host "`r`nInitializing NIC scan..." -ForegroundColor Cyan
Write-Host "NOTE: Cleanup may take time depending on the number of ghosted NICs detected."
Write-Host " - Up to 30 minutes if ~600 ghosted NICs are found."
Write-Host " - Up to 60+ minutes if over 1000 ghosted NICs are found.`r`n" -ForegroundColor Yellow
# Get all current network adapter PnP IDs
Write-Host "Collecting all current network adapter PnPDeviceIDs..." -ForegroundColor White
$AllAdaptersPnPIDs = (Get-NetAdapter).PnpDeviceID
$global:FoundGhostNICs = 0
$global:FoundValidNICs = 0
Function ScanRegistryForNICs($BusName) {
$RootPath = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\"
$RegistryPath = "$($RootPath.Replace("HKEY_LOCAL_MACHINE\","HKLM:\"))$($BusName)"
Write-Host "`r`nScanning $BusName devices in registry..." -ForegroundColor Cyan
Try {
If (Test-Path -Path $RegistryPath) {
$Items = Get-ChildItem $RegistryPath -Depth 0
$i = 0
$Total = $Items.Count
$Items | ForEach-Object {
$i++
Write-Progress -Activity "Scanning $BusName registry entries" -Status "Processing $i of $Total" -PercentComplete (($i / $Total) * 100)
$Bus = $_
$BusPath = $Bus.Name.ToString().Replace("HKEY_LOCAL_MACHINE\","HKLM:\")
Get-ChildItem $BusPath -Depth 0 | ForEach-Object {
$Bus = $_
$BusPath = $Bus.Name.ToString().Replace("HKEY_LOCAL_MACHINE\","HKLM:\")
$BusRelativeName = $Bus.Name.ToString().Replace($RootPath,"")
Try { $ServiceType = (Get-ItemProperty -Path $BusPath -Name Service -ErrorAction SilentlyContinue).Service } Catch { $ServiceType = $null }
Try { $DeviceDesc = (Get-ItemProperty -Path $BusPath -Name DeviceDesc -ErrorAction SilentlyContinue).DeviceDesc } Catch { $DeviceDesc = $null }
Try { $DevIndex = (Get-ItemProperty -Path "$BusPath\Device Parameters" -Name InstanceIndex -ErrorAction SilentlyContinue).InstanceIndex } Catch { $DevIndex = $null }
If ($DeviceDesc) {
Try {
$Split = $DeviceDesc -split ";"
$DeviceDescription = $Split[1]
If ($DevIndex -and $DevIndex -ne 1) {
$DeviceDescription += " #$($DevIndex)"
}
} Catch {
$DeviceDescription = "Unknown Device"
}
} Else {
$DeviceDescription = "Unknown Device"
}
If ($ServiceType -in ("netvsc", "mlx5", "mlx4_bus")) {
$MatchesPnpID = $AllAdaptersPnPIDs | Where-Object { $_ -eq $BusRelativeName }
If ($MatchesPnpID) {
$global:FoundValidNICs++
Write-Host "Valid NIC: $($DeviceDescription)" -ForegroundColor Green
} Else {
$global:FoundGhostNICs++
Write-Host "Ghosted NIC: $($DeviceDescription)" -ForegroundColor Yellow
}
}
}
}
} Else {
Write-Host "Registry path $($RegistryPath) not found, skipping..." -ForegroundColor DarkGray
}
} Catch {
Write-Error "Error while scanning registry: $_"
}
}
ScanRegistryForNICs("PCI")
ScanRegistryForNICs("VMBUS")
Write-Host "`r`nScan complete. Summary:" -ForegroundColor Cyan
Write-Host "Found ghosted NIC(s): $($FoundGhostNICs)" -ForegroundColor Red
Write-Host "Found valid NIC(s): $($FoundValidNICs)" -ForegroundColor Green
Write-Host "`r`nEstimated cleanup time guidance:" -ForegroundColor Cyan
Write-Host " - ~30 mins for ~600 ghosted NICs" -ForegroundColor Yellow
Write-Host " - ~60+ mins for >1000 ghosted NICs`r`n" -ForegroundColor Yellow
Write-Host "Script completed successfully." -ForegroundColor Cyan