VMware Cloud Community
TylerDurden77
Enthusiast
Enthusiast
Jump to solution

Change portgroup of VMs with specific IP adress

Hi,

I'm moving ESX servers from one vCenter to another.

VMs that have been migrated to my new vCenter/DVswitch don't have a correct PG configured.

My setup:

PG10 = 192.168.10.0/24

PG11 = 192.168.11.0/24

PG12 = 192.168.12.0/24

Etc...

Found this script somewhere which solves my problem per vlan

$sourceVMhost = "esx03"

$VMnameFilter = "*"

$IPFilter = "192.168.10.*"

$logfolder = "c:\MigrationLogs"

$getvms = Get-VMhost $sourceVMhost | Get-VM | where name -Like $VMnameFilter | Select Name, @{ L = "IP Address"; E = { ($_.guest.IPAddress[0]) } } | where "IP Address" -Like $IPFilter | select -ExpandProperty name

foreach ($vm in $getvms)

{

$NEWDPG = Get-VDSwitch -Name dvSwitch-prod | Get-VDPortgroup -Name 'PG10'

get-vm $vm | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName $NEWDPG -Confirm:$false

If (!$error)

{ Write-Output $vm, $error[0] | out-file -append -filepath "$logfolder\success.log" }

Else

{ Write-Output $vm, $error[0] | out-file -append -filepath "$logfolder\failed.log" }

Above script works fine but I have to run it many times and change the $IPFilter and $NEWDPG.   (My VMs are spread out on vlan10-20)

Possible to use some kind of If statement to accomplish this task in one single script?

Regards

Tyler

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Looks like there are some blank lines inserted in the code I gave.

That might have happened during the copy/paste on your side.

Try with the attached file, that avoids these copy/paste issues.


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

View solution in original post

Reply
0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

You could try something like this

Remove the WhatIf switch on the Set-NetworkAdapter cmdlet when you are sure everything is working as expected.

$sourceVMhost = "esx03"

$ipTabText = @'

Range,PGName

192.168.10,PG10

192.168.11,PG11

192.168.12,PG12

192.168.13,PG13

192.168.14,PG14

192.168.15,PG15

192.168.16,PG16

192.168.17,PG17

192.168.18,PG18

192.168.19,PG19

192.168.20,PG20

'@

$ipTab = @{}

$ipTabText | ConvertFrom-Csv | %{

    $ipTab.Add($_.Range,$_.PGName)

}

$logfolder = "c:\MigrationLogs"

Get-VMHost -Name $sourceVMhost | Get-VM |

Group-Object -Property {$_.Guest.IPAddress[0].Split('.')[0..2] -join '.'} | %{

    $subnet = $_.Name

    $pg = Get-VDSwitch -Name dvSwitch-prod | Get-VDPortgroup -Name $ipTab[$subnet]

    $_.Group | %{

        Get-NetworkAdapter -VM $_ | Set-NetworkAdapter -NetworkName $pg -Confirm:$false -WhatIf

   

        If (!$error)

        { Write-Output $vm, $error[0] | out-file -append -filepath "$logfolder\success.log" }

        Else

        { Write-Output $vm, $error[0] | out-file -append -filepath "$logfolder\failed.log" }

    }

}


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

TylerDurden77
Enthusiast
Enthusiast
Jump to solution

Hi LucD,

Thanks for your quick response.

Testing in my "test" environment where I have some other PG names.

When I run :

$sourceVMhost = "esx08"

$ipTabText = @'

Range,PGName

172.30.155,net-155

172.30.174,net-175

'@

$ipTab = @{}

$ipTabText | ConvertFrom-Csv | %{

    $ipTab.Add($_.Range,$_.PGName)

}

I get this error :

Exception calling "Add" with "2" argument(s): "Key cannot be null.

Parameter name: key"

At line:19 char:5

+     $ipTab.Add($_.Range,$_.PGName)

+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

    + FullyQualifiedErrorId : ArgumentNullException

Exception calling "Add" with "2" argument(s): "Key cannot be null.

Parameter name: key"

At line:19 char:5

+     $ipTab.Add($_.Range,$_.PGName)

+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

    + FullyQualifiedErrorId : ArgumentNullException

Exception calling "Add" with "2" argument(s): "Key cannot be null.

Parameter name: key"

At line:19 char:5

+     $ipTab.Add($_.Range,$_.PGName)

+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

    + FullyQualifiedErrorId : ArgumentNullException

Any advice?

Regards

Tyler

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Looks like there are some blank lines inserted in the code I gave.

That might have happened during the copy/paste on your side.

Try with the attached file, that avoids these copy/paste issues.


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

Reply
0 Kudos
TylerDurden77
Enthusiast
Enthusiast
Jump to solution

Thank you LucD

Works like a charm Smiley Happy

Reply
0 Kudos