From b6d8083b69c31954d876490a446410114aa6b159 Mon Sep 17 00:00:00 2001 From: Wes Carroll Date: Tue, 4 Aug 2020 22:05:35 -0400 Subject: [PATCH] Remove-ZertoVpgVm update --- ZertoApiWrapper/Public/Remove-ZertoVpgVm.ps1 | 102 ++++--------------- 1 file changed, 21 insertions(+), 81 deletions(-) diff --git a/ZertoApiWrapper/Public/Remove-ZertoVpgVm.ps1 b/ZertoApiWrapper/Public/Remove-ZertoVpgVm.ps1 index b34737a..f0e0b33 100644 --- a/ZertoApiWrapper/Public/Remove-ZertoVpgVm.ps1 +++ b/ZertoApiWrapper/Public/Remove-ZertoVpgVm.ps1 @@ -1,18 +1,7 @@ <# .ExternalHelp ./en-us/ZertoApiWrapper-help.xml #> function Remove-ZertoVpgVm { - [CmdletBinding(SupportsShouldProcess, DefaultParameterSetName = "VpgName")] + [CmdletBinding(SupportsShouldProcess, ConfirmImpact = "High")] param ( - [Parameter( - Mandatory, - HelpMessage = "Vpg Settings Identifier", - ValueFromPipeline, - ValueFromPipelineByPropertyName, - ValueFromRemainingArguments, - ParameterSetName = "VpgSettingsIdentifier" - )] - [ValidateNotNullOrEmpty()] - [Alias("sid", "settingsIdentifier", "vpgSettingsId")] - [String]$vpgSettingsIdentifier, [Parameter( Mandatory, HelpMessage = "Name of the VPG that contains the VM you wish to remove", @@ -33,78 +22,29 @@ function Remove-ZertoVpgVm { } process { - switch ($PSCmdlet.ParameterSetName) { - "VpgName" { - $VpgData = Get-ZertoVpg -vpgName $VpgName - if (-not $VpgData) { - Write-Error "Unable to find Vpg with name $VpgName. Please check your parameters and try again." -ErrorAction Stop - } else { - $protectedVms = Get-ZertoProtectedVm -vpgName $VpgData.VpgName - $vpgSettingsIdentifier = New-ZertoVpgSettingsIdentifier -vpgIdentifier $VpgIdentifier - } - } - Default {} - } - - if ($PSCmdlet.ParameterSetName -eq "VpgName") { - - - } - $baseUrl = "vpgsettings/{0}/vms" -f $vpgSettingsIdentifier - $baseSettings = Get-ZertoVpgSetting -vpgSettingsIdentifier $vpgSettingsIdentifier - if ($PSCmdlet.ParameterSetName -eq "VpgSettingsIdentifier") { - $VpgName = $baseSettings.Basic.Name - } - $unprotectedVms = Get-ZertoUnprotectedVm - $protectedVms = Get-ZertoProtectedVm - $vmMap = Get-Map -inputObject $unprotectedVms -key VmName -value VmIdentifier - $vmMap = $vmMap + (Get-Map -inputObject $protectedVms -key VmName -value VmIdentifier) - # Create array of VM identifiers - $vmIdentifiers = foreach ($machine in ($Vm | Select-Object -Unique)) { - if ($vmMap[$machine] -notin $baseSettings.Vms.vmIdentifier ) { - # If the VM is unprotected, get the identifier - $vmIdentifier = $unprotectedVms | Where-Object { $_.vmName -like $machine } | Select-Object -ExpandProperty vmIdentifier - # If the VM is not unprotected, check the protected VMs - if ( -not $vmIdentifier) { - # Get all identifiers to test if the VM is eligible to be a member of an additional VPG - $results = $protectedVms | Where-Object { $_.VmName -like $machine } | Select-Object -ExpandProperty vmIdentifier - $recoverySiteIdentifiers = $protectedVms | Where-Object { $_.VmName -like $machine } | Select-Object -ExpandProperty RecoverySite | Select-Object -ExpandProperty identifier - # If VM is currently a member of 3 VPGs, skip it. If it cannot be found, skip it. Otherwise, set the identifier - if ($baseSettings.basic.RecoverySiteIdentifier -in $recoverySiteIdentifiers) { - Write-Warning "$machine is already replicating to target site. It cannot be added to an additional VPG replicating to that site. Please check your configurations and try again. Skipping $machine" - continue - } elseif ($results.count -eq 3) { - Write-Warning "$machine is already a part of 3 VPGs and cannot be part of an additional VPG. Skipping $machine" - continue - } elseif ($results.count -eq 0) { - Write-Warning "$machine not found. Skipping $machine" - continue - } else { - $vmIdentifier = $results | Select-Object -First 1 - } - } - # Create a custom object to store the information to easily convert to JSON. Return to vmIdentifiers array. - $vmIdentifier - } else { - Write-Warning "$machine is already a member of this VPG Settings object. It will not be added again. Skipping $machine" - continue - } - } - if ($vmIdentifiers.Count -gt 0 -and $PSCmdlet.ShouldProcess($VmIdentifiers, "Adding VM(s): $Vm to Vpg $VpgName")) { - foreach ($id in $VmIdentifiers) { - # Build the Body - $Body = @{VmIdentifier = $id } - # Submit the request. Out to Null to prevent line returns while running. - $null = Invoke-ZertoRestRequest -uri $baseUrl -method POST -body ($Body | ConvertTo-Json -Depth 10) - } - - $vpgSettingsIdentifier + $VpgData = Get-ZertoVpg -vpgName $VpgName + if (-not $VpgData) { + Write-Error "Unable to find Vpg with name $VpgName. Please check your parameters and try again." -ErrorAction Stop } else { - Write-Warning "No VMs found to add. Please check your parameters and try again." - if ($PSCmdlet.ParameterSetName -eq "VpgName") { - Remove-ZertoVpgSettingsIdentifier -vpgSettingsIdentifier $vpgSettingsIdentifier + $protectedVms = Get-ZertoProtectedVm -vpgName $VpgData.VpgName + } + $VmIdentifiers = foreach ($machine in ($vm | Select-Object -Unique)) { + if ($machine -in $protectedVms.VmName) { + $protectedVms.VmName.Where( { $_.VmName -like $machine }) | Select-Object -ExpandProperty VmIdentifier + } else { + Write-Warning "$machine is not found in $VpgName. Check your parameters. Skipping $machine" } } + if ($VmIdentifiers.Count -gt 0 -and $PSCmdlet.ShouldProcess(($Vm | Select-Object -Unique), "Removing VM(s): $($Vm | Select-Object -Unique) from Vpg $VpgName")) { + $vpgSettingsIdentifier = New-ZertoVpgSettingsIdentifier -vpgIdentifier $VpgData.VpgIdentifier + foreach ($identifier in $VmIdentifiers) { + $url = "vpgSettings/{0}/vms/{1}" -f $vpgSettingsIdentifier, $identifier + Invoke-ZertoRestRequest -uri $uri -method DELETE + } + Save-ZertoVpgSetting -vpgSettingsIdentifier $vpgSettingsIdentifier + } else { + Write-Warning "No VMs found to remove. Please check your parameters and try again." + } } end {