VMware Cloud Community
VicMware
Contributor
Contributor
Jump to solution

Restart-VMHost doesn't work after disconnect from vCenter

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
}  

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
VicMware
Contributor
Contributor
Jump to solution

It works!!

Thanks

Vickie

0 Kudos
VicMware
Contributor
Contributor
Jump to solution

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
    }
}          

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos