SRM Script: Start All Windows Services

Summary: How to create a script to check if all services came up correctly after a failover with VMware Site Recovery Manager 5.1.
Date: 8 April 2013
Refactor: 29 April 2025: Checked links and formatting.

This script tests if all automatic Windows Services have been started as they should have. Note that this script does not work for a test environment. The SRM server executing this script lives in production network (of course) so it will only connect to production machines. In a recovery the recovered VMs are production, so in that case it will not be a problem. For checking up on Windows services in a test, please check SRM Script in Recovered VM: Check And Start Automatic Services.

The Script

# Script Details
# Author: Sjoerd Hooft
# Creation Date: 2013-04-08
 
# Script functionality
# 1. If this is a recovery plan run as test, exit. This script only works for a real recovery.
# 2. Get all Windows VMs that are started for a SRM Test / Recovery
# 3. Check automatic services on these VMs
# 4. Restart services if required
# 5. Email the log to the SRM administrator
 
# Note: This scrip can be run each time a RDM or a host is added.
 
# Script variables
$timestamp = Get-Date -format "yyyyMMdd-HH.mm"
$startdir = "G:\scripts"
$logfile = "$startdir\SRM-winservices-$timestamp.txt"
 
# Start Logging
# Note that you can suppress output from commands using | Out-Null
Function Add-LogEntry
{
    param
    (
        [String]$data = ""
    )
 
    # Sent to output so it shows up in the recovery plan results.
    $data
 
    # Log to file with timestamped data
    "{0} {1}" -f (Get-Date -format "yyyyMMdd-HH.mm"), $data | Out-File $logFile -Append
}
 
 
# Add VMware snapin
if(-not (Get-PSSnapin VMware.VimAutomation.Core)){
   Add-PSSnapin VMware.VimAutomation.Core}
 
# Determine Site Recovery Manager Mode
$recoverymode = $env:VMware_RecoveryMode
 
# Determine other SRM information
$recoveryplan = $env:VMware_RecoveryName
 
Add-LogEntry "$recoveryplan is run in $recoverymode mode"
 
# Setting email functionality
# Set this variable to $false if you don't want to receive emails regarding this script
$SendEmail = $true
# Set the SMTP Server address
$MailServer = "10.10.10.25"
# Set the email addresses
$toemail = "it@getshifting_com"
$fromemail = "srm@getshifting_com"
# Email functions
Function Send-Email ($subject, $info){if ($SendEmail){Send-MailMessage -To $toemail -From $fromemail -SmtpServer $mailserver -Subject $subject -Body $info -Attachments "$logfile"}}
 
# This script is not for test mode. In Test mode this script will query the live production VMs.
if ($recoverymode -eq "test")
{
    Add-LogEntry "This script is designed to run only when doing a recovery of VMs and will not run in a DR Test."
	Add-LogEntry "This script will exit now"
	exit 1
}
 
# VMware Settings
$cluster = "DR Cluster"
$vcenter = $env:VMware_VC_Host
 
# Connect to vCenter
Connect-VIServer $vcenter
 
# Get all Production VMs that are Powered On
$vms = Get-Cluster "$cluster" | Get-VM | where {$_.Name -like "???prd*" -and $_.Powerstate -eq "PoweredOn"}
Add-LogEntry "Found VMs: $vms"
 
# Check and Restart Automatic Services
ForEach ($vm in $vms){
   $vmview = Get-VM $VM | Get-View
   $vmhostname = $vmview.Guest.HostName
   Add-LogEntry "Checking services for $vmhostname"
   $svcs = Get-wmiobject win32_service -Filter "startmode = 'auto' AND state != 'running' AND name != 'Ati Hotkey Poller' AND name != 'NetBackup SAN Client Fibre Transport Service' AND name != 'BMR Boot Service' AND name != 'clr_optimization_v4.0.30319_32' AND name != 'clr_optimization_v4.0.30319_64' AND name != 'sppsvc' AND name != 'SysmonLog'" -ComputerName $vmhostname
      foreach ($service in $svcs){
      Add-Logentry "Starting the $service.DisplayName service on $vmhostname"
      $service.StartService()
      }
}
 
# Disconnect from vCenter
Disconnect-VIServer * -Confirm:$false
 
# Preparing info for email
# It would be preferred to list the recovery plan name in the subject and the VMs in the body. This is yet not possible with powershell/powerCLI.
$subject = "$timestamp $recoveryplan $recoverymode Result"
$info = "Results final recovery script"
Send-Email $subject $info