Script Sample - Azure Automation - Check VM Availability@20aman Oct 05, 2018
One of the most common scenarios is to check if a VM is available or not. If you want to connect to a VM and take some actions, it is a best practice to first check if the VM is available and responsive or not. This script sample performs multiple checks. You can tweak these checks to include only the ones that you need.
Also, there are some pieces of information that you can get only from inside the VM. E.g. the domain to which a VM is connected. This scripts ensures that the script is available. If it is available then it can also fetch such pieces of information for you.
What does the Script check
The script makes multiple checks which include:
- Ping check using the Test-Connection cmdlet
- PS Remoting checking by trying to open a PS Session using New-PSSession cmdlet
- The script also tries to connect to the WMI and list various object using Get-Wmiobject cmdlet
- Similar to the PS Remoting check it also checks for WS Man by using the Test-WSMan cmdlet
It can then fetch VM related information by either remoting into it or by using WMI. E.g. it can fetch domain of the VM by using below WMI cmdlet.
$Domain = $wmi.GetStringValue($HKLM, $MachineKey, "NV Domain").sValue
Script Inputs and Requirements
The script only takes below two input parameters:
The script requires a credential object with the name as "AutomationCredentialName". These credential should have login and remoting access into the VM.
Location of the Script
You can find this script in GitHub at this location: Check-VMAvailability.ps1