VMware Cloud Community
ugorur2000
Contributor
Contributor

Bulk IP Address change with PowerCLI

Hi All,

I want to change the IP addresses of some VMs using the script that is left behind. However, I get a result like in the picture. Can you help me?

CSV file content is as follows "ServerName, Username, Password, NewPortgroup, OrigIP, NewIP, NewMask, NewGateway"

. "C:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1"

$credential = Import-Clixml -Path C:\Temp\Ca-cwestwater.cred

Connect-VIServer vcenter.domain.com -Credential $credential

# Processing CSV. You need the following columns in the CSV file: ServerName, Username, Password, NewPortgroup,origIP,newIP,newMask,newGateway

$csv = @()

$csv = Import-CSV "C:\temp\Inventory.csv" | Where {$_.ServerName}

$csv | % {

    $_.ServerName = $_.ServerName.Trim()

    $_.Username = $_.Username.Trim()

    $_.Password = $_.Password.Trim()

    $_.NewPortgroup = $_.NewPortgroup.Trim()

    $_.origIP = $_.origIP.Trim()

    $_.newIP = $_.newIP.Trim()

    $_.newMask = $_.newMask.Trim()

    $_.newGateway = $_.newGateway.Trim()

    }

# Updating VMs' Portgroups and IP Addresses

foreach ($vm in $csv){

    #Check if the Portgroup exists. Get the correct PG in case there is more than one PG with identical name

$PG= get-virtualswitch -VM $vm.ServerName | get-virtualportgroup | ?{$_.Name -eq $vm.NewPortgroup}

if(!$PG){

Write-Host -Fore:Red "The Portgroup" $vm.NewPortgroup "was not found. Proceeding to the next VM"

Continue

}

#Change Portgroup

Write-Host -Fore:Yellow "Connecting" $vm.ServerName "to Portgroup" $vm.NewPortgroup

    $nic = (get-vm -name $vm.ServerName) | get-NetworkAdapter

    Set-NetworkAdapter -NetworkAdapter $nic -Portgroup $PG -Confirm:$false

# Changing IP Address

# Check if VM is powered on and if it has the VMtools running

    if($vm.PowerState -eq 'PoweredOff' -or $vm.ExtensionData.Guest.ToolsRunningStatus -eq 'guestToolsNotRunning') {

Write-Host -Fore:Red $VM ' is powered off and the IP address of the VM cannot be updated'

} else {

        # Check if Guest OS is Windows 2012

        if ((Get-vm -name $vm.Servername).Guest.OSFullName -eq "Microsoft Windows Server 2012 (64-bit)"){

        

            # Get the Interface Name

            $script = '(Get-NetIPAddress | where-object {$_.IPAddress -match "' + $vm.origIp + '" -and $_.AddressFamily -eq "IPv4"}).InterfaceAlias'

            $InterfaceName = invoke-vmscript -ScriptText $script -ScriptType PowerShell -VM $vm.ServerName -GuestUser $vm.Username -GuestPassword $vm.Password

            $InterfaceName = $InterfaceName -replace "`t|`n|`r",""

            if(!$InterfaceName) {

Write-Host -Fore:Red "The Interface with IP Address" $vm.origIP " was not found in VM" $vm.ServerName "`n"

Continue

}

            #Change the IP Address

            Write-host -Fore:Yellow "`nChanging IP Address of" $vm.ServerName "interface" $InterfaceName "from" $vm.origIp "to" $vm.newIp

            $changingIp = '%WINDIR%\system32\netsh.exe interface ipv4 set address name="' + $InterfaceName + '" source=static address=' + $vm.newIP + ' mask=' + $vm.newMask + ' gateway=' + $vm.newGateway + ' gwmetric=1 store=persistent'

            $setIp = invoke-vmscript -ScriptText $changingIp -ScriptType bat -VM $vm.ServerName -GuestUser $vm.Username -GuestPassword $vm.Password           

            }

       

        # For all other Windows Guest OS types

        else {

            # Get the Interface Name

            $InterfaceName = Get-VMGuestNetworkInterface -VM $vm.ServerName -GuestUser $vm.Username -GuestPassword $vm.Password | where {$_.IP -match $vm.OrigIP}

if(!$InterfaceName) {

Write-Host -Fore:Red "The Interface with IP Address" $vm.origIP " was not found in VM" $vm.ServerName "`n"

Continue

}

            #Change the IP Address

            Write-host -Fore:Yellow "`nChanging IP Address of" $vm.ServerName "interface" $InterfaceName "from" $vm.origIp "to" $vm.newIp

            Set-VMGuestNetworkInterface -VMGuestNetworkInterface $InterfaceName -GuestUser $vm.Username -GuestPassword $vm.Password -Ip $vm.NewIP -Netmask $vm.NewMask -Gateway $vm.NewGateway

            } 

   

    # Register the new IP Address with DNS

    Write-Host -Fore:Yellow "Registering with DNS"

    $registeringDNS = '%WINDIR%\System32\ipconfig /registerdns'

    $segDNS = invoke-vmscript -ScriptText $registeringDNS -ScriptType bat -VM $vm.ServerName -GuestUser $vm.Username -GuestPassword $vm.Password

    Write-Host -Fore:Green $vm.ServerName "has been sucessfully updated `n"

}

}

pastedImage_4.png

Tags (3)
Reply
0 Kudos
3 Replies
LucD
Leadership
Leadership

Which PowerCLI version are you using?

The Get-VMGuestNetworkInterface was removed in PowerCLI 10.0 (Feb 2018).

The reason for removing those VMGuestNetworkInterface cmdlets was that it needed a dedicated script for each Guest OS to interpret the results.


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

Reply
0 Kudos
ugorur2000
Contributor
Contributor

Hi LucD​\

PowerCLI Version

----------------

   VMware PowerCLI 6.5 Release 1 build 4624819

---------------

Component Versions

---------------

   VMware Cis Core PowerCLI Component 6.5 build 4624453

   VMware VimAutomation Core PowerCLI Component 6.5 build 4624450

   VMWare ImageBuilder PowerCLI Component 6.5 build 4561891

   VMWare AutoDeploy PowerCLI Component 6.5 build 4561891

   VMware Vds PowerCLI Component 6.5 build 4624695

   VMware Cloud PowerCLI Component 6.5 build 4624821

   VMware HA PowerCLI Component 6.0 build 4525225

   VMware HorizonView PowerCLI Component 7.0.2 build 4596620

   VMware Licensing PowerCLI Component 6.5 build 4624822

   VMware PCloud PowerCLI Component 6.5 build 4624825

   VMware Storage PowerCLI Component 6.5 build 4624820

   VMware vROps PowerCLI Component 6.5 build 4624824

   VMware vSphere Update Manager PowerCLI 6.5 build 4540462

How can i hang this problem? What can I use instead of the `Get-VMGuestNetworkInterface` command? Or how can I make changes in the script?

Reply
0 Kudos
LucD
Leadership
Leadership

You have a rather old PowerCLI version.
I would strongly suggest upgrading.

See Updating PowerCLI through the PowerShell Gallery for instructions.

I suggest using the Guest OS native commands to change the IP address.

You can run those commands through Invoke-VMScript inside the Guest OS.

See for example Alan's POWERCLI: CHANGING A VM IP ADDRESS WITH INVOKE-VMSCRIPT


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

Reply
0 Kudos