VMware Cloud Community
dazzpowder
Enthusiast
Enthusiast
Jump to solution

Move multiple vm on multiple portgroups from dvs to standard switch

Hi all,

Trying to put together a script, I took the following from Virtu-Al and this works but for one set of variables, For the life of me I cant get this to work for more than just one portgroup

$OldNetwork = "PG1 192.168.0"
$NewNetwork = "PG2 10.1.1"

Get-VM |Get-NetworkAdapter |Where {$_.NetworkName -eq $OldNetwork} |Set-NetworkAdapter -NetworkName $NewNetwork -Confirm:$false

what I would like is something like

$OldNetwork1 = "PG1 192.168.0"
$NewNetwork1= "PG2 10.1.1"

$OldNetwork2 = "PG10"
$NewNetwork2= "PG2 "

so if the first "where" statement wouldn't match it would go on and try to match another old portgroup from the list of variables.  Any help much appreciated tired of seeing red

thanks

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try like this

$OldNetwork1 = "PG1 192.168.0"

$NewNetwork1= "PG2 10.1.1"

$OldNetwork2 = "PG10"

$NewNetwork2= "PG2 "

Get-VM |Get-NetworkAdapter | %{

    $_ | Where {$_.NetworkName -eq $OldNetwork1} |Set-NetworkAdapter -NetworkName $NewNetwork1 -Confirm:$false

    $_ | Where {$_.NetworkName -eq $OldNetwork2} |Set-NetworkAdapter -NetworkName $NewNetwork2 -Confirm:$false

}


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

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

Try like this

$OldNetwork1 = "PG1 192.168.0"

$NewNetwork1= "PG2 10.1.1"

$OldNetwork2 = "PG10"

$NewNetwork2= "PG2 "

Get-VM |Get-NetworkAdapter | %{

    $_ | Where {$_.NetworkName -eq $OldNetwork1} |Set-NetworkAdapter -NetworkName $NewNetwork1 -Confirm:$false

    $_ | Where {$_.NetworkName -eq $OldNetwork2} |Set-NetworkAdapter -NetworkName $NewNetwork2 -Confirm:$false

}


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

0 Kudos
dazzpowder
Enthusiast
Enthusiast
Jump to solution

Thanks LucD, I couldn't get this thing to iterate through.  Thanks again! as simple as it is, it saves me a whole load of man hours. although you can migrate an entire portgroup via vm networking not so simple when selecting individual guests a couple of hundred at a time.

0 Kudos