Hi
Hope someone can help me with this.
The Restart-VMHost doesn't work in my script. The output error is saying vCenter connection is required by the current operation. The connection does not exist, possibly because it was actively closed or timed out.
What I am trying to achieve in the following script is to shutdown all VMs and then reboot the hosts. Because the vCenter is one of the VM that needs to be shutdown. So the script connects to each hosts instead of vCenter
Connect-VIServer -server 'vcenter01' #to get a list of hosts before shutting down the vCenter
$hostlist = Get-VMHost
Disconnect-VIServer * -confirm:$false
foreach ($string in $hostlist) {
Connect-VIserver -server $string -user 'root' -password 'abcd'
}
$vmlist = get-VM
Foreach ($vm in $vmlist){
write-host "Shutting down $vm" -BackgroundColor blue -ForegroundColor White
$vm | Shutdown-VMGuest -Confirm:$false
}
sleep 10
foreach ($path in $hostlist) {
Restart-VMHost $path -force -Confirm:$false
write-host "Restarting $path" -BackgroundColor blue -ForegroundColor White
}
I suspect the message might be caused by the parameter you pass to the Restart-VMHost cmdlet.
You use the VMHost object retrieved by Get-VMHost while connected to the vCenter.
Can you try that part of the code as follows
....
foreach ($path in $hostlist) {
Restart-VMHost $path.Name -force -Confirm:$false
write-host "Restarting $path" -BackgroundColor blue -ForegroundColor White
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I suspect the message might be caused by the parameter you pass to the Restart-VMHost cmdlet.
You use the VMHost object retrieved by Get-VMHost while connected to the vCenter.
Can you try that part of the code as follows
....
foreach ($path in $hostlist) {
Restart-VMHost $path.Name -force -Confirm:$false
write-host "Restarting $path" -BackgroundColor blue -ForegroundColor White
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
It works!!
Thanks
Vickie
Ok, that actually does't work 100%. The server was successfully rebooted but I am getting other errors. >.<
All I am trying to do is to shutdown all VMs(include vCenter), edit host firewall configuration on each host and then reboot all hosts, bring up VMs.
$SleepTimeOut = 300
Connect-VIServer -server 'vCenter' -username 'vickie' -password 'abcd'
$hostlist = Get-VMHost
Disconnect-VIServer * -confirm:$false
foreach ($string in $hostlist) {
Connect-VIserver -server $string -user 'root' -password '1234'
}
$vmlist = get-VM
Foreach ($virn in $vmlist){
write-host "Shutting down $virn" -BackgroundColor blue -ForegroundColor White
$virn | Shutdown-VMGuest -Confirm:$false
}
sleep 10
write-host "point1"
Disconnect-VIServer * -confirm:$false
foreach ($path in $hostlist) {
Connect-VIserver -server $path -user 'root' -password '1234'
Restart-VMHost $path.Name -RunAsync -force -Confirm:$false
write-host "Restarting $path" -BackgroundColor blue -ForegroundColor White
}
write-host "Waiting for hosts to come online(This will take 10 - 15 minutes)..... . ." -BackgroundColor blue -ForegroundColor White
sleep $SleepTimeOut
if(Test-Connection -ComputerName $path -Count 4 -ea silentlycontinue) {
write-host "Hosts are online. Continue with Syslog firewall enable." -BackgroundColor blue -ForegroundColor White
#configure syslog firewall
} else {
write-host "Hosts unavailable, please check UCS" -BackgroundColor red -ForegroundColor White
#exit the script
}
foreach ($string1 in $hostlist) {
Connect-VIserver $string1 -user 'root' -password '1234'
ForEach ($vms in Get-VM) {
write-host "starting up $vm"
Start-VM $vms
}
}
I made a few small changes which avoids the use of the VMHost and VM objects alltogether.
Does this still produce the same errors ?
$SleepTimeOut = 300
Connect-VIServer -server 'vCenter' -username 'vickie' -password 'abcd'
$hostlist = Get-VMHost | Select -ExpandProperty Name
Disconnect-VIServer * -confirm:$false
Connect-VIserver -server $hostlist -user 'root' -password '1234'
$vmlist = get-VM | Select -ExcludeProperty Name
Foreach ($virn in $vmlist){ write-host "Shutting down $virn" -BackgroundColor blue -ForegroundColor White
$virn | Shutdown-VMGuest -Confirm:$false
} sleep 10
write-host "point1"
Disconnect-VIServer * -confirm:$false foreach ($path in $hostlist) { Connect-VIserver -server $path -user 'root' -password '1234'
Restart-VMHost $path -RunAsync -force -Confirm:$false
write-host "Restarting $path" -BackgroundColor blue -ForegroundColor White
Disconnect-VIServer * -Confirm:$false
} write-host "Waiting for hosts to come online(This will take 10 - 15 minutes)..... . ." -BackgroundColor blue -ForegroundColor White sleep $SleepTimeOut if(Test-Connection -ComputerName $path -Count 4 -ea silentlycontinue) { write-host "Hosts are online. Continue with Syslog firewall enable." -BackgroundColor blue -ForegroundColor White
#configure syslog firewall } else { write-host "Hosts unavailable, please check UCS" -BackgroundColor red -ForegroundColor White
#exit the script } foreach ($string1 in $hostlist) { Connect-VIserver $string1 -user 'root' -password '1234'
ForEach ($vms in Get-VM) { write-host "starting up $vm"
Start-VM $vms
} }
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference