I am formulating a script which will shut down and reconfigure a bunch of VMs defined in an imported csv file. I am at the point of attempting to gracefully shutdown powered on VMs (which have VMTools installed) and wait for the powerstate to change to "PoweredOff". I am using the Do/Until statements to wait for the powerstate to change. However, even although I can visibly see the VM shutdown in vSphere, the script does not appear to be re-registering the powerstate change and thus stuck in an infinite loop.
Please see the script below. I have highlighted in bold the section of the script that appears to be looping constantly. I have also attached a screenshot of the script running. Any help with this would be greatly appreciated.
function VMCustomAttributes
{
# Connect to the vCenter
Write-Host "Connecting to vCenter $($vcenter_srv)" -ForegroundColor Yellow
Connect-VIServer $vcenter_srv
#Read the input file
Write-Host "Reading Input File" -ForegroundColor Yellow
$Input = Import-CSV -Path $InputFile
#Running Main CMDs
Write-Host "Running Main CMDs" -ForegroundColor Magenta
foreach ($row in $Input) {
# Setting Variables
Write-Host "Setting VM specific variables" -foregroundcolor Green
$VMGet = get-vm $row.name
$VMName = $row.name
$newCPU = $row.newcpu
$newCores = $row.newcores
$newMem = $row.newmem
$spec = New-Object -Type VMware.Vim.VirtualMachineConfigSpec -Property @{"NumCoresPerSocket" = $newCores}
$vmview = $VMGet | Get-View
$toolsStatus = $vmview.Guest.ToolsStatus
if ($VMGet.Powerstate -eq "PoweredOff"){
Write-host "VM: " $VMGet "is powered off, making changes as required and leaving VM in a powered off state"
#Running reconfig command
$VMGet | Set-VM –MemoryGB $newMEM –NumCpu $newCPU –Confirm:$False
sleep -s 5
(Get-VM $vmtest).ExtensionData.ReconfigVM_Task($spec)
}
if ($VMGet.Powerstate -eq "PoweredOn" -And $toolsStatus -eq "toolsNotInstalled"){
#VMTools Not Running
Write-Host "VM " $VMGet "Does not have VMTools running and subsequently can't perform a gracefull shutdwon"
}
if ($VMGet.Powerstate -eq "PoweredOn" -And $toolsStatus -ne "toolsNotInstalled"){
Write-host "Proceeding with task to shutdown, reconfig and start backup up" $VMGet
#Shutting down Guest
Write-host "Shutting Down Guest on" $VMGet
$VMGet | Shutdown-VMGuest -Confirm:$False
#Waiting for Shutdownn to complete
Writelog "Waiting for Shutdown to complete"
do {
Write-Host "Waiting on shutdown for VM: " $VMGet
#Check the power status
$Status = $VMGet.Powerstate
Write-Host "PowerStatus is:" $Status
sleep 5
}until($VMGet.Powerstate -eq "Poweredoff")
Write-Host "Success"
}
}
# Disconnect from vCenter
Writelog "Disconnect from vCenter: " $VCentername -ForegroundColor Green
Disconnect-VIServer -Confirm:$false
Pause
}
#==============================================================================
function Main
{
# Turn off error reporting
$erroractionpreference = "SilentlyContinue"
# Set the execution policy
# Set-executionpolicy RemoteSigned
Write-Host "Setting Execution Policy" -ForegroundColor Green
Set-executionpolicy UnRestricted
# Add the PowerCLI snapin
Write-Host "Adding PowerCLI Snapin" -ForegroundColor Green
Add-PsSnapin vmware.vimautomation.core
# Ignore certificate warning
#Set-PowerCLIConfiguration -InvalidCertificateAction prompt
# Setting Variables
Write-Host "Setting Variables"
$Inputfile = Read-Host "CSV input file path"
$vcenter_srv = Read-Host "What is the Virtual Center name?"
# Create the reports
Write-Host "Moving onto Next Function" -ForegroundColor Green
VMCustomAttributes
}
#==============================================================================
#BEGIN
Main
#END
You should refresh the content of the $vmGet variable, that will not be done automatically.
In other words, inside you Do-While loop, you need to do a Get-VM befoer you test the powerstate
$VMGet = Get-VM -Name $VMGet.Name
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You should refresh the content of the $vmGet variable, that will not be done automatically.
In other words, inside you Do-While loop, you need to do a Get-VM befoer you test the powerstate
$VMGet = Get-VM -Name $VMGet.Name
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You my friend are a legend. Something so simple.....and it should have been obvious. Thanks again and I appreciate the quick response.