SHIFT

--- Sjoerd Hooft's InFormation Technology ---

User Tools

Site Tools


Sidebar

Sponsor:


Recently Changed Pages:

View All Pages


View All Tags







WIKI Disclaimer: As with most other things on the Internet, the content on this wiki is not supported. It was contributed by me and is published “as is”. It has worked for me, and might work for you.
Also note that any view or statement expressed anywhere on this site are strictly mine and not the opinions or views of my employer.


Pages with comments

PageDateDiscussionTags
2019/11/18 13:52 1 Comment

View All Comments

srmwinservices

SRM Script: Start All Windows Services

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

Discussion

Enter your comment. Wiki syntax is allowed:
X M E E K
 
srmwinservices.txt · Last modified: 2019/11/18 12:25 (external edit)