How to use Powershell to remotely disable a networkadapter on an array of computers

Posted by O.Sommer

Today I had the challenge to disable a specifically named NIC on a larger number of Hyper-V hosts, because that card was causing issues with the cluster compatibility check when joining those Nodes to a Windows Hyper-V Cluster.
I found that disabling 64 Networkadapters on 64 Servers would take a while if you use RDP to remote into each server, so I wrote this little script to automate this using Remote Powershell, which is enabled by default if those computers are Part of a domain, or I enabled it during the deployment phase, which I can not remember right now:

# ask for Admin password
$cred = Get-Credential -Credential hypercluster\Administrator
# run through all i nodes and disable USB NIC
for ($i = 1;$i -le 64;$i++){
Echo Node$i
Enter-PSSession -ComputerName Node$i -Credential $cred
$usbnic = Get-WmiObject win32_networkadapter | where {$_.name -eq "IBM USB Remote NDIS Network Device"}
$usbnic.disable()
}
# exit remote Powershell
exit


EDIT:
This is an optimized version of the upper script and I also added a line that disables all disconnected networkcards:

# ask for Admin password
$cred = Get-Credential -Credential hypercluster\Administrator
# run through all i nodes and disable USB NIC
for ($i = 1;$i -le 64;$i++){
Echo Node$i
Enter-PSSession -ComputerName Node$i -Credential $cred
(Get-WmiObject win32_networkadapter -ComputerName Node$i | where {$_.name -eq "IBM USB Remote NDIS Network Device"}).Disable()
# use the following lien to disable all disconnected (unpluged networkcable) NICs
# (Get-WmiObject win32_networkadapter -ComputerName Node$i -Filter 'NetConnectionStatus= 7').Disable()

 

Edit2:
To shutdown an array of servers via Powershell i used this version of the script:

# ask for Admin password
$cred = Get-Credential -Credential hypercluster\Administrator
# run through all i nodes and shutdown each one
for ($i = 1;$i -le 64;$i++){
Echo Node$i
stop-Computer –ComputerName node$i –Credential $cred –force