Category Archives: PowerCLI

Stuff related to PowerCLi

vmkping via PowerCLI/ESXCLi

When troubleshooting an ESXi host one of the most common problems is testing connectivity.  A tool that used on the console or ssh session of the ESXi host is vmkping. Use cases are testing connectivity from vmkernel to other servers in cluster like vMotion or vSAN connectivity.

This article from VMware KB article 1003728 shows use case on how to use vmkping.

There are situations where you may not have access to the root account to ssh into the box.  There is way to still troubleshooting vmkernel network connectivity by using PowerCLi and ESXCli.


$esxcli = Get-EsxCli -VMHost (Get-VMHost "testesxihost") -V2
$params = $esxcli.network.diag.ping.CreateArgs()
$params.host = '10.1.1.2'
$params.interface  = 'vmk0'
$params.size = '1472' #use 1472 for 1500 MTU or 8972 for 9000 MTU (VMware uses these values on MTU pings on ESXi)
$res = $esxcli.network.diag.ping.Invoke($params)
$res.summary

You will then get output like this:

<

Duplicated : 0
HostAddr : 10.1.1.2
PacketLost : 0
Recieved : 3
RoundtripAvg : 49
RoundtripAvgMS : 0
RoundtripMax : 61
RoundtripMaxMS : 0
RoundtripMin : 42
RoundtripMinMS : 0
Transmitted : 3

You can get further script options for ESXCLI for networking.diag.ping by:


PS C:\> $params

Name                           Value                                                                                                                                                                                                                             
----                           -----                                                                                                                                                                                                                             
host                           Unset, ([string], optional)                                                                                                                                                                                                       
wait                           Unset, ([string], optional)                                                                                                                                                                                                       
df                             Unset, ([boolean], optional)                                                                                                                                                                                                      
interval                       Unset, ([string], optional)                                                                                                                                                                                                       
ttl                            Unset, ([long], optional)                                                                                                                                                                                                         
debug                          Unset, ([boolean], optional)                                                                                                                                                                                                      
nexthop                        Unset, ([string], optional)                                                                                                                                                                                                       
count                          Unset, ([long], optional)                                                                                                                                                                                                         
netstack                       Unset, ([string], optional)                                                                                                                                                                                                       
size                           Unset, ([long], optional)                                                                                                                                                                                                         
ipv4                           Unset, ([boolean], optional)                                                                                                                                                                                                      
ipv6                           Unset, ([boolean], optional)                                                                                                                                                                                                      
interface                      Unset, ([string], optional) 

For help:

PS C:\> $esxcli.network.diag.ping.Help()



vim.EsxCLI.network.diag.ping
-----------------------------------------------------------------------------------------------------------------------
Send ICMP echo requests to network hosts.
Param
-----------------------------------------------------------------------------------------------------------------------
- count           | Specify the number of packets to send.                                                          
- debug           | VMKPing debug mode.                                                                             
- df              | Set DF bit on IPv4 packets.                                                                     
- host            | Specify the host to send packets to. This parameter is required when not executing ping in debug mode (-D)                                                                                      
- interface       | Specify the outgoing interface.                                                                 
- interval        | Set the interval for sending packets in seconds.                                                
- ipv4            | Ping with ICMPv4 echo requests.                                                                 
- ipv6            | Ping with ICMPv6 echo requests.                                                                 
- netstack        | Specify the TCP/IP netstack which the interface resides on                                      
- nexthop         | Override the system's default route selection, in dotted quad notation. (IPv4 only. Requires int
                  | erface option)                                                                                  
- size            | Set the payload size of the packets to send.                                                    
- ttl             | Set IPv4 Time To Live or IPv6 Hop Limit                                                         
- wait            | Set the timeout to wait if no responses are received in seconds.

Using PowerCLi to Export and Import OS Customization Specs

I was asked to find a solution to automate importing OS customization Specs to our Vcenters. Its a task that not difficult but better if was scripted. I found this code located at ForwardorReverse blog. Export function allows you to export the OS customization Spec to $secname.xml and the import allows you to import them. These two functions are few years old according to the Blog but still work on Vsphere 6.5 without issue.


Function Export-OSCustomizationSpec {
    param (
        [string]$specName,
        [string]$exportFile = "$specname.xml"
    )
    $csmgr = Get-View CustomizationSpecManager
 
    if ($csmgr.DoesCustomizationSpecExist($specName)) {
        $spec = $csmgr.GetCustomizationSpec($specName)
        $csmgr.CustomizationSpecItemToXml($spec) | Out-File $exportFile
    }
    else {
        throw "Spec $specName not found"
    }
}
 
Function Import-OSCustomizationSpec {
    param (
        [string]$importFile,
        [string]$specName #Use to change the spec name from that defined in the file
    )
    $specXml = Get-Content $importFile
    $csmgr = Get-View CustomizationSpecManager
    $spec = $csmgr.XmlToCustomizationSpecItem($specXml)
     # Change the name if a new one was given.
    if ($specName) {
        $spec.Info.Name = $specName
    }
     if ($csmgr.DoesCustomizationSpecExist($spec.Info.Name)) {
        throw "Spec $specName already exists."
    }
    else {
        $csmgr.CreateCustomizationSpec($spec)
    }
}

Creating vSAN cluster with over 32 hosts

So I was building out a 44 node vSAN cluster last week and I ran into an issue where 12 of the ESXi hosts had their own network Partition group different than the other 32.  I had no issues with the vSAN network, I was able to use vmkping every server so there was no communication issue with any of the hosts in the cluster  via the vSAN kernel.  In most cases vSAN Network partitioning occurs when there was issue with vSAN kernel communicating with other hosts.

After several attempts of removing the diskgroups, removing vSAN kernel, moving out of cluster and away from DVS then back I had no luck.  I knew based on VMware supported Maximum that I could create a 64 node vSAN cluster.  I was at a loss so after several hours troubleshooting and Google searching I ended up opening a SR with VMware after about hour or so troubleshooting the VMware engineer was at loss until he found an article that indicates that you must create some advanced settings on the ESXi hosts in order to see above 32 nodes and once we made those settings rebooted the hosts we had a single network partition our issue was resolved.

The KB article (2110081) shows how to perform task via esxcli via SSH logged into root but does not show how to do it via PowerCli.

$vcenter = Read-Host "Enter Vcenter connecting to"

Connect-VIserver $vcenter

$cluster = Read-host "Cluster Name"

foreach ($host in (Get-Vmhost -Location $cluster)){
Get-VMhost $host | Get-AdvancedSetting -Name "VSAN.goto11" | Set-AdvancedSetting -value 1 -confirm:$false
Get-VMhost $host | Get-AdvancedSetting -Name "Net.TcpipHeapMax" | Set-AdvancedSetting -value 1536 -confirm:$false
}

Then reboot your hosts.

That’s it!

You would think that supported maximums would occur out of box but according to VMware they did not want smaller vSAN cluster to sacrifice memory overhead that would be required for larger vSAN clusters to run efficiently.

Migrate VMs to another ESXi host in another Vcenter

Can across situation where I needed to move VM from one Vcenter to another.

The following script will do the following:
Will prompt for a Source and Target Vcenter and then ask for user credentials.
Connect to the Source and Target Vcenter. When then cycle through VMs listed from csv file (VMList.csv). (Place the csv file in same directory as the script.)

The list of VMs in the One by one and Migrate the VM to new VC. If the VMs powered on will attempt to shutdown the Server (if VMtools installed) and t

The format of the VMList.csv file needs to be in format as below.

#ensure that not connected to any vCenters
Disconnect-VIServer -Server * -Force -Confirm:$false

#enter Vcenter
Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Scope User -Confirm:$false
$vc1 = Read-Host "Enter Source Vcenter"
$vc2 = Read-Host "Enter Target Vcenter"

Write-Host "VC1 = " $vc1
Write-Host "VC2 = " $vc2

$vmlist = Import-CSV “$pwd\VMList.csv”

# Connect To Vcenter Server
$User = Read-Host -Prompt "Please enter your VC admin username"
$PW = Read-Host -Prompt "Please enter your VC admin password" -asSecureString
$PW = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($PW))

Connect-VIServer $vc1 -User $User -Password $PW -ErrorAction SilentlyContinue
Connect-VIServer $vc2 -User $User -Password $PW -ErrorAction SilentlyContinue

foreach ($item in $vmlist) {
# Map variables
$vm = $item.vmName
$destESXi = $item.targetesxihost
$destDatastore = $item.targetdatastore
$destVDSwitch = $item.targetDVSwtich
$destPortgroup= $item.targetPortGroup
$typeDisk = $item.Disktype

$vm = get-vm $vm
#Check to see if VM powered off if powered on will shutdown and check ever 5 sec til shutdown
if ($vm.powerstate -eq "PowerdOn"){
write-host "Shutting down" $vm
$vm | Shutdown-VMguest -Confirm:$false
do {
#wait 5 secs
start-Sleep -s 5
$vm = get-vm $vm
$status = $vm.powerstate
}until($status -eq "PoweredOff")
}

Write-Host "===== Connected vCenters ====="
$global:DefaultVIServers | ft -AutoSize

#Command to migrate the VM to new Host in new Vcenter
Write-Host "Moving VM" $vm
Move-VM -VM (Get-VM -Server $vc1 -Name $vm) -VMotionPriority High `
-Destination (Get-VMHost -Server $vc2 -Name $destESXi) `
-Datastore (Get-Datastore -Server $vc2 -Name $destDatastore) -DiskStorageFormat $typeDisk `
-PortGroup (Get-VDPortgroup -Server $vc2 -VDSwitch $destVDSwitch -Name $destPortgroup)

} #end of ForLoop

Disconnect-VIServer -Server * -Force -Confirm:$false

Rolling Reboot of VMware ESXi Cluster

Ran into situation where I needed to reboot a full cluster of ESXi hosts. In most cases when I need to reboot Cluster full of hosts I would utilize VUM (VMware Update Manager) to use VMware DRS to move VMs off the host, place the host in maintenance mode, reboot host and when host completes the reboot take server back out of maintenance mode, then move to next host and does for each host in cluster.

I did not need to patch the hosts this time. And Since the cluster had 32 hosts and several VMs I did not want to do this by hand. So used google and was found this script that I wanted to share. I wish I could give credit to the creator but the was on in an archived word press blog.

The script does the following:
Goes through the cluster one host at a time and puts ESXi server maintenance mode, reboots the server and the puts it back online. If VMs are running on the host DRS will need to be enabled in fully automated mode to allow VMs to VMotion off to other hosts (There should also be enough HA capacity in cluster to have 1 host taken offline at a time.

###################
## reboot-vmcluster.ps1 
## Supply the hostname/FQDN for you vcenter server and the name of the cluster you want rebooted
## Script reboots each ESXi server in the cluster one at a time
###################
##################
## Args
##################
# Check to make sure an argument was passed
if ($args.count -ne 2) {
Write-Host “Usage: reboot-vmcluster.ps1 ”
exit
}

# Set vCenter and Cluster name from Arg
$vCenterServer = $args[0]
$ClusterName = $args[1]

##################
## Connect to infrastructure
##################
Connect-VIServer -Server $vCenterServer | Out-Null

##################
## Get Server Objects from the cluster
##################
# Get VMware Server Object based on name passed as arg
$ESXiServers = @(get-cluster $ClusterName | get-vmhost)

##################
## Reboot ESXi Server Function
## Puts an ESXI server in maintenance mode, reboots the server and the puts it back online
## Requires fully automated DRS and enough HA capacity to take a host off line
##################
Function RebootESXiServer ($CurrentServer) {
# Get Server name
$ServerName = $CurrentServer.Name

# Put server in maintenance mode
Write-Host “#### Rebooting $ServerName ####”
Write-Host “Entering Maintenance Mode”
Set-VMhost $CurrentServer -State maintenance -Evacuate | Out-Null

$ServerState = (get-vmhost $ServerName).ConnectionState
if ($ServerState -ne “Maintenance”)
{
Write-Host “Server did not enter maintanenace mode. Cancelling remaining servers”
Disconnect-VIServer -Server $vCenterServer -Confirm:$False
Exit
}
Write-Host “$ServerName is in Maintenance Mode”

# Reboot blade
Write-Host “Rebooting”
Restart-VMHost $CurrentServer -confirm:$false | Out-Null

# Wait for Server to show as down
do {
sleep 15
$ServerState = (get-vmhost $ServerName).ConnectionState
}
while ($ServerState -ne “NotResponding”)
Write-Host “$ServerName is Down”

$j=1
# Wait for server to reboot
do {
sleep 120
$ServerState = (get-vmhost $ServerName).ConnectionState
Write-Host “… Waiting for reboot”
$j++
}
while ($ServerState -ne “Maintenance”)
$RebootTime=$j/2
Write-Host “$ServerName is back up. Took $RebootTime minutes”

# Exit maintenance mode
Write-Host “Exiting Maintenance mode”
Set-VMhost $CurrentServer -State Connected | Out-Null
Write-Host “#### Reboot Complete####”
Write-Host “”
}

##################
## MAIN
##################
foreach ($ESXiServer in $ESXiServers) {
RebootESXiServer ($ESXiServer)
}

##################
## Cleanup
##################
# Close vCenter connection
Disconnect-VIServer -Server $vCenterServer -Confirm:$False

Example of Script Output:

>.\reboot-vmcluster.ps1 vcenter.domain.com demo-cluster
#### Rebooting esxi06.domain.com ####
Entering Maintenance Mode
Rebooting
esxi06.domain.com is Down
Waiting for Reboot ...
Waiting for Reboot ...
Waiting for Reboot ...
esxi06.domain.com is back up
Exiting Maintenance mode
#### Reboot Complete####

#### Rebooting esxi05.domain.com ####
Entering Maintenance Mode
Rebooting
esxi05.domain.com is Down
Waiting for Reboot ...
Waiting for Reboot ...
Waiting for Reboot ...
Waiting for Reboot ...
esxi05.domain.com is back up
Exiting Maintenance mode
#### Reboot Complete####