VMware Cloud Community
mekmiotek
Contributor
Contributor

Help with speeding up a script execution

Hi Everyone, Happy Friday.

I have this script, which works, it disables both NIC settings I need it to. 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 server, 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 sample output:

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!

Reply
0 Kudos
2 Replies
LucD
Leadership
Leadership

First of all, not sure where the PowerCLI part is in this thread.

But ok.
Instead of Invoke-Command, which needs the network connection, I would opt to create a one-off scheduled task on that targetted server.
That scheduled task can run the exact same script you are now running.
Just schedule it to run immediately.

Once the scheduled task is created your master script comes back, and you can continue to the next server.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
mekmiotek
Contributor
Contributor

My apologies about the PowerCLI.

Thats a great solution, Thanks, Ill give it a go.

Reply
0 Kudos