Refactor Function

This commit is contained in:
Wes Carroll
2020-04-14 09:26:22 -04:00
parent fe4d8cf4dd
commit 9913c96d65
+8 -16
View File
@@ -25,32 +25,25 @@ function Copy-ZertoVpg {
process { process {
$VpgIdToCopy = @{ VpgIdentifier = (Get-ZertoVpg -vpgName $SourceVpgName).vpgIdentifier } $VpgIdToCopy = @{ VpgIdentifier = (Get-ZertoVpg -vpgName $SourceVpgName).vpgIdentifier }
if ( $null -eq $VpgIdToCopy.VpgIdentifier ) { if ( $null -eq $VpgIdToCopy.VpgIdentifier ) {
Write-Error -Message "Unable to find a VPG with the name: $SourceVpgName. Please check the name and try again." Throw "Unable to find a VPG with the name: $SourceVpgName. 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: $SourceVpgName. Please try again."
Break
} }
$BaseUri = "vpgSettings/copyVpgSettings" $BaseUri = "vpgSettings/copyVpgSettings"
$UnprotectedVms = Get-ZertoUnprotectedVm $VmsMap = Get-Map -InputObject (Get-ZertoUnprotectedVm) -Key 'VmName' -Value 'VmIdentifier'
$ProtectedVms = Get-ZertoProtectedVm $VmsMap += Get-Map -InputObject (Get-ZertoProtectedVm) -Key 'VmName' -Value 'VmIdentifier'
$VMsToAdd = foreach ($VM in $VMs) { $VMsToAdd = foreach ($VM in $VMs) {
if ($UnprotectedVms.VmName -contains $VM) { if ($VmsMap.Keys -contains $VM) {
$VmId = $UnprotectedVms | Where-Object { $_.VmName -like $VM } | Select-Object -ExpandProperty VmIdentifier [PSCustomObject]@{
} elseif ($ProtectedVms.VmName -contains $VM) { VmIdentifier = $VmsMap[$VM]
$VmId = $ProtectedVms | Where-Object { $_.VmName -like $VM } | Select-Object -ExpandProperty VmIdentifier }
} else { } else {
Write-Warning -Message "Unable to find VM with Name $VM. Skipping." Write-Warning -Message "Unable to find VM with Name $VM. Skipping."
} }
$returnObject = New-Object PSObject
$returnObject | Add-Member -MemberType NoteProperty -Name "VmIdentifier" -Value $VmId
$returnObject
} }
if ($PSCmdlet.ShouldProcess("$VMsToAdd", "Copying $SourceVpgName to $NewVpgName with Settings")) { if ($PSCmdlet.ShouldProcess("$VMsToAdd", "Copying $SourceVpgName to $NewVpgName with Settings")) {
$NewVpgId = Invoke-ZertoRestRequest -Uri $BaseUri -Body ($VpgIdToCopy | ConvertTo-Json) -Method "POST" $NewVpgId = Invoke-ZertoRestRequest -Uri $BaseUri -Body ($VpgIdToCopy | ConvertTo-Json) -Method "POST"
$Uri = "{0}/{1}/vms" -f "vpgSettings", $NewVpgId $Uri = "{0}/{1}/vms" -f "vpgSettings", $NewVpgId
foreach ($VM in $VMsToAdd) { foreach ($VM in $VMsToAdd) {
$null = Invoke-ZertoRestRequest -Uri $Uri -Body ($VM | ConvertTo-Json) -Method "POST" $null = Invoke-ZertoRestRequest -Uri $Uri -Body ($VM | ConvertTo-Json) -Method "POST" -ErrorAction Stop
} }
$Uri = "vpgSettings/{0}" -f $NewVpgId $Uri = "vpgSettings/{0}" -f $NewVpgId
$CurrentSettings = Invoke-ZertoRestRequest -Uri $Uri $CurrentSettings = Invoke-ZertoRestRequest -Uri $Uri
@@ -58,7 +51,6 @@ function Copy-ZertoVpg {
$Null = Invoke-ZertoRestRequest -Uri $Uri -Method "Put" -Body $($CurrentSettings | ConvertTo-Json -Depth 20) $Null = Invoke-ZertoRestRequest -Uri $Uri -Method "Put" -Body $($CurrentSettings | ConvertTo-Json -Depth 20)
Save-ZertoVpgSetting -vpgSettingsIdentifier $NewVpgId Save-ZertoVpgSetting -vpgSettingsIdentifier $NewVpgId
} }
} }
end { end {