VMware Cloud Community
Joggid_1981
Contributor
Contributor
Jump to solution

Set-NetworkAdapter for multiple VM's

Hi,

I need little help to make below script faster. I have csv file "VM_s.csv" like this

   

Name
David-DR-01
David-DR-02
David-DR-03
David-DR-04
David-DR-05

and when I use function "foreach", so this script use VM one by one. Is it possible use a faster solution?

$vmNames = Import-Csv -Path .\VM_s.csv -UseCulture | select -ExpandProperty Name

Get-VM -Name $vmNames

foreach ($vm in $vmNames)

Get-VM $vm | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName "My_PortGroup_Name" -StartConnected:$true -Confirm:$false

}

David

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You can use the RunAsync switch on the Set-NetworkAdapter cmdlet.
That way the action will run in the background and your script will immediately continue with the next line.

The problem might be that if you have dependencies on that vNIC to be reconfigured for later in your script, that you might have to verify that all these background tasks are actually finished.

You can use the Get-Task cmdlet for that (in a loop)


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

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

You can use the RunAsync switch on the Set-NetworkAdapter cmdlet.
That way the action will run in the background and your script will immediately continue with the next line.

The problem might be that if you have dependencies on that vNIC to be reconfigured for later in your script, that you might have to verify that all these background tasks are actually finished.

You can use the Get-Task cmdlet for that (in a loop)


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

0 Kudos
Joggid_1981
Contributor
Contributor
Jump to solution

It works. Smiley Happy Thank you very much.

David

0 Kudos