From 5533bae319f7921c87c68b3ff6449844c943d0c2 Mon Sep 17 00:00:00 2001 From: Wes Carroll Date: Fri, 5 Apr 2019 17:18:56 -0400 Subject: [PATCH] Various fixes Removed Commit Value as it is not a vaild param Enabled ShouldProcess Ensured mandatory default values make it into the body Added Validate a valid VPG is selected. --- .../Public/Invoke-ZertoFailover.ps1 | 33 ++++++++++++------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/ZertoApiWrapper/Public/Invoke-ZertoFailover.ps1 b/ZertoApiWrapper/Public/Invoke-ZertoFailover.ps1 index 75f70cc..9665b6e 100644 --- a/ZertoApiWrapper/Public/Invoke-ZertoFailover.ps1 +++ b/ZertoApiWrapper/Public/Invoke-ZertoFailover.ps1 @@ -1,16 +1,18 @@ <# .ExternalHelp ./en-us/ZertoApiWrapper-help.xml #> function Invoke-ZertoFailover { - [cmdletbinding()] + [cmdletbinding( SupportsShouldProcess = $true )] param( [Parameter( Mandatory = $true, HelpMessage = "Name of the VPG to Failover" )] + [ValidateNotNullOrEmpty()] [string]$vpgName, [Parameter( HelpMessage = "Checkpoint Identifier to use as the Point-In-Time to rollback to." )] [Alias("checkpointId")] + [ValidateNotNullOrEmpty()] [string]$checkpointIdentifier, [Parameter( HelpMessage = "'Rollback': After the seconds specified in the commitValue setting have elapsed, the failover is rolled back. @@ -19,11 +21,7 @@ function Invoke-ZertoFailover { Default is the Site Settings setting." )] [ValidateSet("Rollback", "Commit", "None")] - [string]$commitPolicy, - [Parameter( - HelpMessage = "The amount of time in seconds the failover waits in a Before Commit state to enable checking that the failover is as required before performing the commitPolicy setting. Default is the Site Setting" - )] - [string]$commitValue, + [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. @@ -32,9 +30,9 @@ function Invoke-ZertoFailover { [ValidateSet(0, 1, 2)] [int]$shutdownPolicy = 0, [Parameter( - HelpMessage = "Time, in seconds, before VMs are forcibly turned off if the Force Shutdown option is seclected after attempting to gracefully shut down the VMs" + HelpMessage = "The amount of time in seconds the failover waits in a Before Commit state to enable checking that the failover is as required before performing the commitPolicy setting. Default is 60 Minutes (3600 Seconds)" )] - [long]$timeToWaitBeforeShutdownInSec = 300, + [int]$timeToWaitBeforeShutdownInSec = 3600, [Parameter( HelpMessage = "True: Enable reverse protection. The virtual machines are recovered on the recovery site and then protected using the default reverse protection settings. False: Do not enable reverse protection. The VPG definition is kept with the status Needs Configuration and the reverse settings in the VPG definition are not set." @@ -43,15 +41,23 @@ function Invoke-ZertoFailover { [Parameter( HelpMessage = "Name(s) of VMs in the VPG to failover" )] + [ValidateNotNullOrEmpty()] [string[]]$vmName ) begin { - $vpgId = $(Get-ZertoVpg -name $name).vpgIdentifier - $baseUri = "vpgSettings/{0}/failover" -f $vpgId + $vpgId = $(Get-ZertoVpg -name $vpgName).vpgIdentifier + if ( -not $vpgId) { + Write-Error "VPG: $vpgName Not Found. Please check the name and try again!" + break + } + $baseUri = "vpgs/{0}/failover" -f $vpgId $body = [ordered]@{} + # Setup Defaults + $body['commitpolicy'] = $commitPolicy + $body['TimeToWaitBeforeShutdownInSec'] = $timeToWaitBeforeShutdownInSec foreach ($key in $PSBoundParameters.Keys) { - if ($key -notlike 'vpgGroup' -or $key -notlike 'vmName') { + if ($key -notlike 'vpgName' -and $key -notlike 'vmName' -and $key -notlike 'WhatIf' -and $key -notlike 'TimeToWaitBeforeShutdownInSec' -and $key -notlike 'commitpolicy') { $body[$key] = $PSBoundParameters['key'] } } @@ -65,7 +71,10 @@ function Invoke-ZertoFailover { } process { - Invoke-ZertoRestRequest -uri $baseUri -body $($body | ConvertTo-Json) -method "POST" + if ($PSCmdlet.ShouldProcess("$vpgName with identifier $vpgId and these options $($body | convertto-json)")) { + Invoke-ZertoRestRequest -uri $baseUri -body $($body | ConvertTo-Json) -method "POST" + } + } end {