├── .gitignore ├── .gitattributes ├── NOTICE.txt ├── Meadowcroft.Srm.Storage.ps1 ├── Examples ├── SrmTagging.ps1 └── ReportConfiguration.ps1 ├── Meadowcroft.Srm.psd1 ├── README.md ├── Meadowcroft.Srm.psm1 ├── LICENSE.txt ├── Meadowcroft.Srm.Protection.ps1 └── Meadowcroft.Srm.Recovery.ps1 /.gitignore: -------------------------------------------------------------------------------- 1 | *.zip -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | *.psd1 diff 2 | -------------------------------------------------------------------------------- /NOTICE.txt: -------------------------------------------------------------------------------- 1 | 2 | Copyright (c) 2017 VMware, Inc. All Rights Reserved. 3 | 4 | This product is licensed to you under the Apache License version 2.0 (the "License"). You may not use this product except in compliance with the License. 5 | 6 | This product may include a number of subcomponents with separate copyright notices and license terms. Your use of these subcomponents is subject to the terms and conditions of the subcomponent's license, as noted in the LICENSE file. 7 | 8 | -------------------------------------------------------------------------------- /Meadowcroft.Srm.Storage.ps1: -------------------------------------------------------------------------------- 1 | # SRM Helper Methods - https://github.com/benmeadowcroft/SRM-Cmdlets 2 | 3 | <# 4 | .SYNOPSIS 5 | Trigger Discover Devices for Site Recovery Manager 6 | 7 | .OUTPUTS 8 | Returns discover devices task 9 | #> 10 | Function Start-DiscoverDevice { 11 | [cmdletbinding(SupportsShouldProcess=$True, ConfirmImpact="Medium")] 12 | [OutputType([VMware.VimAutomation.Srm.Views.DiscoverDevicesTask])] 13 | Param( 14 | [VMware.VimAutomation.Srm.Types.V1.SrmServer] $SrmServer 15 | ) 16 | 17 | $api = Get-ServerApiEndpoint -SrmServer $SrmServer 18 | $name = $SrmServer.Name 19 | [VMware.VimAutomation.Srm.Views.DiscoverDevicesTask] $task = $null 20 | if ($pscmdlet.ShouldProcess($name, "Rescan Storage Devices")) { 21 | $task = $api.Storage.DiscoverDevices() 22 | } 23 | return $task 24 | } 25 | -------------------------------------------------------------------------------- /Examples/SrmTagging.ps1: -------------------------------------------------------------------------------- 1 | # Depends on SRM Helper Methods - https://github.com/benmeadowcroft/SRM-Cmdlets 2 | # It is assumed that the connections to active VC and SRM Server have already been made 3 | 4 | Import-Module Meadowcroft.SRM -Prefix Srm 5 | 6 | $TagCategoryName = 'Meadowcroft.SRM.VM' 7 | $TagCategoryDescription = 'Tag category for tagging VMs with SRM state' 8 | 9 | # If the tag category doesn't exist, create it and the relevant tags 10 | $TagCategory = Get-TagCategory -Name $TagCategoryName -ErrorAction SilentlyContinue 11 | if (-Not $TagCategory) { 12 | Write-Output "Creating Tag Category $TagCategoryName" 13 | $TagCategory = New-TagCategory -Name $TagCategoryName -Description $TagCategoryDescription -EntityType 'VirtualMachine' 14 | 15 | Write-Output "Creating Tag SrmProtectedVm" 16 | New-Tag -Name 'SrmProtectedVm' -Category $TagCategory -Description "VM protected by VMware SRM" 17 | Write-Output "Creating Tag SrmTestVm" 18 | New-Tag -Name 'SrmTestVm' -Category $TagCategory -Description "Test VM instantiated by VMware SRM" 19 | Write-Output "Creating Tag SrmPlaceholderVm" 20 | New-Tag -Name 'SrmPlaceholderVm' -Category $TagCategory -Description "Placeholder VM used by VMware SRM" 21 | } 22 | 23 | $protectedVmTag = Get-Tag -Name 'SrmProtectedVm' -Category $TagCategory 24 | $testVmTag = Get-Tag -Name 'SrmTestVm' -Category $TagCategory 25 | $placeholderVmTag = Get-Tag -Name 'SrmPlaceholderVm' -Category $TagCategory 26 | 27 | # Assign protected tag to a VM, use ready state to get "local" protected VMs 28 | Get-SrmProtectedVM -State Ready | %{ New-TagAssignment -Tag $protectedVmTag -Entity $(Get-VIObjectByVIView $_.Vm) | Out-Null } 29 | 30 | # Assign test tag to a VM 31 | Get-SrmTestVM | %{ New-TagAssignment -Tag $testVmTag -Entity $_ | Out-Null } 32 | 33 | # Assign placeholder tag to a VM 34 | Get-SrmPlaceholderVM | %{ New-TagAssignment -Tag $placeholderVmTag -Entity $_ | Out-Null } 35 | -------------------------------------------------------------------------------- /Meadowcroft.Srm.psd1: -------------------------------------------------------------------------------- 1 | # 2 | # Module manifest for module 'Meadowcroft.Srm' 3 | # 4 | 5 | @{ 6 | 7 | # Script module or binary module file associated with this manifest. 8 | RootModule = 'Meadowcroft.Srm.psm1' 9 | 10 | # Version number of this module. 11 | ModuleVersion = '0.2' 12 | 13 | # ID used to uniquely identify this module 14 | GUID = 'f9247009-9168-4a21-831b-819f82884ffe' 15 | 16 | # Author of this module 17 | Author = 'Ben Meadowcroft' 18 | 19 | # Company or vendor of this module 20 | CompanyName = 'VMware, Inc' 21 | 22 | # Copyright statement for this module 23 | Copyright = '(c) 2014 - 2017. All rights reserved.' 24 | 25 | # Description of the functionality provided by this module 26 | # Description = '' 27 | 28 | # Minimum version of the Windows PowerShell engine required by this module 29 | # PowerShellVersion = '' 30 | 31 | # Name of the Windows PowerShell host required by this module 32 | # PowerShellHostName = '' 33 | 34 | # Minimum version of the Windows PowerShell host required by this module 35 | # PowerShellHostVersion = '' 36 | 37 | # Minimum version of Microsoft .NET Framework required by this module 38 | # DotNetFrameworkVersion = '' 39 | 40 | # Minimum version of the common language runtime (CLR) required by this module 41 | # CLRVersion = '' 42 | 43 | # Processor architecture (None, X86, Amd64) required by this module 44 | # ProcessorArchitecture = '' 45 | 46 | # Modules that must be imported into the global environment prior to importing this module 47 | RequiredModules = @{ModuleName='VMware.VimAutomation.Srm'; ModuleVersion='6.5'} 48 | 49 | # Assemblies that must be loaded prior to importing this module 50 | # RequiredAssemblies = @() 51 | 52 | # Script files (.ps1) that are run in the caller's environment prior to importing this module. 53 | # ScriptsToProcess = @() 54 | 55 | # Type files (.ps1xml) to be loaded when importing this module 56 | # TypesToProcess = @() 57 | 58 | # Format files (.ps1xml) to be loaded when importing this module 59 | # FormatsToProcess = @() 60 | 61 | # Modules to import as nested modules of the module specified in RootModule/ModuleToProcess 62 | NestedModules = 'Meadowcroft.Srm.Recovery.ps1','Meadowcroft.Srm.Protection.ps1','Meadowcroft.Srm.Storage.ps1' 63 | # NestedModules = @() 64 | 65 | # Functions to export from this module, note that internal functions use '_' not '-' as separator 66 | FunctionsToExport = '*-*' 67 | 68 | # Cmdlets to export from this module 69 | CmdletsToExport = '*' 70 | 71 | # Variables to export from this module 72 | VariablesToExport = '*' 73 | 74 | # Aliases to export from this module 75 | AliasesToExport = '*' 76 | 77 | # List of all modules packaged with this module 78 | # ModuleList = @() 79 | 80 | # List of all files packaged with this module 81 | # FileList = @() 82 | 83 | # Private data to pass to the module specified in RootModule/ModuleToProcess 84 | # PrivateData = '' 85 | 86 | # HelpInfo URI of this module 87 | # HelpInfoURI = '' 88 | 89 | # Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix. 90 | DefaultCommandPrefix = 'Srm' 91 | 92 | } 93 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Archive Notice 2 | 3 | Thie repository is now archived. The SRM Public API has evolved significantly and I am no longer involved with the SRM product and unable to keep it up to date or validate any changes. 4 | 5 | # SRM PowerCLI Cmdlets 6 | 7 | Helper functions for working with VMware SRM 6.5 with PowerCLI 6.5.1 or later. PowerShell 5.0 and above is required. 8 | 9 | This module is provided for illustrative/educational purposes to explain how the PowerCLI access to the SRM public API can be used. 10 | 11 | ## Getting Started 12 | 13 | ### Getting the SRM cmdlets 14 | 15 | The latest version of the software can be cloned from the git repository: 16 | 17 | git clone https://github.com/benmeadowcroft/SRM-Cmdlets.git 18 | 19 | Or downloaded as a [zip file](https://github.com/benmeadowcroft/SRM-Cmdlets/archive/master.zip). 20 | 21 | Specific releases (compatible with earlier PowerCLI and SRM versions) can be downloaded via the [release page](https://github.com/benmeadowcroft/SRM-Cmdlets/releases). 22 | 23 | ### Deploy SRM-Cmdlets module 24 | 25 | After cloning (or downloading and extracting) the PowerShell module, you can import the module into your current PowerShell session by by passing the path to `Meadowcroft.Srm.psd1` to the `Import-Module` cmdlet, e.g.: 26 | 27 | Import-Module -Name .\SRM-Cmdlets\Meadowcroft.Srm.psd1 28 | 29 | You can also install the module into the PowerShell path so it can be loaded implicitly. See [Microsoft's Installing Modules instructions](http://msdn.microsoft.com/en-us/library/dd878350) for more details on how to do this. 30 | 31 | The module uses the default prefix of `Srm` for the custom functions it defines. This can be overridden when importing the module by setting the value of the `-Prefix` parameter when calling `Import-Module`. 32 | 33 | ### Connecting to SRM 34 | 35 | After installing the module the next step is to connect to the SRM server. Details of how to do this are located in the [PowerCLI 6.5.1 User's Guide](http://pubs.vmware.com/vsphere-65/topic/com.vmware.powercli.ug.doc/GUID-A5F206CF-264D-4565-8CB9-4ED1C337053F.html) 36 | 37 | $credential = Get-Credential 38 | Connect-VIServer -Server vc-a.example.com -Credential $credential 39 | Connect-SrmServer -Credential $credential -RemoteCredential $credential 40 | 41 | At this point we've just been using the cmdlets provided by PowerCLI, the PowerCLI documentation also provides some examples of how to call the SRM API to perform various tasks. In the rest of this introduction we'll perform some of those tasks using the custom functions defined in this project. 42 | 43 | ### Report the Protected Virtual Machines and Their Protection Groups 44 | 45 | Goal: Create a simple report listing the VMs protected by SRM and the protection group they belong to. 46 | 47 | Get-SrmProtectionGroup | %{ 48 | $pg = $_ 49 | Get-SrmProtectedVM -ProtectionGroup $pg } | %{ 50 | $output = "" | select VmName, PgName 51 | $output.VmName = $_.Vm.Name 52 | $output.PgName = $pg.GetInfo().Name 53 | $output 54 | } | Format-Table @{Label="VM Name"; Expression={$_.VmName} }, 55 | @{Label="Protection group name"; Expression={$_.PgName} 56 | } 57 | 58 | ### Report the Last Recovery Plan Test 59 | 60 | Goal: Create a simple report listing the state of the last test of a recovery plan 61 | 62 | Get-SrmRecoveryPlan | %{ $_ | 63 | Get-SrmRecoveryPlanResult -RecoveryMode Test | select -First 1 64 | } | Select Name, StartTime, RunMode, ResultState | Format-Table 65 | 66 | 67 | ### Execute a Recovery Plan Test 68 | 69 | Goal: for a specific recovery plan, execute a test failover. Note the "local" SRM server we are connected to should be the recovery site in order for this to be successful. 70 | 71 | Get-SrmRecoveryPlan -Name "Name of Plan" | Start-SrmRecoveryPlan -RecoveryMode Test 72 | 73 | ### Export the Detailed XML Report of the Last Recovery Plan Workflow 74 | 75 | Goal: get the XML report of the last recovery plan execution for a specific recovery plan. 76 | 77 | Get-SrmRecoveryPlan -Name "Name of Plan" | Get-SrmRecoveryPlanResult | 78 | select -First 1 | Export-SrmRecoveryPlanResultAsXml 79 | 80 | ### Protect a Replicated VM 81 | 82 | Goal: Take a VM replicated using vSphere Replication or Array Based Replication, add it to an appropriate protection group and configure it for protection 83 | 84 | $pg = Get-SrmProtectionGroup "Name of Protection Group" 85 | Get-VM vm-01a | Protect-SrmVM -ProtectionGroup $pg 86 | -------------------------------------------------------------------------------- /Meadowcroft.Srm.psm1: -------------------------------------------------------------------------------- 1 | # SRM Helper Methods - https://github.com/benmeadowcroft/SRM-Cmdlets 2 | 3 | <# 4 | .SYNOPSIS 5 | This is intended to be an "internal" function only. It filters a 6 | pipelined input of objects and elimiates duplicates as identified 7 | by the MoRef property on the object. 8 | 9 | .LINK 10 | https://github.com/benmeadowcroft/SRM-Cmdlets/ 11 | #> 12 | Function Select_UniqueByMoRef { 13 | 14 | Param( 15 | [Parameter (ValueFromPipeline=$true)] $in 16 | ) 17 | process { 18 | $moref = New-Object System.Collections.ArrayList 19 | $in | Sort-Object | Select-Object MoRef -Unique | ForEach-Object { $moref.Add($_.MoRef) } > $null 20 | $in | ForEach-Object { 21 | if ($_.MoRef -in $moref) { 22 | $moref.Remove($_.MoRef) 23 | $_ #output 24 | } 25 | } 26 | } 27 | } 28 | 29 | <# 30 | .SYNOPSIS 31 | This is intended to be an "internal" function only. It gets the 32 | MoRef property of a VM from either a VM object, a VM view, or the 33 | protected VM object. 34 | #> 35 | Function Get_MoRefFromVmObj { 36 | Param( 37 | [Parameter (ValueFromPipeline=$true)][VMware.VimAutomation.ViCore.Types.V1.Inventory.VirtualMachine] $Vm, 38 | [Parameter (ValueFromPipeline=$true)][VMware.Vim.VirtualMachine] $VmView, 39 | [Parameter (ValueFromPipeline=$true)][VMware.VimAutomation.Srm.Views.SrmProtectionGroupProtectedVm] $ProtectedVm 40 | ) 41 | 42 | 43 | $moRef = $null 44 | if ($Vm.ExtensionData.MoRef) { # VM object 45 | $moRef = $Vm.ExtensionData.MoRef 46 | } elseif ($VmView.MoRef) { # VM view 47 | $moRef = $VmView.MoRef 48 | } elseif ($protectedVm) { 49 | $moRef = $ProtectedVm.Vm.MoRef 50 | } 51 | 52 | $moRef 53 | } 54 | 55 | <# 56 | .SYNOPSIS 57 | Lookup the srm instance for a specific server. 58 | #> 59 | Function Get-Server { 60 | [cmdletbinding()] 61 | Param( 62 | [string] $SrmServerAddress, 63 | [VMware.VimAutomation.Srm.Types.V1.SrmServer] $SrmServer 64 | ) 65 | 66 | $found = $null 67 | 68 | if ($SrmServer) { 69 | $found = $SrmServer 70 | } elseif ($SrmServerAddress) { 71 | # search for server address in default servers 72 | $global:DefaultSrmServers | ForEach-Object { 73 | if ($_.Name -ieq $SrmServerAddress) { 74 | $found = $_ 75 | } 76 | } 77 | if (-not $found) { 78 | throw "SRM server $SrmServerAddress not found. Connect-Server must be called first." 79 | } 80 | } 81 | 82 | if (-not $found) { 83 | #default result 84 | $found = $global:DefaultSrmServers[0] 85 | } 86 | 87 | return $found; 88 | } 89 | 90 | <# 91 | .SYNOPSIS 92 | Retrieve the SRM Server Version 93 | #> 94 | Function Get-ServerVersion { 95 | [cmdletbinding()] 96 | Param( 97 | [VMware.VimAutomation.Srm.Types.V1.SrmServer] $SrmServer 98 | ) 99 | $srm = Get-Server $SrmServer 100 | $srm.Version 101 | } 102 | 103 | <# 104 | .SYNOPSIS 105 | Lookup the SRM API endpoint for a specific server. 106 | #> 107 | Function Get-ServerApiEndpoint { 108 | [cmdletbinding()] 109 | Param( 110 | [string] $SrmServerAddress, 111 | [VMware.VimAutomation.Srm.Types.V1.SrmServer] $SrmServer 112 | ) 113 | 114 | [VMware.VimAutomation.Srm.Types.V1.SrmServer] $server = Get-Server -SrmServerAddress $SrmServerAddress -SrmServer $SrmServer 115 | 116 | return $server.ExtensionData 117 | } 118 | 119 | <# 120 | .SYNOPSIS 121 | Get the placeholder VMs that are associated with SRM 122 | #> 123 | Function Get-PlaceholderVM { 124 | [cmdletbinding()] 125 | Param() 126 | Get-VM @Args | Where-Object {$_.ExtensionData.Config.ManagedBy.extensionKey -like "com.vmware.vcDr*" -and $_.ExtensionData.Config.ManagedBy.Type -ieq 'placeholderVm'} 127 | } 128 | 129 | <# 130 | .SYNOPSIS 131 | Get the test VMs that are associated with SRM 132 | #> 133 | Function Get-TestVM { 134 | [cmdletbinding()] 135 | Param() 136 | Get-VM @Args | Where-Object {$_.ExtensionData.Config.ManagedBy.extensionKey -like "com.vmware.vcDr*" -and $_.ExtensionData.Config.ManagedBy.Type -ieq 'testVm'} 137 | } 138 | 139 | <# 140 | .SYNOPSIS 141 | Get the VMs that are replicated using vSphere Replication. These may not be SRM 142 | protected VMs. 143 | #> 144 | Function Get-ReplicatedVM { 145 | [cmdletbinding()] 146 | Param() 147 | Get-VM @Args | Where-Object {($_.ExtensionData.Config.ExtraConfig | Where-Object { $_.Key -eq 'hbr_filter.destination' -and $_.Value } )} 148 | } 149 | -------------------------------------------------------------------------------- /Examples/ReportConfiguration.ps1: -------------------------------------------------------------------------------- 1 | # Depends on SRM Helper Methods - https://github.com/benmeadowcroft/SRM-Cmdlets 2 | # It is assumed that the connection to VC and SRM Server have already been made 3 | 4 | Function Get-SrmConfigReportSite { 5 | Param( 6 | [VMware.VimAutomation.Srm.Types.V1.SrmServer] $SrmServer 7 | ) 8 | 9 | Get-SrmServer $SrmServer | 10 | Format-Table -Wrap -AutoSize @{Label="SRM Site Name"; Expression={$_.ExtensionData.GetSiteName()} }, 11 | @{Label="SRM Host"; Expression={$_.Name} }, 12 | @{Label="SRM Port"; Expression={$_.Port} }, 13 | @{Label="Version"; Expression={$_.Version} }, 14 | @{Label="Build"; Expression={$_.Build} }, 15 | @{Label="SRM Peer Site Name"; Expression={$_.ExtensionData.GetPairedSite().Name} } 16 | } 17 | 18 | Function Get-SrmConfigReportPlan { 19 | Param( 20 | [VMware.VimAutomation.Srm.Types.V1.SrmServer] $SrmServer 21 | ) 22 | 23 | Get-SrmRecoveryPlan -SrmServer $SrmServer | %{ 24 | $rp = $_ 25 | $rpinfo = $rp.GetInfo() 26 | $peerState = $rp.GetPeer().State 27 | $pgs = Get-SrmProtectionGroup -RecoveryPlan $rp 28 | $pgnames = $pgs | %{ $_.GetInfo().Name } 29 | 30 | $output = "" | select plan, state, peerState, groups 31 | $output.plan = $rpinfo.Name 32 | $output.state = $rpinfo.State 33 | $output.peerState = $peerState 34 | if ($pgnames) { 35 | $output.groups = [string]::Join(",`r`n", $pgnames) 36 | } else { 37 | $output.groups = "NONE" 38 | } 39 | 40 | $output 41 | } | Format-Table -Wrap -AutoSize @{Label="Recovery Plan Name"; Expression={$_.plan} }, 42 | @{Label="Recovery State"; Expression={$_.state} }, 43 | @{Label="Peer Recovery State"; Expression={$_.peerState} }, 44 | @{Label="Protection Groups"; Expression={$_.groups}} 45 | } 46 | 47 | 48 | Function Get-SrmConfigReportProtectionGroup { 49 | Param( 50 | [VMware.VimAutomation.Srm.Types.V1.SrmServer] $SrmServer 51 | ) 52 | 53 | Get-SrmProtectionGroup -SrmServer $SrmServer | %{ 54 | $pg = $_ 55 | $pginfo = $pg.GetInfo() 56 | $pgstate = $pg.GetProtectionState() 57 | $peerState = $pg.GetPeer().State 58 | $rps = Get-SrmRecoveryPlan -ProtectionGroup $pg 59 | $rpnames = $rps | %{ $_.GetInfo().Name } 60 | 61 | $output = "" | select name, type, state, peerState, plans 62 | $output.name = $pginfo.Name 63 | $output.type = $pginfo.Type 64 | $output.state = $pgstate 65 | $output.peerState = $peerState 66 | if ($rpnames) { 67 | $output.plans = [string]::Join(",`r`n", $rpnames) 68 | } else { 69 | $output.plans = "NONE" 70 | } 71 | 72 | $output 73 | } | Format-Table -Wrap -AutoSize @{Label="Protection Group Name"; Expression={$_.name} }, 74 | @{Label="Type"; Expression={$_.type} }, 75 | @{Label="Protection State"; Expression={$_.state} }, 76 | @{Label="Peer Protection State"; Expression={$_.peerState} }, 77 | @{Label="Recovery Plans"; Expression={$_.plans} } 78 | } 79 | 80 | 81 | Function Get-SrmConfigReportProtectedDatastore { 82 | Param( 83 | [VMware.VimAutomation.Srm.Types.V1.SrmServer] $SrmServer 84 | ) 85 | 86 | Get-SrmProtectionGroup -SrmServer $SrmServer -Type "san" | %{ 87 | $pg = $_ 88 | $pginfo = $pg.GetInfo() 89 | $pds = Get-SrmProtectedDatastore -ProtectionGroup $pg 90 | $pds | %{ 91 | $pd = $_ 92 | $output = "" | select datacenter, group, name, capacity, free 93 | $output.datacenter = $pd.Datacenter.Name 94 | $output.group = $pginfo.Name 95 | $output.name = $pd.Name 96 | $output.capacity = $pd.CapacityGB 97 | $output.free = $pd.FreeSpaceGB 98 | 99 | $output 100 | 101 | } 102 | } | Format-Table -Wrap -AutoSize -GroupBy "datacenter" @{Label="Datastore Name"; Expression={$_.name} }, 103 | @{Label="Capacity GB"; Expression={$_.capacity} }, 104 | @{Label="Free GB"; Expression={$_.free} }, 105 | @{Label="Protection Group"; Expression={$_.group} } 106 | } 107 | 108 | 109 | Function Get-SrmConfigReportProtectedVm { 110 | Param( 111 | [VMware.VimAutomation.Srm.Types.V1.SrmServer] $SrmServer 112 | ) 113 | 114 | $srmversion = Get-SrmServerVersion -SrmServer $SrmServer 115 | $srmMajorVersion, $srmMinorVersion = $srmversion -split "\." 116 | 117 | Get-SrmProtectionGroup -SrmServer $SrmServer | %{ 118 | $pg = $_ 119 | $pginfo = $pg.GetInfo() 120 | $pvms = Get-SrmProtectedVM -ProtectionGroup $pg 121 | $rps = Get-SrmRecoveryPlan -ProtectionGroup $pg 122 | $rpnames = $rps | %{ $_.GetInfo().Name } 123 | $pvms | %{ 124 | $pvm = $_ 125 | if ($srmMajorVersion -ge 6 -or ($srmMajorVersion -eq 5 -and $srmMinorVersion -eq 8)) { 126 | $rs = $rps | Select -First 1 | %{ $_.GetRecoverySettings($pvm.Vm.MoRef) } 127 | } 128 | $output = "" | select group, name, moRef, needsConfiguration, state, plans, priority, finalPowerState, preCallouts, postCallouts 129 | $output.group = $pginfo.Name 130 | $output.name = $pvm.Vm.Name 131 | $output.moRef = $pvm.Vm.MoRef # this is necessary in case we can't retrieve the name when VC is unavailable 132 | $output.needsConfiguration = $pvm.NeedsConfiguration 133 | $output.state = $pvm.State 134 | $output.plans = [string]::Join(",`r`n", $rpnames) 135 | if ($rs) { 136 | $output.priority = $rs.RecoveryPriority 137 | $output.finalPowerState = $rs.FinalPowerState 138 | $output.preCallouts = $rs.PrePowerOnCallouts.Count 139 | $output.postCallouts = $rs.PostPowerOnCallouts.Count 140 | } 141 | $output 142 | 143 | } 144 | } | Format-Table -Wrap -AutoSize @{Label="VM Name"; Expression={$_.name} }, 145 | @{Label="VM MoRef"; Expression={$_.moRef} }, 146 | @{Label="Needs Config"; Expression={$_.needsConfiguration} }, 147 | @{Label="VM Protection State"; Expression={$_.state} }, 148 | @{Label="Protection Group"; Expression={$_.group} }, 149 | @{Label="Recovery Plans"; Expression={$_.plans} }, 150 | @{Label="Recovery Priority"; Expression={$_.priority} }, 151 | @{Label="Final Power State"; Expression={$_.finalPowerState} }, 152 | @{Label="Pre-PowerOn Callouts"; Expression={$_.preCallouts} }, 153 | @{Label="Post-PowerOn Callouts"; Expression={$_.postCallouts} } 154 | 155 | } 156 | 157 | Function Get-SrmConfigReport { 158 | Param( 159 | [VMware.VimAutomation.Srm.Types.V1.SrmServer] $SrmServer 160 | ) 161 | 162 | Get-SrmConfigReportSite -SrmServer $SrmServer 163 | Get-SrmConfigReportPlan -SrmServer $SrmServer 164 | Get-SrmConfigReportProtectionGroup -SrmServer $SrmServer 165 | Get-SrmConfigReportProtectedDatastore -SrmServer $SrmServer 166 | Get-SrmConfigReportProtectedVm -SrmServer $SrmServer 167 | } 168 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. 13 | 14 | "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. 15 | 16 | "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. 17 | 18 | "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. 19 | 20 | "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. 21 | 22 | "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). 23 | 24 | "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. 25 | 26 | "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." 27 | 28 | "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 29 | 30 | 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 31 | 32 | 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 33 | 34 | 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: 35 | (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and 36 | (b) You must cause any modified files to carry prominent notices stating that You changed the files; and 37 | (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and 38 | (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. 39 | 40 | You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 41 | 42 | 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 43 | 44 | 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 45 | 46 | 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 47 | 48 | 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 49 | 50 | 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. 51 | 52 | END OF TERMS AND CONDITIONS 53 | 54 | APPENDIX: How to apply the Apache License to your work. 55 | 56 | To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. 57 | 58 | Copyright [yyyy] [name of copyright owner] 59 | 60 | Licensed under the Apache License, Version 2.0 (the "License"); 61 | you may not use this file except in compliance with the License. 62 | You may obtain a copy of the License at 63 | 64 | http://www.apache.org/licenses/LICENSE-2.0 65 | 66 | Unless required by applicable law or agreed to in writing, software 67 | distributed under the License is distributed on an "AS IS" BASIS, 68 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 69 | implied. 70 | 71 | See the License for the specific language governing permissions and 72 | limitations under the License. 73 | 74 | 75 | -------------------------------------------------------------------------------- /Meadowcroft.Srm.Protection.ps1: -------------------------------------------------------------------------------- 1 | # SRM Helper Methods - https://github.com/benmeadowcroft/SRM-Cmdlets 2 | 3 | <# 4 | .SYNOPSIS 5 | Get the subset of protection groups matching the input criteria 6 | 7 | .PARAMETER Name 8 | Return protection groups matching the specified name 9 | 10 | .PARAMETER Type 11 | Return protection groups matching the specified protection group 12 | type. For SRM 5.0-5.5 this is either 'san' for protection groups 13 | consisting of a set of replicated datastores or 'vr' for vSphere 14 | Replication based protection groups. 15 | 16 | .PARAMETER RecoveryPlan 17 | Return protection groups associated with a particular recovery 18 | plan 19 | 20 | .PARAMETER SrmServer 21 | the SRM server to use for this operation. 22 | #> 23 | Function Get-ProtectionGroup { 24 | [cmdletbinding()] 25 | Param( 26 | [Parameter(position=1)][string] $Name, 27 | [string] $Type, 28 | [Parameter (ValueFromPipeline=$true)][VMware.VimAutomation.Srm.Views.SrmRecoveryPlan[]] $RecoveryPlan, 29 | [VMware.VimAutomation.Srm.Types.V1.SrmServer] $SrmServer 30 | ) 31 | begin { 32 | $api = Get-ServerApiEndpoint -SrmServer $SrmServer 33 | $pgs = @() 34 | } 35 | process { 36 | if ($RecoveryPlan) { 37 | foreach ($rp in $RecoveryPlan) { 38 | $pgs += $RecoveryPlan.GetInfo().ProtectionGroups 39 | } 40 | $pgs = Select_UniqueByMoRef($pgs) 41 | } else { 42 | $pgs += $api.Protection.ListProtectionGroups() 43 | } 44 | } 45 | end { 46 | $pgs | ForEach-Object { 47 | $pg = $_ 48 | $pgi = $pg.GetInfo() 49 | $selected = (-not $Name -or ($Name -eq $pgi.Name)) -and (-not $Type -or ($Type -eq $pgi.Type)) 50 | if ($selected) { 51 | Add-Member -InputObject $pg -MemberType NoteProperty -Name "Name" -Value $pgi.Name 52 | $pg 53 | } 54 | } 55 | } 56 | } 57 | 58 | <# 59 | .SYNOPSIS 60 | Get the subset of protected VMs matching the input criteria 61 | 62 | .PARAMETER Name 63 | Return protected VMs matching the specified name 64 | 65 | .PARAMETER State 66 | Return protected VMs matching the specified state. For protected 67 | VMs on the protected site this is usually 'ready', for 68 | placeholder VMs this is 'shadowing' 69 | 70 | .PARAMETER ProtectionGroup 71 | Return protected VMs associated with particular protection 72 | groups 73 | #> 74 | Function Get-ProtectedVM { 75 | [cmdletbinding()] 76 | Param( 77 | [Parameter(position=1)][string] $Name, 78 | [VMware.VimAutomation.Srm.Views.SrmProtectionGroupProtectionState] $State, 79 | [VMware.VimAutomation.Srm.Views.SrmProtectionGroupProtectionState] $PeerState, 80 | [switch] $ConfiguredOnly, 81 | [switch] $UnconfiguredOnly, 82 | [Parameter (ValueFromPipeline=$true)][VMware.VimAutomation.Srm.Views.SrmProtectionGroup[]] $ProtectionGroup, 83 | [Parameter (ValueFromPipeline=$true)][VMware.VimAutomation.Srm.Views.SrmRecoveryPlan[]] $RecoveryPlan, 84 | [string] $ProtectionGroupName, 85 | [VMware.VimAutomation.Srm.Types.V1.SrmServer] $SrmServer 86 | ) 87 | 88 | if ($null -eq $ProtectionGroup) { 89 | $ProtectionGroup = Get-ProtectionGroup -Name $ProtectionGroupName -RecoveryPlan $RecoveryPlan -SrmServer $SrmServer 90 | } 91 | $ProtectionGroup | ForEach-Object { 92 | $pg = $_ 93 | $pg.ListProtectedVms() | ForEach-Object { 94 | # try and update the view data for the protected VM 95 | try { 96 | $_.Vm.UpdateViewData() 97 | } catch { 98 | Write-Error $_ 99 | } finally { 100 | $_ 101 | } 102 | } | Where-object { -not $Name -or ($Name -eq $_.Vm.Name) } | 103 | where-object { -not $State -or ($State -eq $_.State) } | 104 | where-object { -not $PeerState -or ($PeerState -eq $_.PeerState) } | 105 | where-object { ($ConfiguredOnly -and $_.NeedsConfiguration -eq $false) -or ($UnconfiguredOnly -and $_.NeedsConfiguration -eq $true) -or (-not $ConfiguredOnly -and -not $UnconfiguredOnly) } 106 | } 107 | } 108 | 109 | 110 | <# 111 | .SYNOPSIS 112 | Get the unprotected VMs that are associated with a protection group 113 | 114 | .PARAMETER ProtectionGroup 115 | Return unprotected VMs associated with particular protection 116 | groups. For VR protection groups this is VMs that are associated 117 | with the PG but not configured, For ABR protection groups this is 118 | VMs on replicated datastores associated with the group that are not 119 | configured. 120 | #> 121 | Function Get-UnProtectedVM { 122 | [cmdletbinding()] 123 | Param( 124 | [Parameter (ValueFromPipeline=$true)][VMware.VimAutomation.Srm.Views.SrmProtectionGroup[]] $ProtectionGroup, 125 | [Parameter (ValueFromPipeline=$true)][VMware.VimAutomation.Srm.Views.SrmRecoveryPlan[]] $RecoveryPlan, 126 | [string] $ProtectionGroupName, 127 | [VMware.VimAutomation.Srm.Types.V1.SrmServer] $SrmServer 128 | ) 129 | 130 | if ($null -eq $ProtectionGroup) { 131 | $ProtectionGroup = Get-ProtectionGroup -Name $ProtectionGroupName -RecoveryPlan $RecoveryPlan -SrmServer $SrmServer 132 | } 133 | 134 | $associatedVMs = @() 135 | $protectedVmRefs = @() 136 | 137 | $ProtectionGroup | ForEach-Object { 138 | $pg = $_ 139 | # For VR listAssociatedVms to get list of VMs 140 | if ($pg.GetInfo().Type -eq 'vr') { 141 | $associatedVMs += @($pg.ListAssociatedVms() | Get-VIObjectByVIView) 142 | } 143 | # TODO test this: For ABR get VMs on GetProtectedDatastore 144 | if ($pg.GetInfo().Type -eq 'san') { 145 | $pds = @(Get-ProtectedDatastore -ProtectionGroup $pg) 146 | $pds | ForEach-Object { 147 | $ds = Get-Datastore -id $_.MoRef 148 | $associatedVMs += @(Get-VM -Datastore $ds) 149 | } 150 | } 151 | 152 | # get protected VMs 153 | $protectedVmRefs += @(Get-ProtectedVM -ProtectionGroup $pg | ForEach-Object { $_.Vm.MoRef } | Select-Object -Unique) 154 | } 155 | 156 | # get associated but unprotected VMs 157 | $associatedVMs | Where-Object { $protectedVmRefs -notcontains $_.ExtensionData.MoRef } 158 | } 159 | 160 | 161 | #Untested as I don't have ABR setup in my lab yet 162 | <# 163 | .SYNOPSIS 164 | Get the subset of protected Datastores matching the input criteria 165 | 166 | .PARAMETER ProtectionGroup 167 | Return protected datastores associated with particular protection 168 | groups 169 | #> 170 | Function Get-ProtectedDatastore { 171 | [cmdletbinding()] 172 | Param( 173 | [Parameter (ValueFromPipeline=$true)][VMware.VimAutomation.Srm.Views.SrmProtectionGroup[]] $ProtectionGroup, 174 | [Parameter (ValueFromPipeline=$true)][VMware.VimAutomation.Srm.Views.SrmRecoveryPlan[]] $RecoveryPlan, 175 | [string] $ProtectionGroupName, 176 | [VMware.VimAutomation.Srm.Types.V1.SrmServer] $SrmServer 177 | ) 178 | 179 | if (-not $ProtectionGroup) { 180 | $ProtectionGroup = Get-ProtectionGroup -Name $ProtectionGroupName -RecoveryPlan $RecoveryPlan -SrmServer $SrmServer 181 | } 182 | $ProtectionGroup | ForEach-Object { 183 | $pg = $_ 184 | if ($pg.GetInfo().Type -eq 'san') { # only supported for array based replication datastores 185 | $pg.ListProtectedDatastores() 186 | } 187 | } 188 | } 189 | 190 | 191 | #Untested as I don't have ABR setup in my lab yet 192 | <# 193 | .SYNOPSIS 194 | Get the replicated datastores that aren't associated with a protection group. 195 | #> 196 | Function Get-ReplicatedDatastore { 197 | [cmdletbinding()] 198 | Param( 199 | [VMware.VimAutomation.Srm.Types.V1.SrmServer] $SrmServer 200 | ) 201 | 202 | $api = Get-ServerApiEndpoint -SrmServer $SrmServer 203 | 204 | $api.Protection.ListUnassignedReplicatedDatastores() 205 | } 206 | 207 | <# 208 | .SYNOPSIS 209 | Protect a VM using SRM 210 | 211 | .PARAMETER ProtectionGroup 212 | The protection group that this VM will belong to 213 | 214 | .PARAMETER Vm 215 | The virtual machine to protect 216 | #> 217 | Function Protect-VM { 218 | [cmdletbinding()] 219 | Param( 220 | [Parameter (Mandatory=$true)][VMware.VimAutomation.Srm.Views.SrmProtectionGroup] $ProtectionGroup, 221 | [Parameter (ValueFromPipeline=$true)][VMware.VimAutomation.ViCore.Types.V1.Inventory.VirtualMachine] $Vm, 222 | [Parameter (ValueFromPipeline=$true)][VMware.Vim.VirtualMachine] $VmView 223 | ) 224 | 225 | $moRef = Get_MoRefFromVmObj -Vm $Vm -VmView $VmView 226 | 227 | $pgi = $ProtectionGroup.GetInfo() 228 | #TODO query protection status first 229 | 230 | if ($moRef) { 231 | if ($pgi.Type -eq 'vr') { 232 | $ProtectionGroup.AssociateVms(@($moRef)) 233 | } 234 | $protectionSpec = New-Object VMware.VimAutomation.Srm.Views.SrmProtectionGroupVmProtectionSpec 235 | $protectionSpec.Vm = $moRef 236 | $protectTask = $ProtectionGroup.ProtectVms($protectionSpec) 237 | while(-not $protectTask.IsComplete()) { Start-Sleep -Seconds 1 } 238 | $protectTask.GetResult() 239 | } else { 240 | throw "Can't protect the VM, no MoRef found." 241 | } 242 | } 243 | 244 | 245 | <# 246 | .SYNOPSIS 247 | Unprotect a VM using SRM 248 | 249 | .PARAMETER ProtectionGroup 250 | The protection group that this VM will be removed from 251 | 252 | .PARAMETER Vm 253 | The virtual machine to unprotect 254 | #> 255 | Function Unprotect-VM { 256 | [cmdletbinding()] 257 | Param( 258 | [Parameter (Mandatory=$true)][VMware.VimAutomation.Srm.Views.SrmProtectionGroup] $ProtectionGroup, 259 | [Parameter (ValueFromPipeline=$true)][VMware.VimAutomation.ViCore.Types.V1.Inventory.VirtualMachine] $Vm, 260 | [Parameter (ValueFromPipeline=$true)][VMware.Vim.VirtualMachine] $VmView, 261 | [Parameter (ValueFromPipeline=$true)][VMware.VimAutomation.Srm.Views.SrmProtectionGroupProtectedVm] $ProtectedVm 262 | ) 263 | 264 | $moRef = Get_MoRefFromVmObj -Vm $Vm -VmView $VmView -ProtectedVm $ProtectedVm 265 | 266 | $pgi = $ProtectionGroup.GetInfo() 267 | $protectTask = $ProtectionGroup.UnprotectVms($moRef) 268 | while(-not $protectTask.IsComplete()) { Start-Sleep -Seconds 1 } 269 | if ($pgi.Type -eq 'vr') { 270 | $ProtectionGroup.UnassociateVms(@($moRef)) 271 | } 272 | $protectTask.GetResult() 273 | } 274 | 275 | <# 276 | .SYNOPSIS 277 | Get a protection group folder 278 | 279 | .PARAMETER SrmServer 280 | The SRM Server to query for the protection group folder 281 | #> 282 | Function Get-ProtectionGroupFolder { 283 | [cmdletbinding()] 284 | Param( 285 | [VMware.VimAutomation.Srm.Types.V1.SrmServer] $SrmServer 286 | ) 287 | 288 | $api = Get-ServerApiEndpoint -SrmServer $SrmServer 289 | 290 | $folder = $api.Protection.GetProtectionGroupRootFolder() 291 | 292 | return $folder 293 | } 294 | 295 | <# 296 | .SYNOPSIS 297 | Create a new protection group 298 | 299 | .PARAMETER Name 300 | The name of the protection group 301 | 302 | .PARAMETER Description 303 | Description of the protection group 304 | 305 | .PARAMETER Folder 306 | The protection group folder in which to create the new protection group 307 | 308 | .PARAMETER ArrayReplication 309 | Set if protection group is for replicating VMs using Array based replication 310 | 311 | .PARAMETER vSphereReplication 312 | Set if protection group is for replicating VMs with vSphere Replication 313 | 314 | .PARAMETER VMs 315 | For vSphere Replication based protection, the VMs to add to the replication 316 | group. These should already be replicated. 317 | 318 | .PARAMETER VMViews 319 | For vSphere Replication based protection, the VMs to add to the replication 320 | group. These should already be replicated. 321 | 322 | .PARAMETER SrmServer 323 | The SRM Server to perform the operation against 324 | #> 325 | Function New-ProtectionGroup { 326 | [cmdletbinding(DefaultParameterSetName="VR", SupportsShouldProcess=$True, ConfirmImpact="Medium")] 327 | [OutputType([VMware.VimAutomation.Srm.Views.SrmProtectionGroup])] 328 | Param( 329 | [Parameter (Mandatory=$true)] $Name, 330 | $Description, 331 | [VMware.VimAutomation.Srm.Views.SrmProtectionGroupFolder] $Folder, 332 | [Parameter (ParameterSetName="ABR", Mandatory=$true)][switch] $ArrayReplication, 333 | [Parameter (ValueFromPipeline=$true, ParameterSetName="ABR")][VMware.VimAutomation.ViCore.Types.V1.DatastoreManagement.Datastore[]] $Datastores, 334 | [Parameter (ValueFromPipeline=$true, ParameterSetName="ABR")][VMware.Vim.Datastore[]] $DatastoreViews, 335 | [Parameter (ParameterSetName="VR", Mandatory=$true)][switch] $vSphereReplication, 336 | [Parameter (ValueFromPipeline=$true, ParameterSetName="VR")][VMware.VimAutomation.ViCore.Types.V1.Inventory.VirtualMachine[]] $VMs, 337 | [Parameter (ValueFromPipeline=$true, ParameterSetName="VR")][VMware.Vim.VirtualMachine[]] $VMViews, 338 | [VMware.VimAutomation.Srm.Types.V1.SrmServer] $SrmServer 339 | ) 340 | 341 | $api = Get-ServerApiEndpoint $SrmServer 342 | [VMware.VimAutomation.Srm.Views.SrmCreateProtectionGroupTask] $task = $null 343 | 344 | #get root folder if this wasn't specified as a parameter 345 | if(-not $Folder) { 346 | $Folder = Get-ProtectionGroupFolder -SrmServer $SrmServer 347 | } 348 | 349 | if ($vSphereReplication) { 350 | #create list of managed object references from VM and/or VM view arrays 351 | [VMware.Vim.ManagedObjectReference[]]$moRefs = @() 352 | foreach ($vm in $VMs) { 353 | $moRefs += Get_MoRefFromVmObj -Vm $Vm 354 | } 355 | foreach ($VmView in $VMViews) { 356 | $moRefs += Get_MoRefFromVmObj -VmView $VmView 357 | } 358 | 359 | if ($pscmdlet.ShouldProcess($Name, "New")) { 360 | $task = $api.Protection.CreateHbrProtectionGroup($Folder.MoRef, $Name, $Description, $moRefs) 361 | } 362 | 363 | } elseif ($ArrayReplication) { 364 | #create list of managed object references from VM and/or VM view arrays 365 | $moRefs = @() 366 | foreach ($ds in $Datastores) { 367 | $moRefs += $ds.ExtensionData.MoRef 368 | } 369 | foreach ($DsView in $DatastoreViews) { 370 | $moRefs += $DsView.MoRef 371 | } 372 | 373 | if ($pscmdlet.ShouldProcess($Name, "New")) { 374 | $task = $api.Protection.CreateAbrProtectionGroup($Folder.MoRef, $Name, $Description, $moRefs) 375 | } 376 | 377 | } else { 378 | throw "Undetermined protection group type" 379 | } 380 | 381 | # Complete task 382 | while(-not $task.IsCreateProtectionGroupComplete()) { Start-Sleep -Seconds 1 } 383 | 384 | # Retrieve the protection group, and protect associated VMs 385 | $pg = $task.GetNewProtectionGroup() 386 | if ($pg) { 387 | $unProtectedVMs = Get-UnProtectedVM -ProtectionGroup $pg 388 | $unProtectedVMs | Protect-VM -ProtectionGroup $pg 389 | } 390 | 391 | return $pg 392 | } 393 | 394 | 395 | <# 396 | .SYNOPSIS 397 | Delete a protection group 398 | 399 | .PARAMETER ProtectionGroup 400 | The protection group to remove 401 | 402 | .PARAMETER SrmServer 403 | The SRM Server to perform the operation against 404 | #> 405 | Function Remove-ProtectionGroup { 406 | [cmdletbinding(SupportsShouldProcess=$True, ConfirmImpact="High")] 407 | [OutputType([VMware.VimAutomation.Srm.Views.RemoveProtectionGroupTask])] 408 | Param( 409 | [Parameter (Mandatory=$true, ValueFromPipeline=$true)][VMware.VimAutomation.Srm.Views.SrmProtectionGroup] $ProtectionGroup, 410 | [VMware.VimAutomation.Srm.Types.V1.SrmServer] $SrmServer 411 | ) 412 | 413 | $api = Get-ServerApiEndpoint $SrmServer 414 | [VMware.VimAutomation.Srm.Views.RemoveProtectionGroupTask] $task = $null 415 | 416 | $pginfo = $ProtectionGroup.GetInfo() 417 | if ($pscmdlet.ShouldProcess($pginfo.Name, "Remove")) { 418 | $task = $api.Protection.RemoveProtectionGroup($ProtectionGroup.MoRef) 419 | } 420 | 421 | return $task 422 | } 423 | -------------------------------------------------------------------------------- /Meadowcroft.Srm.Recovery.ps1: -------------------------------------------------------------------------------- 1 | # SRM Helper Methods - https://github.com/benmeadowcroft/SRM-Cmdlets 2 | 3 | <# 4 | .SYNOPSIS 5 | Get the subset of recovery plans matching the input criteria 6 | 7 | .PARAMETER Name 8 | Return recovery plans matching the specified name 9 | 10 | .PARAMETER ProtectionGroup 11 | Return recovery plans associated with particular protection 12 | groups 13 | #> 14 | Function Get-RecoveryPlan { 15 | [cmdletbinding()] 16 | Param( 17 | [Parameter(position=1)][string] $Name, 18 | [Parameter (ValueFromPipeline=$true)][VMware.VimAutomation.Srm.Views.SrmProtectionGroup[]] $ProtectionGroup, 19 | [VMware.VimAutomation.Srm.Types.V1.SrmServer] $SrmServer 20 | ) 21 | 22 | begin { 23 | $api = Get-ServerApiEndpoint -SrmServer $SrmServer 24 | $rps = @() 25 | } 26 | process { 27 | if ($ProtectionGroup) { 28 | foreach ($pg in $ProtectionGroup) { 29 | $rps += $pg.ListRecoveryPlans() 30 | } 31 | $rps = Select_UniqueByMoRef($rps) 32 | } else { 33 | $rps += $api.Recovery.ListPlans() 34 | } 35 | } 36 | end { 37 | $rps | ForEach-Object { 38 | $rp = $_ 39 | $rpi = $rp.GetInfo() 40 | $selected = (-not $Name -or ($Name -eq $rpi.Name)) 41 | if ($selected) { 42 | Add-Member -InputObject $rp -MemberType NoteProperty -Name "Name" -Value $rpi.Name 43 | $rp 44 | } 45 | } 46 | } 47 | } 48 | 49 | <# 50 | .SYNOPSIS 51 | Start a Recovery Plan action like test, recovery, cleanup, etc. 52 | 53 | .PARAMETER RecoveryPlan 54 | The recovery plan to start 55 | 56 | .PARAMETER RecoveryMode 57 | The recovery mode to invoke on the plan. May be one of "Test", "Cleanup", "Failover", "Migrate", "Reprotect" 58 | #> 59 | Function Start-RecoveryPlan { 60 | [cmdletbinding(SupportsShouldProcess=$True, ConfirmImpact="High")] 61 | Param( 62 | [Parameter (Mandatory=$true, ValueFromPipeline=$true, Position=1)][VMware.VimAutomation.Srm.Views.SrmRecoveryPlan] $RecoveryPlan, 63 | [VMware.VimAutomation.Srm.Views.SrmRecoveryPlanRecoveryMode] $RecoveryMode = [VMware.VimAutomation.Srm.Views.SrmRecoveryPlanRecoveryMode]::Test, 64 | [bool] $SyncData = $True 65 | ) 66 | 67 | # Validate with informative error messages 68 | $rpinfo = $RecoveryPlan.GetInfo() 69 | 70 | # Create recovery options 71 | $rpOpt = New-Object VMware.VimAutomation.Srm.Views.SrmRecoveryOptions 72 | $rpOpt.SyncData = $SyncData 73 | 74 | # Prompt the user to confirm they want to execute the action 75 | if ($pscmdlet.ShouldProcess($rpinfo.Name, $RecoveryMode)) { 76 | if ($rpinfo.State -eq 'Protecting') { 77 | throw "This recovery plan action needs to be initiated from the other SRM instance" 78 | } 79 | 80 | $RecoveryPlan.Start($RecoveryMode, $rpOpt) 81 | } 82 | } 83 | 84 | <# 85 | .SYNOPSIS 86 | Stop a running Recovery Plan action. 87 | 88 | .PARAMETER RecoveryPlan 89 | The recovery plan to stop 90 | #> 91 | Function Stop-RecoveryPlan { 92 | [cmdletbinding(SupportsShouldProcess=$True,ConfirmImpact="High")] 93 | Param( 94 | [Parameter (Mandatory=$true, ValueFromPipeline=$true, Position=1)][VMware.VimAutomation.Srm.Views.SrmRecoveryPlan] $RecoveryPlan 95 | ) 96 | 97 | # Validate with informative error messages 98 | $rpinfo = $RecoveryPlan.GetInfo() 99 | 100 | # Prompt the user to confirm they want to cancel the running action 101 | if ($pscmdlet.ShouldProcess($rpinfo.Name, 'Cancel')) { 102 | 103 | $RecoveryPlan.Cancel() 104 | } 105 | } 106 | 107 | <# 108 | .SYNOPSIS 109 | Retrieve the historical results of a recovery plan 110 | 111 | .PARAMETER RecoveryPlan 112 | The recovery plan to retrieve the history for 113 | #> 114 | Function Get-RecoveryPlanResult { 115 | [cmdletbinding()] 116 | Param( 117 | [Parameter (Mandatory=$true, ValueFromPipeline=$true, Position=1)][VMware.VimAutomation.Srm.Views.SrmRecoveryPlan] $RecoveryPlan, 118 | [VMware.VimAutomation.Srm.Views.SrmRecoveryPlanRecoveryMode] $RecoveryMode, 119 | [VMware.VimAutomation.Srm.Views.SrmRecoveryResultResultState] $ResultState, 120 | [DateTime] $StartedAfter, 121 | [DateTime] $startedBefore, 122 | [VMware.VimAutomation.Srm.Types.V1.SrmServer] $SrmServer 123 | ) 124 | 125 | $api = Get-ServerApiEndpoint -SrmServer $SrmServer 126 | 127 | # Get the history objects 128 | $history = $api.Recovery.GetHistory($RecoveryPlan.MoRef) 129 | $resultCount = $history.GetResultCount() 130 | 131 | if ($resultCount -gt 0) { 132 | $results = $history.GetRecoveryResult($resultCount) 133 | 134 | $results | 135 | Where-Object { -not $RecoveryMode -or $_.RunMode -eq $RecoveryMode } | 136 | Where-Object { -not $ResultState -or $_.ResultState -eq $ResultState } | 137 | Where-Object { $null -eq $StartedAfter -or $_.StartTime -gt $StartedAfter } | 138 | Where-Object { $null -eq $StartedBefore -or $_.StartTime -lt $StartedBefore } 139 | } 140 | } 141 | 142 | <# 143 | .SYNOPSIS 144 | Exports a recovery plan result object to XML format 145 | 146 | .PARAMETER RecoveryPlanResult 147 | The recovery plan result to export 148 | #> 149 | Function Export-RecoveryPlanResultAsXml { 150 | [cmdletbinding()] 151 | [OutputType([xml])] 152 | Param( 153 | [Parameter (Mandatory=$true, ValueFromPipeline=$true, Position=1)][VMware.VimAutomation.Srm.Views.SrmRecoveryResult] $RecoveryPlanResult, 154 | [VMware.VimAutomation.Srm.Types.V1.SrmServer] $SrmServer 155 | ) 156 | 157 | $api = Get-ServerApiEndpoint -SrmServer $SrmServer 158 | 159 | $RecoveryPlan = $RecoveryPlanResult.Plan 160 | $history = $api.Recovery.GetHistory($RecoveryPlan.MoRef) 161 | $lines = $history.GetResultLength($RecoveryPlanResult.Key) 162 | [xml] $history.RetrieveStatus($RecoveryPlanResult.Key, 0, $lines) 163 | } 164 | 165 | <# 166 | .SYNOPSIS 167 | Add a protection group to a recovery plan. This requires SRM 5.8 or later. 168 | 169 | .PARAMETER RecoveryPlan 170 | The recovery plan the protection group will be associated with 171 | 172 | .PARAMETER ProtectionGroup 173 | The protection group to associate with the recovery plan 174 | #> 175 | Function Add-ProtectionGroupToRecoveryPlan { 176 | [cmdletbinding()] 177 | Param( 178 | [Parameter (Mandatory=$true, Position=1)][VMware.VimAutomation.Srm.Views.SrmRecoveryPlan] $RecoveryPlan, 179 | [Parameter (Mandatory=$true, ValueFromPipeline=$true, Position=2)][VMware.VimAutomation.Srm.Views.SrmProtectionGroup] $ProtectionGroup 180 | ) 181 | 182 | if ($RecoveryPlan -and $ProtectionGroup) { 183 | foreach ($pg in $ProtectionGroup) { 184 | try { 185 | $RecoveryPlan.AddProtectionGroup($pg.MoRef) 186 | } catch { 187 | Write-Error $_ 188 | } 189 | } 190 | } 191 | } 192 | 193 | <# 194 | .SYNOPSIS 195 | Remove a protection group to a recovery plan. This requires SRM 6.5 or later. 196 | 197 | .PARAMETER RecoveryPlan 198 | The recovery plan the protection group will be disassociated from 199 | 200 | .PARAMETER ProtectionGroup 201 | The protection group to disassociate from the recovery plan 202 | #> 203 | Function Remove-ProtectionGroupFromRecoveryPlan { 204 | [cmdletbinding()] 205 | Param( 206 | [Parameter (Mandatory=$true)][VMware.VimAutomation.Srm.Views.SrmRecoveryPlan] $RecoveryPlan, 207 | [Parameter (Mandatory=$true)][VMware.VimAutomation.Srm.Views.SrmProtectionGroup] $ProtectionGroup 208 | ) 209 | 210 | if ($RecoveryPlan -and $ProtectionGroup) { 211 | foreach ($pg in $ProtectionGroup) { 212 | try { 213 | $RecoveryPlan.RemoveProtectionGroupFromRecoveryPlan($pg.MoRef) 214 | } catch { 215 | Write-Error $_ 216 | } 217 | } 218 | } 219 | } 220 | 221 | <# 222 | .SYNOPSIS 223 | Get the recovery settings of a protected VM. This requires SRM 5.8 or later. 224 | 225 | .PARAMETER RecoveryPlan 226 | The recovery plan the settings will be retrieved from. 227 | 228 | .PARAMETER Vm 229 | The virtual machine to retieve recovery settings for. 230 | 231 | #> 232 | Function Get-RecoverySetting { 233 | [cmdletbinding()] 234 | Param( 235 | [Parameter (Mandatory=$true)][VMware.VimAutomation.Srm.Views.SrmRecoveryPlan] $RecoveryPlan, 236 | [Parameter (ValueFromPipeline=$true)][VMware.VimAutomation.ViCore.Types.V1.Inventory.VirtualMachine] $Vm, 237 | [Parameter (ValueFromPipeline=$true)][VMware.Vim.VirtualMachine] $VmView, 238 | [Parameter (ValueFromPipeline=$true)][VMware.VimAutomation.Srm.Views.SrmProtectionGroupProtectedVm] $ProtectedVm 239 | ) 240 | 241 | $moRef = Get_MoRefFromVmObj -Vm $Vm -VmView $VmView -ProtectedVm $ProtectedVm 242 | 243 | if ($RecoveryPlan -and $moRef) { 244 | $RecoveryPlan.GetRecoverySettings($moRef) 245 | } 246 | } 247 | 248 | <# 249 | .SYNOPSIS 250 | Get the recovery settings of a protected VM. This requires SRM 5.8 or later. 251 | 252 | .PARAMETER RecoveryPlan 253 | The recovery plan the settings will be retrieved from. 254 | 255 | .PARAMETER Vm 256 | The virtual machine to configure recovery settings on. 257 | 258 | .PARAMETER RecoverySettings 259 | The recovery settings to configure. These should have been retrieved via a 260 | call to Get-RecoverySettings 261 | 262 | #> 263 | Function Set-RecoverySetting { 264 | [cmdletbinding(SupportsShouldProcess=$true, ConfirmImpact="Medium")] 265 | Param( 266 | [Parameter (Mandatory=$true)][VMware.VimAutomation.Srm.Views.SrmRecoveryPlan] $RecoveryPlan, 267 | [Parameter (ValueFromPipeline=$true)][VMware.VimAutomation.ViCore.Types.V1.Inventory.VirtualMachine] $Vm, 268 | [Parameter (ValueFromPipeline=$true)][VMware.Vim.VirtualMachine] $VmView, 269 | [Parameter (ValueFromPipeline=$true)][VMware.VimAutomation.Srm.Views.SrmProtectionGroupProtectedVm] $ProtectedVm, 270 | [Parameter (Mandatory=$true, ValueFromPipeline=$true)][VMware.VimAutomation.Srm.Views.SrmRecoverySettings] $RecoverySettings 271 | ) 272 | 273 | 274 | $moRef = Get_MoRefFromVmObj -Vm $Vm -VmView $VmView -ProtectedVm $ProtectedVm 275 | 276 | if ($RecoveryPlan -and $moRef -and $RecoverySettings) { 277 | if ($PSCmdlet.ShouldProcess("$moRef", "Set")) { 278 | $RecoveryPlan.SetRecoverySettings($moRef, $RecoverySettings) 279 | } 280 | } 281 | } 282 | 283 | <# 284 | .SYNOPSIS 285 | Create a new per-Vm command to add to the SRM Recovery Plan 286 | 287 | .PARAMETER Command 288 | The command script to execute. 289 | 290 | .PARAMETER Description 291 | The user friendly description of this script. 292 | 293 | .PARAMETER Timeout 294 | The number of seconds this command has to execute before it will be timedout. 295 | 296 | .PARAMETER RunInRecoveredVm 297 | For a post-power on command this flag determines whether it will run on the 298 | recovered VM or on the SRM server. 299 | 300 | #> 301 | Function New-Command { 302 | [cmdletbinding(SupportsShouldProcess=$true, ConfirmImpact="None")] 303 | Param( 304 | [Parameter (Mandatory=$true)][string] $Command, 305 | [Parameter (Mandatory=$true)][string] $Description, 306 | [int] $Timeout = 300, 307 | [switch] $RunInRecoveredVm = $false 308 | ) 309 | 310 | if($PSCmdlet.ShouldProcess("Description", "New")) { 311 | $srmWsdlCmd = New-Object VMware.VimAutomation.Srm.WsdlTypes.SrmCommand 312 | $srmCmd = New-Object VMware.VimAutomation.Srm.Views.SrmCommand -ArgumentList $srmWsdlCmd 313 | $srmCmd.Command = $Command 314 | $srmCmd.Description = $Description 315 | $srmCmd.RunInRecoveredVm = $RunInRecoveredVm 316 | $srmCmd.Timeout = $Timeout 317 | $srmCmd.Uuid = [guid]::NewGuid() 318 | 319 | return $srmCmd 320 | } 321 | } 322 | 323 | <# Internal function #> 324 | Function Add_Command { 325 | [cmdletbinding()] 326 | Param( 327 | [Parameter (Mandatory=$true, ValueFromPipeline=$true)][VMware.VimAutomation.Srm.Views.SrmRecoverySettings] $RecoverySettings, 328 | [Parameter (Mandatory=$true)][VMware.VimAutomation.Srm.Views.SrmCommand] $SrmCommand, 329 | [Parameter (Mandatory=$true)][bool] $PostRecovery 330 | ) 331 | 332 | if ($PostRecovery) { 333 | $commands = $RecoverySettings.PostPowerOnCallouts 334 | } else { 335 | $commands = $RecoverySettings.PrePowerOnCallouts 336 | } 337 | 338 | if (-not $commands) { 339 | $commands = New-Object System.Collections.Generic.List[VMware.VimAutomation.Srm.Views.SrmCallout] 340 | } 341 | $commands.Add($SrmCommand) 342 | 343 | if ($PostRecovery) { 344 | $RecoverySettings.PostPowerOnCallouts = $commands 345 | } else { 346 | $RecoverySettings.PrePowerOnCallouts = $commands 347 | } 348 | } 349 | 350 | <# 351 | .SYNOPSIS 352 | Add an SRM command to the set of pre recovery callouts for a VM. 353 | 354 | .PARAMETER RecoverySettings 355 | The recovery settings to update. These should have been retrieved via a 356 | call to Get-RecoverySettings 357 | 358 | .PARAMETER SrmCommand 359 | The command to add to the list. 360 | 361 | #> 362 | Function Add-PreRecoveryCommand { 363 | [cmdletbinding()] 364 | [OutputType([VMware.VimAutomation.Srm.Views.SrmRecoverySettings])] 365 | Param( 366 | [Parameter (Mandatory=$true, ValueFromPipeline=$true)][VMware.VimAutomation.Srm.Views.SrmRecoverySettings] $RecoverySettings, 367 | [Parameter (Mandatory=$true)][VMware.VimAutomation.Srm.Views.SrmCommand] $SrmCommand 368 | ) 369 | Add_Command -RecoverySettings $RecoverySettings -SrmCommand $SrmCommand -PostRecovery $false 370 | return $RecoverySettings 371 | } 372 | 373 | <# 374 | .SYNOPSIS 375 | Remove an SRM command from the set of pre recovery callouts for a VM. 376 | 377 | .PARAMETER RecoverySettings 378 | The recovery settings to update. These should have been retrieved via a 379 | call to Get-RecoverySettings 380 | 381 | .PARAMETER SrmCommand 382 | The command to remove from the list. 383 | 384 | #> 385 | Function Remove-PreRecoveryCommand { 386 | [cmdletbinding(SupportsShouldProcess=$true, ConfirmImpact="Low")] 387 | [OutputType([VMware.VimAutomation.Srm.Views.SrmRecoverySettings])] 388 | Param( 389 | [Parameter (Mandatory=$true, ValueFromPipeline=$true)][VMware.VimAutomation.Srm.Views.SrmRecoverySettings] $RecoverySettings, 390 | [Parameter (Mandatory=$true)][VMware.VimAutomation.Srm.Views.SrmCommand] $SrmCommand 391 | ) 392 | 393 | if ($pscmdlet.ShouldProcess($SrmCommand.Description, "Remove")) { 394 | $RecoverySettings.PrePowerOnCallouts.Remove($SrmCommand) 395 | } 396 | 397 | return $RecoverySettings 398 | } 399 | 400 | <# 401 | .SYNOPSIS 402 | Add an SRM command to the set of post recovery callouts for a VM. 403 | 404 | .PARAMETER RecoverySettings 405 | The recovery settings to update. These should have been retrieved via a 406 | call to Get-RecoverySettings 407 | 408 | .PARAMETER SrmCommand 409 | The command to add to the list. 410 | 411 | #> 412 | Function Add-PostRecoveryCommand { 413 | [cmdletbinding()] 414 | [OutputType([VMware.VimAutomation.Srm.Views.SrmRecoverySettings])] 415 | Param( 416 | [Parameter (Mandatory=$true, ValueFromPipeline=$true)][VMware.VimAutomation.Srm.Views.SrmRecoverySettings] $RecoverySettings, 417 | [Parameter (Mandatory=$true)][VMware.VimAutomation.Srm.Views.SrmCommand] $SrmCommand 418 | ) 419 | 420 | Add_Command -RecoverySettings $RecoverySettings -SrmCommand $SrmCommand -PostRecovery $true 421 | 422 | return $RecoverySettings 423 | } 424 | 425 | 426 | <# 427 | .SYNOPSIS 428 | Remove an SRM command from the set of post recovery callouts for a VM. 429 | 430 | .PARAMETER RecoverySettings 431 | The recovery settings to update. These should have been retrieved via a 432 | call to Get-RecoverySettings 433 | 434 | .PARAMETER SrmCommand 435 | The command to remove from the list. 436 | 437 | #> 438 | Function Remove-PostRecoveryCommand { 439 | [cmdletbinding(SupportsShouldProcess=$true, ConfirmImpact="Low")] 440 | [OutputType([VMware.VimAutomation.Srm.Views.SrmRecoverySettings])] 441 | Param( 442 | [Parameter (Mandatory=$true, ValueFromPipeline=$true)][VMware.VimAutomation.Srm.Views.SrmRecoverySettings] $RecoverySettings, 443 | [Parameter (Mandatory=$true)][VMware.VimAutomation.Srm.Views.SrmCommand] $SrmCommand 444 | ) 445 | 446 | if ($pscmdlet.ShouldProcess($SrmCommand.Description, "Remove")) { 447 | $RecoverySettings.PostPowerOnCallouts.Remove($SrmCommand) 448 | } 449 | 450 | return $RecoverySettings 451 | } 452 | 453 | 454 | <# 455 | .SYNOPSIS 456 | Create a new recovery plan 457 | 458 | .PARAMETER Name 459 | The name for this recovery plan 460 | 461 | .PARAMETER Description 462 | A description of the recovery plan 463 | 464 | .PARAMETER Folder 465 | The recovery plan folder in which to create this recovery plan. Will default to 466 | the root recovery plan folder 467 | 468 | .PARAMETER ProtectionGroups 469 | The protection groups to associate with this recovery plan 470 | 471 | .PARAMETER TestNetworkMappings 472 | The test network mappings to configure as part of this recovery plan 473 | 474 | .PARAMETER SrmServer 475 | The SRM Server to operate against 476 | #> 477 | Function New-RecoveryPlan { 478 | [cmdletbinding(SupportsShouldProcess=$true, ConfirmImpact="Medium")] 479 | Param( 480 | [Parameter (Mandatory=$true)][string] $Name, 481 | [string] $Description, 482 | [VMware.VimAutomation.Srm.Views.SrmRecoveryPlanFolder] $Folder, 483 | [VMware.VimAutomation.Srm.Views.SrmProtectionGroup[]] $ProtectionGroups, 484 | [VMware.VimAutomation.Srm.Views.SrmRecoveryTestNetworkMapping[]] $TestNetworkMappings, 485 | [VMware.VimAutomation.Srm.Types.V1.SrmServer] $SrmServer 486 | ) 487 | 488 | $api = Get-ServerApiEndpoint -SrmServer $SrmServer 489 | 490 | if (-not $Folder) { 491 | $Folder = Get-RecoveryPlanFolder -SrmServer $SrmServer 492 | } 493 | 494 | $protectionGroupmRefs += @( $ProtectionGroups | ForEach-Object { $_.MoRef } | Select-Object -Unique) 495 | 496 | [VMware.VimAutomation.Srm.Views.CreateRecoveryPlanTask] $task = $null 497 | 498 | if ($PSCmdlet.ShouldProcess($Name, "New")) { 499 | $task = $api.Recovery.CreateRecoveryPlan( 500 | $Name, 501 | $Folder.MoRef, 502 | $protectionGroupmRefs, 503 | $Description, 504 | $TestNetworkMappings 505 | ) 506 | } 507 | 508 | while(-not $task.IsCreateRecoveryPlanComplete()) { Start-Sleep -Seconds 1 } 509 | 510 | $task.GetNewRecoveryPlan() 511 | } 512 | 513 | <# 514 | .SYNOPSIS 515 | Remove a recovery plan permanently 516 | 517 | .PARAMETER RecoveryPlan 518 | The recovery plan to remove 519 | 520 | .PARAMETER SrmServer 521 | The SRM Server to operate against 522 | #> 523 | Function Remove-RecoveryPlan { 524 | [cmdletbinding(SupportsShouldProcess=$True, ConfirmImpact="High")] 525 | Param( 526 | [Parameter (Mandatory=$true)][VMware.VimAutomation.Srm.Views.SrmRecoveryPlan] $RecoveryPlan, 527 | [VMware.VimAutomation.Srm.Types.V1.SrmServer] $SrmServer 528 | ) 529 | 530 | $api = Get-ServerApiEndpoint -SrmServer $SrmServer 531 | 532 | $rpinfo = $RecoveryPlan.GetInfo() 533 | if ($pscmdlet.ShouldProcess($rpinfo.Name, "Remove")) { 534 | $api.Recovery.DeleteRecoveryPlan($RecoveryPlan.MoRef) 535 | } 536 | } 537 | 538 | <# 539 | .SYNOPSIS 540 | Get a recovery plan folder 541 | 542 | .PARAMETER SrmServer 543 | The SRM Server to query for the recovery plan folder 544 | #> 545 | Function Get-RecoveryPlanFolder { 546 | [cmdletbinding()] 547 | Param( 548 | [VMware.VimAutomation.Srm.Types.V1.SrmServer] $SrmServer 549 | ) 550 | 551 | $api = Get-ServerApiEndpoint -SrmServer $SrmServer 552 | 553 | $folder = $api.Recovery.GetRecoveryPlanRootFolder() 554 | 555 | return $folder 556 | } 557 | --------------------------------------------------------------------------------