profile for Gajendra D Ambi on Stack Exchange, a network of free, community-driven Q&A sites

Thursday, July 27, 2017

Changing the vmkernel/management ip address (and subnet mask too if you want) using powercli


powercli just rox. we at dell emc today had an issue where we had
3 UCS domains and client wanted all the 110+ blades to be distributed across these 3 domains. IP addresses were not in range. We use a custom linux virtual machine which install esxi on UCS with proper vmkernel networking. However that is not designed for such scenarios. So the ip addresses how this esxi deployer assigned was not at all matching what the customer wanted. We thought by the time our storage guys assign boot luns then i could figure out a script to change the ip addresses of all these 110+ blades. I even took my buddy gowtham's data center without asking him and played around with it. It worked for the 8 blades that we had. I used plink to deliver the esxcli command as mentioned by duncan epping . Needless to say what worked on a small solution didnt work on this one for some reason. I was using a csv file to input the
old ip
new ip
username
password
vmkernel (it was vmk0)
but it failed and we as a team shared all 15 blades per head and changed the ip addresses manually. We did not have much time to play. I just reached home and so far i have this.
connect to the esxi host first

PS C:\WINDOWS\system32> Connect-VIServer 192.168.248.136 -User root -Password 'vmware1'
WARNING: There were one or more problems with the server certificate for the server 192.168.248.136:443:

* The X509 chain could not be built up to the root certificate.

* The certificate's CN name does not match the passed value.

Certificate: [Subject]
  OID.1.2.840.113549.1.9.2="1477497341,564d7761726520496e632e", CN=localhost.localdomain, E=ssl-certificates@vm
ware.com, OU=VMware ESX Server Default Certificate, O="VMware, Inc", L=Palo Alto, S=California, C=US

[Issuer]
  O=VMware Installer

[Serial Number]
  00A4602D4BBDF6

[Not Before]
  26-10-2016 9.25.42 PM

[Not After]
  26-04-2028 9.25.42 PM

[Thumbprint]
  360BDA508071AF7832778CA9C038195A14468A4F



The server certificate is not valid.
   
WARNING: THE DEFAULT BEHAVIOR UPON INVALID SERVER CERTIFICATE WILL CHANGE IN A FUTURE RELEASE. To ensure script
s are not affected by the change, use Set-PowerCLIConfiguration to set a value for the InvalidCertificateAction
 option.
      

Name                           Port  User                         
----                           ----  ----                         
192.168.248.136                443   root

Let us see to whom we are connected with
------------------
PS C:\WINDOWS\system32> Get-VMHost
 
Name                 ConnectionState PowerState NumCpu CpuUsageMhz CpuTotalMhz   MemoryUsageGB   MemoryTotalGB
----                 --------------- ---------- ------ ----------- -----------   -------------   -------------
192.168.248.136      Connected       PoweredOn       4          69       16000           1.418          11.718 
 
Now we should find out who this is
 
 PS C:\WINDOWS\system32> (Get-VMHost).NetworkInfo
 
HostName     DomainName   DnsFro ConsoleGateway  ConsoleGatewayD DnsAddress          
                          mDhcp                  evice                               
--------     ----------   ------ --------------  --------------- ----------          
host1        ambi.com     False                                  {192.168.248.136} 
 
 
Now change the ip address  
 
 
 PS C:\WINDOWS\system32> (Get-VMHost | Get-EsxCli).network.ip.interface.ipv4.set('vmk0','192.168.248.135', '255.255.255.0', $null, 'static')
true 
 
That true is really encouraging. I connected to the new changed ip address now.
  
 PS C:\WINDOWS\system32> Connect-VIServer 192.168.248.135 -User root -Password 'vmware1'
 
Name                           Port  User                          
----                           ----  ----                          
192.168.248.135                443   root 
 

I checked the name of this new ip.
 
 
PS C:\WINDOWS\system32> (Get-VMHost).NetworkInfo
 
HostName     DomainName   DnsFro ConsoleGateway  ConsoleGatewayD DnsAddress          
                          mDhcp                  evice                               
--------     ----------   ------ --------------  --------------- ----------          
host1        ambi.com     False                                  {192.168.248.136}   
 
 
 
PS C:\WINDOWS\system32>
------------------

Yes, you can change any vmkernel ip address like this but i am trying to figure out an esxcli -v2 version of it. Nay sayers listen to this. If you do not know how to do it then it doesn't mean it can't be done.
Now here is the esxcli -v2 version.


PS C:\WINDOWS\system32> $esxcli = Get-VMHost | Get-EsxCli -v2
$esxcliset = $esxcli.network.ip.interface.ipv4.set
$args = $esxcliset.CreateArgs()
$args.interfacename = 'vmk0'
$args.type = 'static'
$args.ipv4 = '192.168.248.136'
$args.netmask = '255.255.255.0'
$esxcliset.Invoke($args)



true

PS C:\WINDOWS\system32> disconnect-viserver * -confirm:$false
Connect-VIServer 192.168.248.136 -User root -Password 'vmware1'

WARNING: There were one or more problems with the server certificate for the server 192.168.248.136:443:

* The X509 chain could not be built up to the root certificate.

* The certificate's CN name does not match the passed value.

Certificate: [Subject]
  OID.1.2.840.113549.1.9.2="1477497341,564d7761726520496e632e", CN=localhost.localdomain, E=ssl-certificates@vmware.com, OU=VMware ESX Server Defaul
t Certificate, O="VMware, Inc", L=Palo Alto, S=California, C=US

[Issuer]
  O=VMware Installer

[Serial Number]
  00A4602D4BBDF6

[Not Before]
  26-10-2016 9.25.42 PM

[Not After]
  26-04-2028 9.25.42 PM

[Thumbprint]
  360BDA508071AF7832778CA9C038195A14468A4F



The server certificate is not valid.
    
WARNING: THE DEFAULT BEHAVIOR UPON INVALID SERVER CERTIFICATE WILL CHANGE IN A FUTURE RELEASE. To ensure scripts are not affected by the change, use
 Set-PowerCLIConfiguration to set a value for the InvalidCertificateAction option.
       

Name                           Port  User                          
----                           ----  ----                          
192.168.248.136                443   root                          



PS C:\WINDOWS\system32> $esxcli = Get-VMHost | Get-EsxCli -v2
$esxcliset = $esxcli.network.ip.interface.ipv4.set
$args = $esxcliset.CreateArgs()
$args.interfacename = 'vmk0'
$args.type = 'static'
$args.ipv4 = '192.168.248.135'
$args.netmask = '255.255.255.0'
$esxcliset.Invoke($args)



true

PS C:\WINDOWS\system32> disconnect-viserver * -confirm:$false
Connect-VIServer 192.168.248.135 -User root -Password 'vmware1'


Name                           Port  User                          
----                           ----  ----                          
192.168.248.135                443   root                          



PS C:\WINDOWS\system32> 
update : 2017nov17


#Start of function
function IpChanger2
{
<#
.SYNOPSIS
    Changes the ip address and gateway of the given vmkernel
.DESCRIPTION
    Imports esxcli in powercli to update the vmkernel's networking details
.NOTES
    File Name      : IpChanger.ps1
    Author         : gajendra d ambi
    updated        : July 2017
    Prerequisite   : PowerShell v4+, powercli 6+ over win7 and upper.
    Copyright      - None
.LINK
    Script posted over: github.com/MrAmbig
#>
#Start of Script
$OldIp = Read-Host 'original ip?'
$NewIp = Read-Host 'new ip?'
$user = '' # username to log into the host
$pass = '' # password for the esxi to login
$vmk = '' # vmk to change the the ip for. ex: vmvk0
$subnetMask = '' # subnetmask

    Connect-VIServer $OldIp -User $user -Password $pass
    $esxcliset = $esxcli.network.ip.interface.ipv4.set
    $args = $esxcliset.CreateArgs()
    $args.interfacename = "$vmk"
    $args.type = 'static'
    $args.ipv4 = $NewIp
    $args.netmask = $subnetMask
    $esxcliset.Invoke($args)

    Start-Sleep -Seconds $delay
    Disconnect-VIServer * -Confirm:$false -ErrorAction SilentlyContinue
} #End of function
do { IpChanger2 } while ($True)

So this will set you up for multiple hosts