Sometime may need to restart multiple Virtual Machines for various reasons. When the Virtual Machine have no dependencies on each other it's an easy task.
However, most of the time Virtual Machines have dependencies on each other. So we need to have a specific order of how to restart it.
For example when you have Virtual Machines joined to a Domain, then the Virtual Machines must be started after the Virtual machine with the Domain Controller. Also, it's not enough only to start the Virtual Machine. You should verify that the appropriate Services have been started.
For this reason I have created a small script that can be used ot restart or shutdown Virtual Machines that have dependencies on each other.
So let's try it !!
Prerequisites
Before start to use the PowerShell Script be sure that you have apply the followings
- Allow access from your HYPER-V Host to your Virtual Machines in the ports:
- 5985, which is the WinRM port for HTTP or
- 5986, which is the WinRM port for HTTPS
Understanding the PowerShell Script
It's very crucial to understand every line of the PowerShell script to know what you are doing and to have the ability to extend or change it based on your requirements.
2 Virtual Machines
- Domain Controller
- Windows Server 2019 joined the Domain.
You have scheduled for maintenance and should shut down the Virtual Machines. When the maintenance finishes then you should start the Virtual Machines.
In this scenario, you should start first the Domain Controller and then the Windows Server 2019 VM.
So let's see the PowerShell script.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
$counter=0
Restart-VM DC1 -Wait -For IPAddress -Force
do
{
$counter+=1
Start-Sleep -Seconds 30
Write-Output 'Waiting to start the DNS Server Service'
$server_connection=Invoke-Command -ComputerName DCSRV1.askme4tech.com -Credential 'askme4tech\administrator' -ScriptBlock {(Get-Service DNS).Status}
$service_status=$server_connection | Select-Object -Property * -ExcludeProperty PSComputerName, RunspaceId,PSShowComputerName
$service_value=$service_status.Value
}
Until ($service_status.Value -like 'Running' -or $counter -ge 4)
"The DNS Server Service status is $service_value"
$ads=(Test-NetConnection -ComputerName DCSRV1.askme4tech.com -Port 53)
if ($ads.TcpTestSucceeded -like "True")
{
Start-VM WSRV2022
}
Else
{
"DNS Server Service failed to start"
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
In the first line we declare a variable as counter for the do-Until Loop. Is used to stop the Loop in case the Services that should start failed and can't be started.
The second line starts the Domain Controller and pauses the PowerShell until the network connection is established.
The next 9 lines are a loop with a do-Until that connects to the Domain Controller and gets the status of the DNS Server Service.
The following lines are used to inform the user about the status of the DNS Server Service.
Start-Sleep -Seconds 30
Write-Output 'Waiting to start the DNS Server Service'
The following line save the status of the Service in the variable $server_connection
$server_connection=Invoke-Command -ComputerName DCSRV1.askme4tech.com -Credential 'askme4tech\administrator' -ScriptBlock {(Get-Service DNS).Status}
The line excludes the PSComputerName, RunspaceId,PSShowComputerName which are coming with the Invoke-Command
$service_status=$server_connection | Select-Object -Property * -ExcludeProperty PSComputerName, RunspaceId,PSShowComputerName
This line extracts only the Status of the Service to use it in the next lines
$service_value=$service_status.Value
The loop continues until the DNS Server Service it's in Running Status or tries more than 4 times (The counter is working to avoid the loop to run without stop)
Until ($service_status.Value -like 'Running' -or $counter -ge 4)
When the DNS Server Service is in status Running then the loop stops and starts the Windows Server 2019 VM
I did some improvements from previous week for better functionality.
That's it for today.
Have a nice weekend !!!