Help with speeding up a script execution

Hi Everyone, Happy Friday.

I have a need to disable 2 NIC settings on a large number of servers.

I have this script, which works, it disables both settings. The problem I have is, after the 2nd setting gets disabled, you lose connection to the server for a few seconds, and the script just hangs for a long period of time. Is there anyway to keep the script moving along to the next $computer, after the 2 settings have been set?

# Get the list of servers from the text file
$servers = Get-Content "C:\temp\servers.txt"
# Loop through each server
foreach ($server in $servers) {
    Write-Host "Disabling NIC power management on $server"
    # Disable NIC power management using the Set-NetAdapterPowerManagement cmdlet
    Invoke-Command -ComputerName $server -ScriptBlock {
        $net = Get-NetAdapter
        $NICs = Get-WmiObject Win32_NetworkAdapter -filter "AdapterTypeID = '0' AND PhysicalAdapter = 'true' AND NOT Description LIKE '%wireless%' AND NOT Description LIKE '%virtual%' AND NOT Description LIKE '%WiFi%' AND NOT Description LIKE '%Bluetooth%'"
        Foreach ($NIC in $NICs) {
            $powerMgmt = Get-WmiObject MSPower_DeviceEnable -Namespace root\wmi | Where-Object { $_.InstanceName -match [regex]::Escape($nic.PNPDeviceID) }
            If ($powerMgmt.Enable -eq $True) {
                $powerMgmt.Enable = $False
                $powerMgmt.psbase.Put()
            }
            Set-NetAdapterAdvancedProperty -Name $net.Name -DisplayName "Interrupt Moderation" -DisplayValue "Disabled"
        }
    }
}

Here is the output I get:

Disabling NIC power management on server
WARNING: Attempting to reconnect to server...been interrupted. Attempting to reconnect for up to 4 minutes...
                                                                                                                        WARNING: TheInvoke-Command: C:\Temp\ITScript\Windows\Modify-Nic.ps1:12
Line |
  12 |      Invoke-Command -ComputerName $server -ScriptBlock {
     |      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Object reference not set to an instance of an object.

PSComputerName : server
RunspaceId     : ce668f8f-0cb5-44bf-a12a-60bb1b3d10a6
Path           : \\localhost\root\wmi:MSPower_DeviceEnable.InstanceName="PCI\\VEN_15AD&DEV_07B0&SUBSYS_07B015AD&REV_01\\005056FFFFA
                 6C05A00_0"
RelativePath   : MSPower_DeviceEnable.InstanceName="PCI\\VEN_15AD&DEV_07B0&SUBSYS_07B015AD&REV_01\\005056FFFFA6C05A00_0"
Server         : localhost
NamespacePath  : root\wmi
ClassName      : MSPower_DeviceEnable
IsClass        : False
IsInstance     : True
IsSingleton    : False

Disabling NIC power management on server
WARNING: The network connection to server has been interrupted. Attempting to reconnect for up to 4 minutes...
WARNING: Attempting to reconnect to server...
WARNING: The network connection to server has been restored.

Thanks!

I would look at using jobs or runspaces, this allows for multiple executions in parallel.

Actually, I need to do these serially. The team that owns the servers asked that we do 1 at a time, because of the network blip. They only want 1 offline at a time.

This is the line that causes the network interruption:

Set-NetAdapterAdvancedProperty -Name $net.Name -DisplayName "Interrupt Moderation" -DisplayValue "Disabled"

If the script can set 1st setting, then just set this and drop the connection and move on, that would be ideal.

Thanks,

Disconnecting from the PSSession helps.

I added a disconnect statement after the 2nd nic command, and it improves things greatly.

Thanks.