Fix AddVM Logic

This commit is contained in:
Wes Carroll
2019-11-07 17:43:18 -05:00
parent 63d56cf862
commit 5f978d7b1a
+27 -11
View File
@@ -1,12 +1,17 @@
<# .ExternalHelp ./en-us/ZertoApiWrapper-help.xml #>
function Copy-ZertoVpg { function Copy-ZertoVpg {
[CmdletBinding()] [CmdletBinding(SupportsShouldProcess)]
param ( param (
# VPG Name to Clone # VPG Name to Clone
[Parameter(Mandatory, [Parameter(Mandatory,
HelpMessage = "Name of the VPG to clone")] HelpMessage = "Name of the VPG to clone")]
[ValidateNotNullOrEmpty()] [ValidateNotNullOrEmpty()]
[String]$VpgName, [String]$SourceVpgName,
# Name of VMs to add to the VPG # New VPG Name
[Parameter(Mandatory,
HelpMessage = "Name to assign the newly created VPG")]
[ValidateNotNullOrEmpty()]
[String]$NewVpgName, # Name of VMs to add to the VPG
[Parameter(Mandatory, [Parameter(Mandatory,
HelpMessage = "Name(s) of the Virtual Machine(s) to add to the VPG")] HelpMessage = "Name(s) of the Virtual Machine(s) to add to the VPG")]
[ValidateNotNullOrEmpty()] [ValidateNotNullOrEmpty()]
@@ -18,12 +23,12 @@ function Copy-ZertoVpg {
} }
process { process {
$VpgIdToCopy = @{ VpgIdentifier = (Get-ZertoVpg -vpgName $VpgName).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: $VpgName. Please check the name and try again." Write-Error -Message "Unable to find a VPG with the name: $SourceVpgName. Please check the name and try again."
Break Break
} elseif ($VpgIdToCopy.VpgIdentifier.Count -gt 1) { } 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." Write-Error -Message "More than one VPG was returned when searching for the VPG name: $SourceVpgName. Please try again."
Break Break
} }
$BaseUri = "vpgSettings/copyVpgSettings" $BaseUri = "vpgSettings/copyVpgSettings"
@@ -31,16 +36,27 @@ function Copy-ZertoVpg {
$ProtectedVms = Get-ZertoProtectedVm $ProtectedVms = Get-ZertoProtectedVm
$VMsToAdd = foreach ($VM in $VMs) { $VMsToAdd = foreach ($VM in $VMs) {
if ($UnprotectedVms.VmName -contains $VM) { if ($UnprotectedVms.VmName -contains $VM) {
$UnprotectedVms | Where-Object { $_.VmName -like $VM } | Select-Object -ExpandProperty VmIdentifier $VmId = $UnprotectedVms | Where-Object { $_.VmName -like $VM } | Select-Object -ExpandProperty VmIdentifier
} elseif ($ProtectedVms.VmName -contains $VM) { } elseif ($ProtectedVms.VmName -contains $VM) {
$ProtectedVms | Where-Object { $_.VmName -like $VM } | Select-Object -ExpandProperty VmIdentifier $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
} }
$NewVpgId = Invoke-ZertoRestRequest -Uri $BaseUri -body ($VpgIdToCopy | ConvertTo-Json) -Method "POST" if ($PSCmdlet.ShouldProcess("$VMsToAdd", "Copying $SourceVpgName to $NewVpgName with Settings")) {
$Uri = "{0}/{1}/vms" -f $BaseUri, $NewVpgId.VpgIdentifier $NewVpgId = Invoke-ZertoRestRequest -Uri $BaseUri -Body ($VpgIdToCopy | ConvertTo-Json) -Method "POST"
Invoke-ZertoRestRequest -Uri $Uri -Body ($VMsToAdd | ConvertTo-Json) -Method "PUT" $Uri = "{0}/{1}/vms" -f "vpgSettings", $NewVpgId
$null = Invoke-ZertoRestRequest -Uri $Uri -Body ($VMsToAdd | ConvertTo-Json) -Method "POST"
$Uri = "vpgSettings/{0}" -f $NewVpgId
$CurrentSettings = Invoke-ZertoRestRequest -Uri $Uri
$CurrentSettings.Basic.Name = $NewVpgName
$Null = Invoke-ZertoRestRequest -Uri $Uri -Method "Put" -Body $($CurrentSettings | ConvertTo-Json -Depth 20)
Save-ZertoVpgSetting -vpgSettingsIdentifier $NewVpgId
}
} }
end { end {