# Help with speeding up a script execution

**URL:** <https://forums.powershell.org/t/help-with-speeding-up-a-script-execution/21854>\
**Category:** PowerShell Help\
**Created:** [March 10, 2023, 4:08pm UTC](https://forums.powershell.org/t/help-with-speeding-up-a-script-execution/21854 "2023-03-10T16:08:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![matt1974](https://avatars.discourse-cdn.com/v4/letter/m/dbc845/32.png) [@matt1974](https://forums.powershell.org/u/matt1974)\
**Post date:** [March 10, 2023, 4:08pm UTC](https://forums.powershell.org/t/help-with-speeding-up-a-script-execution/21854/1 "2023-03-10T16:08:50Z")

</div>

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?

```auto
# 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:

```auto
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!

---

<div class="post-metadata">

**Author:** ![neemobeer](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/neemobeer/32/4087_2.png) [@neemobeer](https://forums.powershell.org/u/neemobeer)\
**Post date:** [March 10, 2023, 4:59pm UTC](https://forums.powershell.org/t/help-with-speeding-up-a-script-execution/21854/2 "2023-03-10T16:59:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![matt1974](https://avatars.discourse-cdn.com/v4/letter/m/dbc845/32.png) [@matt1974](https://forums.powershell.org/u/matt1974)\
**Post date:** [March 10, 2023, 5:06pm UTC](https://forums.powershell.org/t/help-with-speeding-up-a-script-execution/21854/3 "2023-03-10T17:06:24Z")

</div>

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:

```auto
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,

---

<div class="post-metadata">

**Author:** ![matt1974](https://avatars.discourse-cdn.com/v4/letter/m/dbc845/32.png) [@matt1974](https://forums.powershell.org/u/matt1974)\
**Post date:** [March 10, 2023, 6:27pm UTC](https://forums.powershell.org/t/help-with-speeding-up-a-script-execution/21854/4 "2023-03-10T18:27:25Z")

</div>

Disconnecting from the PSSession helps.

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

Thanks.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:15pm UTC](https://forums.powershell.org/t/help-with-speeding-up-a-script-execution/21854/5 "2024-05-16T20:15:06Z")

</div>

Closed on May 16, 2024, 8:15pm UTC


