From 5984bc93c9a8f7cfa5030361cb3e27485ebe04ec Mon Sep 17 00:00:00 2001 From: Wes Carroll Date: Sat, 12 Oct 2019 22:26:13 -0400 Subject: [PATCH] Create Copy-ZertoVpg Function --- ZertoApiWrapper/Public/Copy-ZertoVpg.ps1 | 49 ++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 ZertoApiWrapper/Public/Copy-ZertoVpg.ps1 diff --git a/ZertoApiWrapper/Public/Copy-ZertoVpg.ps1 b/ZertoApiWrapper/Public/Copy-ZertoVpg.ps1 new file mode 100644 index 0000000..67ef11c --- /dev/null +++ b/ZertoApiWrapper/Public/Copy-ZertoVpg.ps1 @@ -0,0 +1,49 @@ +function Copy-ZertoVpg { + [CmdletBinding()] + param ( + # VPG Name to Clone + [Parameter(Mandatory, + HelpMessage = "Name of the VPG to clone")] + [ValidateNotNullOrEmpty()] + [String]$VpgName, + # Name of VMs to add to the VPG + [Parameter(Mandatory, + HelpMessage = "Name(s) of the Virtual Machine(s) to add to the VPG")] + [ValidateNotNullOrEmpty()] + [String[]]$VMs + ) + + begin { + + } + + process { + $VpgIdToCopy = @{ VpgIdentifier = (Get-ZertoVpg -vpgName $VpgName).vpgIdentifier } + if ( $null -eq $VpgIdToCopy.VpgIdentifier ) { + Write-Error -Message "Unable to find a VPG with the name: $VpgName. Please check the name and try again." + Break + } elseif ($VpgIdToCopy.VpgIdentifier.Count -gt 1) { + Write-Error -Message "More than one VPG was returned when searching for the VPG name: $VpgName. Please try again." + Break + } + $BaseUri = "vpgSettings/copyVpgSettings" + $UnprotectedVms = Get-ZertoUnprotectedVm + $ProtectedVms = Get-ZertoProtectedVm + $VMsToAdd = foreach ($VM in $VMs) { + if ($UnprotectedVms.VmName -contains $VM) { + $UnprotectedVms | Where-Object { $_.VmName -like $VM } | Select-Object -ExpandProperty VmIdentifier + } elseif ($ProtectedVms.VmName -contains $VM) { + $ProtectedVms | Where-Object { $_.VmName -like $VM } | Select-Object -ExpandProperty VmIdentifier + } else { + Write-Warning -Message "Unable to find VM with Name $VM. Skipping." + } + } + $NewVpgId = Invoke-ZertoRestRequest -Uri $BaseUri -body ($VpgIdToCopy | ConvertTo-Json) -Method "POST" + $Uri = "{0}/{1}/vms" -f $BaseUri, $NewVpgId.VpgIdentifier + Invoke-ZertoRestRequest -Uri $Uri -Body ($VMsToAdd | ConvertTo-Json) -Method "PUT" + } + + end { + + } +}