PowerShell and SRM – Basic Logic
Here is a script that shows how to get access to the environment variables that VMware sets for a command step and perform some basic control flow using them.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | # A starter script for examining the VMware environment variables and # performing some logic steps relating to the SRM workflow. # Be aware that the temp directory for the SYSTEM account is # %systemroot%\temp by default. Look there when you are running this in # the SRM application. $logFile = "{0}\srm_plan.log" -f $env:temp # A simple helper function for managing ouput. 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), $data | Out-File $logFile -Append } Add-LogEntry "Script Started" # Environment variables for all command steps $vcHost = $env:VMware_VC_Host $vcPort = $env:VMware_VC_Port $recoveryMode = $env:VMware_RecoveryMode -replace "`"","" $recoveryPlan = $env:VMware_RecoveryName -replace "`"","" # Environment variables for command steps operating on VMs $vmUuid = $env:VMware_VM_Uuid $vmName = $env:VMware_VM_Name $vmRef = $env:VMware_VM_Ref $vmGuestOs = $env:VMware_VM_GuestName $vmGuestIp = $env:VMware_VM_GuestIp $vmPath = $env:VMware_VM_Path # Output the settings for debugging purposes. Add-LogEntry ("vCenter Host: '{0}'" -f $vcHost) Add-LogEntry ("vCenter Port: '{0}'" -f $vcPort) Add-LogEntry ("SRM Recovery Mode: '{0}'" -f $recoveryMode) Add-LogEntry ("SRM Recovery Plan: '{0}'" -f $recoveryPlan) Add-LogEntry ("VM UUID: '{0}'" -f $vmUuid) Add-LogEntry ("VM Name: '{0}'" -f $vmName) Add-LogEntry ("VM MoRef: '{0}'" -f $vmRef) Add-LogEntry ("VM Guest OS: '{0}'" -f $vmGuestOs) Add-LogEntry ("VM Guest IP: '{0}'" -f $vmGuestIp) Add-LogEntry ("VM Path: '{0}'" -f $vmPath) # Some basic logic items. if ($vmName -ne $null) { Add-LogEntry "This command step is running for a specific VM." } if ($recoveryMode -eq "test") { Add-LogEntry "Running a test recovery." } Add-LogEntry "Script Complete" # Test exiting with an error code $exitWithError = $false Set-Variable -Name EXIT_SUCCESS -Value 0 -Option Constant Set-Variable -Name EXIT_FAILURE -Value 1 -Option Constant if ($exitWithError) { Add-LogEntry "Testing an exit with an error code." exit $EXIT_FAILURE } else { # Explicitly exit with a 0 code. exit $EXIT_SUCCESS } |