If you work with Windows, Windows Server or Hyper-V you know that before Windows Server 2012 Windows named the network adapters randomly. This was a huge deal if you were trying to automate deployment of servers with multiple network adapters. And of course Hyper-V Servers normally have multiple network adapters. In Windows Server 2012 Microsoft had some different ways how this was fixed. First there is CDN (Consistent Device Naming) which allows hardware vendors to integrate the names so the OS can pick them up and the second one being the possibility of Hyper-V Converged Fabric which is basically making our lives easier by having less network adapters.
Well a lot of vendors have not integrated CDN or you have some old servers without CDN support. Back in May 2012 before the release of Windows Server 2012 I wrote a little Windows PowerShell script to sort network adapters in Windows Server 2008 R2 and Hyper-V Server 2008 R2 by using WMI (Configure Hyper-V Host Network Adapters Like A Boss). Now for a Cisco UCS project I rewrote some parts of the script to use Windows PowerShell in for Windows Server 2012, Windows Server 2012 R2 and Hyper-V.
First lets have a look how you can get the PCI slot information for network adapters, luckily there is now a PowerShell cmdlet for this.
Get-NetAdapterHardwareInfo
Now lets see how you can sort network adapters via Windows PowerShell.
Get-NetAdapterHardwareInfo | Sort-Object Bus,Function
This will get you a output like this:
Lets do a little loop to automatically name them:
$prefix = "NIC" $netAdapters = Get-NetAdapterHardwareInfo | Sort-Object Bus,Function $i = 0 foreach ($netAdapter in $netAdapters){ $interface = $netadapter | Get-NetAdapter $old = $interface.Name $newName = $prefix + $i $interface | Rename-NetAdapter -NewName $newName $i++ Write-Host "Rename" $old "to:" $newName }
So this names all the network adapters to NIC1, NIC2, NIC3,…
So lets do a PowerShell function for this:
# ---------------------------------------------------------------------------------------------- # # Powershell Sort-NetworkAdapter $Rev: 748 $ # (c) 2014 Thomas Maurer. All rights reserved. # created by Thomas Maurer # www.thomasmaurer.ch # last Update by $Author: tmaurer $ on $Date: 2014-01-04 14:07:36 +0100 $ # ---------------------------------------------------------------------------------------------- # function Sort-NetworkAdapter { <# .SYNOPSIS This sorts and renames network adpaters by PCI slot .DESCRIPTION This sorts and renames network adapters sorted by PCI slot .EXAMPLE Sort-NetworkAdapter -prefix vnic -StartingNumber 0 This renames als NICs to vnic0, vnic1, vnic2,... .EXAMPLE Sort-NetworkAdapter -prefix nic -StartingNumber 1 This renames als NICs to nic1, nic2, nic3,... .PARAMETER prefix The Prefix of the network adapter name .PARAMETER StartingNumber The Number of the first network adapter #> [CmdletBinding(SupportsShouldProcess=$True,ConfirmImpact='Low')] param ( [Parameter(Mandatory=$True, ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True, HelpMessage='Which prefix you want to use?')] [ValidateLength(1,20)] [string]$prefix, [Parameter(Mandatory=$False, ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True, HelpMessage='Which Starting Number you want to use?')] [int]$startingNumber = 1 ) begin { write-verbose "Get netadpaters and sort them" $netAdapters = Get-NetAdapterHardwareInfo | Sort-Object Bus,Function } process { write-verbose "Rename netadapters" foreach ($netAdapter in $netAdapters){ $interface = $netadapter | Get-NetAdapter $old = $interface.Name $newName = $prefix + $startingNumber #$interface | Rename-NetAdapter -NewName $newName $startingNumber++ Write-Host "Rename" $old "to:" $newName } } }
Now you can run this by using Sort-NetworkAdapter for exmaple:
Sort-NetworkAdapter -prefix NIC
or
Sort-NetworkAdapter -prefix NIC -StartingNumber 0
You can also get this script from the Microsoft Technet Gallery or Script Center.
Tags: Hyper-V, Microsoft, Network, Network Adapter, Network Interface, Networking, NIC, NICs, PCI, PowerShell, script, Sort, Windows, Windows Powershell, Windows Server, Windows Server 2012, Windows Server 2012 R2 Last modified: August 27, 2018
Hi,
This is great stuff but I was wondering if you could help me with something. I’ve been trying with no luck to figure out how to take a csv or txt file with a list of computer names in it and passing it through the Get-NetAdapter cmdlet to give me back InterfaceDescription,DeviceName,DriverVersion,DriverDate,Signer and all I’ve been able to achieve is pulling the information from my local machine or on another attempt getting the script to pull the computer list but no network information comes back. Do you know how one could accomplish this?
My script goes something like this:
$arrComputers = Import-Csv C:\users\dcook\documents\computers.csv
$Interface = Get-WmiObject Win32_NetworkAdapter
#$Interface | foreach {$friendlyname = $_ | Select-Object -ExpandProperty NetConnectionID}
Get-NetAdapter | Select-Object InterfaceDescription,DeviceName,DriverVersion,Signer
“$friendlyname is $Name
foreach ($Interface in $arrComputers){gwmi -Class win32_PnPSignedDriver -Filter | {Where $._devicename=’$Interface'” | select name, DeviceName, DriverVersion, Signer
write-output $objItem
My output looks like this and what I’m really hoping to do is Export-csv the results. I’m doing this for 500 computers.
PS C:\WINDOWS\system32> C:\Users\dcook\Documents\Sccm_Reports\SCCM_Reports\TestNow.ps1
InterfaceDescription DeviceName DriverVersion Signer
——————– ———- ————- ——
Intel(R) 82574L Gigabit Network Connection \Device\{A8526C61-D366-4F3D-9152-5A505F825948} 12.12.50.4
PS C:\WINDOWS\system32>
It never seems to even look at the csv file or create one either. Another alternate way I’ve done this is here. It doesn’t seem to process the import the file either.
$csvFile = “C:\users\dcook\documents\computers.csv”
$Path = “C:\users\dcook\documents\Computers.xlxs”
$Computers = “Import-Csv -Path $csvFile”
$Excel = New-Object -ComObject excel.application
$Excel.Visible =$True
$Workbook = $Excel.workbook.add()
$Driverswksht = $workbook.worksheets.Item(1)
$driverswksht.Name = ‘Drivers’
$Excel.cells.item(1,2) = “Interface Description”
$Excel.cells.item(1.3) = “Driver Name”
$Excel.cells.item(1.4) = “Driver Version”
$Excel.cells.item(1.5) = “Driver Information”
$i = 2
#$Adapter = Get-NetAdapter -InterfaceDescription “Advanced*”, “Centrino*”, “Dual Band*”, “WiFi*”, “WiMax*” | Select Name, drivername, driverversion, driverinformation, signer
#foreach($Computer in $Computers)
#{
#$Excel.cells.item($i,2) = $Computers.InterfaceDescription
#$Excel.cells.item($i,3) = $Computers.DriverName
#$Excel.cells.item($i,4) = $Computers.DriverVersion
#$Excel.cells.item($i,5) = $Computers.DriverInformation
#$i++
#}
#$usedrange = $Excel.Used.Range
#$UsedRange.EntireColumn.AutoFit()
#$workbook.saveas(“C:\users\dcook\documents\sccm_reports\drivers.xlsx”)
Any help would be greatly appreciated.
David
Oh, I forgot to put the output from the last script above.
PS C:\WINDOWS\system32> C:\Users\dcook\Documents\Drivers.ps1
You cannot call a method on a null-valued expression.
At C:\Users\dcook\Documents\Drivers.ps1:8 char:1
+ $Workbook = $Excel.workbook.add()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At C:\Users\dcook\Documents\Drivers.ps1:9 char:1
+ $Driverswksht = $workbook.worksheets.Item(1)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
The property ‘Name’ cannot be found on this object. Verify that the property exists and can be set.
At C:\Users\dcook\Documents\Drivers.ps1:11 char:1
+ $driverswksht.Name = ‘Drivers’
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound
You cannot call a method on a null-valued expression.
At C:\Users\dcook\Documents\Drivers.ps1:12 char:1
+ $Excel.cells.item(1,2) = “Interface Description”
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At C:\Users\dcook\Documents\Drivers.ps1:13 char:1
+ $Excel.cells.item(1.3) = “Driver Name”
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At C:\Users\dcook\Documents\Drivers.ps1:14 char:1
+ $Excel.cells.item(1.4) = “Driver Version”
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At C:\Users\dcook\Documents\Drivers.ps1:15 char:1
+ $Excel.cells.item(1.5) = “Driver Information”
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
PS C:\WINDOWS\system32>
Thanks again,
David
this is great, thank you!