From 2e4cd68b5e6bb226b2d227fa481dbcd9bebca3ff Mon Sep 17 00:00:00 2001 From: Wes Carroll Date: Mon, 15 Apr 2019 17:20:17 -0400 Subject: [PATCH] Update Body Build Logic --- .../Public/Invoke-ZertoFailover.ps1 | 52 +++++++++++-------- 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/ZertoApiWrapper/Public/Invoke-ZertoFailover.ps1 b/ZertoApiWrapper/Public/Invoke-ZertoFailover.ps1 index cf1431d..5db8223 100644 --- a/ZertoApiWrapper/Public/Invoke-ZertoFailover.ps1 +++ b/ZertoApiWrapper/Public/Invoke-ZertoFailover.ps1 @@ -24,9 +24,9 @@ function Invoke-ZertoFailover { [ValidateSet("Rollback", "Commit", "None")] [string]$commitPolicy = "Rollback", [Parameter( - HelpMessage = "0: The protected virtual machines are not touched before starting the failover. This assumes that you do not have access to the protected virtual machines. -- DEFAULT - 1: If the protected virtual machines have VMware Tools or Microsoft Integration Services available, the virtual machines are gracefully shut down, otherwise the failover operation fails. This is similar to performing a Move operation to a specified checkpoint. - 2: The protected virtual machines are forcibly shut down before starting the failover. If the protected virtual machines have VMware Tools or Microsoft Integration Services available, the procedure waits five minutes for the virtual machines to be gracefully shut down before forcibly powering them off. This is similar to performing a Move operation to a specified checkpoint." + HelpMessage = "0: The protected virtual machines are not touched before starting the failover. -- DEFAULT + 1: If the protected virtual machines have VMware Tools or Microsoft Integration Services available, the virtual machines are gracefully shut down, otherwise the failover operation fails. This is similar to performing a Move operation to a specified checkpoint. This assumes that you do not have access to the protected virtual machines. + 2: The protected virtual machines are forcibly shut down before starting the failover. If the protected virtual machines have VMware Tools or Microsoft Integration Services available, the procedure waits five minutes for the virtual machines to be gracefully shut down before forcibly powering them off. This is similar to performing a Move operation to a specified checkpoint. This assumes that you do not have access to the protected virtual machines" )] [ValidateSet(0, 1, 2)] [int]$shutdownPolicy = 0, @@ -52,29 +52,39 @@ function Invoke-ZertoFailover { Write-Error "VPG: $vpgName Not Found. Please check the name and try again!" -ErrorAction Stop } $baseUri = "vpgs/{0}/failover" -f $vpgId - $body = [ordered]@{} - # Setup Defaults + $body = @{} + # Setup Required Defaults $body['commitpolicy'] = $commitPolicy $body['TimeToWaitBeforeShutdownInSec'] = $timeToWaitBeforeShutdownInSec - foreach ($param in $PSBoundParameters.GetEnumerator()) { - if ($param.key -notlike 'vpgName' -and $param.key -notlike 'vmName' -and $param.key -notlike 'WhatIf' -and $param.key -notlike 'TimeToWaitBeforeShutdownInSec' -and $param.key -notlike 'commitpolicy') { - $body[$param.key] = $param.value + + Switch ($PSBoundParameters.Keys) { + "checkpointIdentifier" { + $body['checkpointIdentifier'] = $checkpointIdentifier } - } - if ($PSBoundParameters.ContainsKey('vmName')) { - $vpgVmInformation = Get-ZertoProtectedVm -vpgName $vpgName - [System.Collections.ArrayList]$vmIdentifiers = @() - foreach ( $name in $vmName ) { - $selectedVm = $vpgVmInformation | Where-Object {$_.VmName.toLower() -eq $name.toLower()} - if ($null -eq $selectedVm) { - Write-Error "VM: $name NOT found in VPG $vpgName. Check the name and try again." -ErrorAction Stop - } elseif ($vmIdentifiers.Contains($selectedVm.vmIdentifier.toString())) { - Write-Error "VM: $($selectedVm.VmName) specified more than once. Please check parameters and try again." -ErrorAction Stop - } else { - $vmIdentifiers.Add($selectedVm.vmIdentifier.toString()) | Out-Null + + "shutdownPolicy" { + $body['shutdownPolicy'] = $shutdownPolicy + } + + "reverseProtection" { + $body['reverseProtection'] = $reverseProtection + } + + "vmName" { + $vpgVmInformation = Get-ZertoProtectedVm -vpgName $vpgName + [System.Collections.ArrayList]$vmIdentifiers = @() + foreach ( $name in $vmName ) { + $selectedVm = $vpgVmInformation | Where-Object {$_.VmName.toLower() -eq $name.toLower()} + if ($null -eq $selectedVm) { + Write-Error "VM: $name NOT found in VPG $vpgName. Check the name and try again." -ErrorAction Stop + } elseif ($vmIdentifiers.Contains($selectedVm.vmIdentifier.toString())) { + Write-Error "VM: $($selectedVm.VmName) specified more than once. Please check parameters and try again." -ErrorAction Stop + } else { + $vmIdentifiers.Add($selectedVm.vmIdentifier.toString()) | Out-Null + } } + $body['VmIdentifiers'] = $vmIdentifiers } - $body['VmIdentifiers'] = $vmIdentifiers } }