Hi Luc,
Good Morning,
could you suggest if following code can be converted to function and can utilized in previous script of upgrading esxi hosts using powercli.
also do yu have simple code for checking prerequisites before putting host in machinate mode.
$destpath=[Environment]::GetFolderPath("Desktop")
$cluster=read-host "specify the cluster name"
$clu=get-cluster $cluster
if($clu.DrsAutomationLevel -eq "fullyautomated")
{
Set-Cluster -Cluster $clu -Drsautomationlevel Manual
}
$hosts=Get-VMHost -Location $clu
$test_vm=read-host "specify the test vm"
Write-Host "Testing vMotion network connectivity by migrating" $TestVM "across the hosts in" $Clu.Name
$TestVM=get-vm $test_vm
$InitialVMHost = $TestVM.VMHost
for ($i=0; $i -le $hosts.GetUpperBound(0); $i++)
{
If ($i -eq $hosts.GetUpperBound(0))
{
$DestHost = $hosts[0]
}
else { $DestHost = $hosts[$i+1] }
Move-VM -VM $TestVM -Destination $DestHost -ErrorVariable +err 2>> $destpath\vMotionNetTest.txt | out-null
}
If ($InitialVMHost -ne $hosts[0]) {
Move-VM -VM $TestVM -Destination $InitialVMHost
}
$datetime = Get-Date
If ($err.count -eq 0) {Write-Output "$datetime Network test completed successfully" >> $destpath\vMotionNetTest.txt}
else
{ Write-Output "$datetime The specified test VM cannot be migrated. Please check vMotionTest.csv file for details" >> $destpath\vMotionNetTest.txt
You can check if the vMotions caused by a maintenance mode could have any potential issues by using the QueryVMotionCompatibilityEx method.
But this of course doesn't cover all the possible reasons that could prevent an ESXi node going into maintenance mode.
Something like this
$clusterName = 'MyCluster'
$cluster = Get-Cluster -Name $clusterName
$vms = Get-VM -Location $cluster
$si = Get-View ServiceInstance -Server $global:DefaultVIServer
$vChecker = Get-View -Id $si.Content.vmProvisioningChecker
$result = $vChecker.QueryVMotionCompatibilityEx($vms.ExtensionData.MoRef,$cluster.ExtensionData.Host)
foreach($entry in ($result | where{$Warning -or $error})){
$entry.Warning |
Select @{N='VM';E={(Get-View -Id $entry.VM -Property Name).Name}},
@{N='Type';E={'Warning'}},
@{N='Text';E={$_.LocalizedMessage}}
$entry.Error |
Select @{N='VM';E={(Get-View -Id $entry.VM -Property Name).Name}},
@{N='Type';E={'Error'}},
@{N='Text';E={$_.LocalizedMessage}}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You could do something like this
param(
[string]$Cluster,
[string]$VM
)
$clu = Get-Cluster $Cluster
if ($clu.DrsAutomationLevel -eq "fullyautomated") {
Set-Cluster -Cluster $clu -Drsautomationlevel Manual
}
$hosts = Get-VMHost -Location $clu
Write-Output "Testing vMotion network connectivity by migrating" $VM "across the hosts in" $Clu.Name
$TestVM = Get-VM -Name $VM
$InitialVMHost = $TestVM.VMHost
for ($i = 0; $i -le $hosts.GetUpperBound(0); $i++) {
If ($i -eq $hosts.GetUpperBound(0)) {
$DestHost = $hosts[0]
}
else { $DestHost = $hosts[$i + 1] }
Move-VM -VM $TestVM -Destination $DestHost -ErrorVariable err
}
If ($InitialVMHost -ne $hosts[0]) {
Move-VM -VM $TestVM -Destination $InitialVMHost
}
$datetime = Get-Date
If ($err.count -eq 0) {
Write-Output "$datetime Network test completed successfully"
}
else {
Write-Output "$datetime The specified test VM cannot be migrated. Please check vMotionTest.csv file for details"
}
}
$destpath = [Environment]::GetFolderPath("Desktop")
$clusterName = Read-Host "specify the cluster name"
$VMName = Read-Host "specify the test vm"
Test-vMotion -Cluster $clusterName -VM $VMName | Out-File -FilePath "$destpath\vMotionNetTest.txt"
Not sure what you mean by "prerequisites"?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
so what i meant by prerequisites is to check host will go into maintenance mode or not .
and for host to go into maintenance mode we should meet following conditions.
1:all virtual machines should move to other hosts using vmotion .
2.anything that stop vmtion like cd drive mounted with .iso file
3:evc mode
4:same datastores to cluster.
the code which we are discussing currently is to check vmotion network of one test vm looping over entire hosts of cluster and back to original host in manual drs mode.
so i thought of asking if you have any script that meet above conditions.
also this code does not work
i checked again and it worked fine.thnaks for yur help. however i made testvm ,destinationpath and cluster as mandatory parameters .
function Test-vmotionfunction
{
[cmdletbinding()]
param(
[parameter(mandatory=$true)]
[string]$TestVM,
[parameter(mandatory=$true)]
[string]$destpath,
[parameter(mandatory=$true)]
[string]$cluster
)
$clu=get-cluster $cluster
if($clu.DrsAutomationLevel -eq "fullyautomated")
{
Set-Cluster -Cluster $clu -Drsautomationlevel Manual
}
#Set-Cluster $clu -DrsMode Manual -errorvariable $err1 2>> $destpath\clustererror.txt
$hosts=Get-VMHost -Location $clu
Write-Host "Testing vMotion network connectivity by migrating" $TestVM "across the hosts in" $Clu.Name
$TVM=get-vm $TestVM
$InitialVMHost = $TVM.VMHost
for ($i=0; $i -le $hosts.GetUpperBound(0); $i++)
{
If ($i -eq $hosts.GetUpperBound(0))
{
$DestHost = $hosts[0]
}
else { $DestHost = $hosts[$i+1] }
Move-VM -VM $TVM -Destination $DestHost -ErrorVariable +err 2>> $destpath\vMotionNetTest.txt | out-null
}
If ($InitialVMHost -ne $hosts[0]) {
Move-VM -VM $TVM -Destination $InitialVMHost
}
$datetime = Get-Date
If ($err.count -eq 0) {Write-Output "$datetime Network test completed successfully" >> $destpath\vMotionNetTest.txt}
else
{ Write-Output "$datetime The specified test VM cannot be migrated. Please check vMotionTest.csv file for details" >> $destpath\vMotionNetTest.txt }
}
You can check if the vMotions caused by a maintenance mode could have any potential issues by using the QueryVMotionCompatibilityEx method.
But this of course doesn't cover all the possible reasons that could prevent an ESXi node going into maintenance mode.
Something like this
$clusterName = 'MyCluster'
$cluster = Get-Cluster -Name $clusterName
$vms = Get-VM -Location $cluster
$si = Get-View ServiceInstance -Server $global:DefaultVIServer
$vChecker = Get-View -Id $si.Content.vmProvisioningChecker
$result = $vChecker.QueryVMotionCompatibilityEx($vms.ExtensionData.MoRef,$cluster.ExtensionData.Host)
foreach($entry in ($result | where{$Warning -or $error})){
$entry.Warning |
Select @{N='VM';E={(Get-View -Id $entry.VM -Property Name).Name}},
@{N='Type';E={'Warning'}},
@{N='Text';E={$_.LocalizedMessage}}
$entry.Error |
Select @{N='VM';E={(Get-View -Id $entry.VM -Property Name).Name}},
@{N='Type';E={'Error'}},
@{N='Text';E={$_.LocalizedMessage}}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks Luc.I am going to check this code shortly.
it has worked with following warnings and errors in one of our clusters .
error is on one vm only which is powered off and can move with manual migration .
i see host does not go to maintenaince mode and stuck @ 2 percent.
I am seeing one strange things that host does not go to maintence mode.however if i change drs to manual and migrates vms manually then maintenance mode operation is successful .
not sure what can be the issue .
You probably have to change the cluster settings before actually doing the maintenance mode action.
Depending on what is causing this (you could check with keeping the cluster in Automated and then trying to set the ESxi node in maintenance mode - there should be a message if it fails).
I suspect you will need to do something like
The last 2 error messages seem to indicate that you have a couple of VMs that might use features, that are not supported on some of the other ESXi nodes.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
thanks for your response it workks but though of discussing further as
we normally put host in maintenance mode by enabling DRS mode of cluster to fully automated which was done .
however disabling HA admission control (Get-Cluster -Name MyCluster | Set-Cluster -HAAdmissionControlEnabled:$false) is a requirement for remediation not for putting host in maintenance mode
but in this case it works .
when vms can move from host a to host b by using manual migrations then it should do the same when we put either of host in maintenance mode .also there is sufficient capacity in cluster for fail over .
and there was no error message as host stuck @ 2 percent without throwing error .
There can be many causes why the Enter maintenance mode gets stuck.
I would suggest to follow KB1036167, and try to determine what the reason could be.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks ,for this particular case it works when i disable HA admission control.
That seems to indicate that the HA admission control (slot policy/cluster resource percentage/dedicated fail over node) is set up in such a way that taking away 1 node (by going into maintenance mode) violates the admission control.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
yes im on it to find out more about it .will let yu know shortly.