From 68ad1e9443bdc1fe7826d046a8bb8c64edf6ac81 Mon Sep 17 00:00:00 2001 From: Wes Carroll Date: Thu, 4 Apr 2019 20:34:15 -0400 Subject: [PATCH] Add Settings Validation and ShouldProcess --- ZertoApiWrapper/Public/New-ZertoVpg.ps1 | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/ZertoApiWrapper/Public/New-ZertoVpg.ps1 b/ZertoApiWrapper/Public/New-ZertoVpg.ps1 index 020a309..48c96b3 100644 --- a/ZertoApiWrapper/Public/New-ZertoVpg.ps1 +++ b/ZertoApiWrapper/Public/New-ZertoVpg.ps1 @@ -1,6 +1,6 @@ <# .ExternalHelp ./en-us/ZertoApiWrapper-help.xml #> function New-ZertoVpg { - [cmdletbinding()] + [cmdletbinding(SupportsShouldProcess = $true)] param( [Parameter( HelpMessage = "Name of the VPG", @@ -226,6 +226,24 @@ function New-ZertoVpg { if (($journalWarningThresholdInMb -eq 0) -or ($journalWarningThresholdInMb -gt $journalHardLimitInMb)) { $journalWarningThresholdInMb = $journalHardLimitInMb * .75 } + + #Validate all items in the hashtable are populated with valid data. + $validSettings = $true + foreach ($item in $identifiersTable.GetEnumerator()) { + if ([String]::IsNullOrEmpty($item.value)) { + $validSettings = $false + Write-Error "$($item.key) is not associated with a valid identifier. Please check the submitted values and try again." + } + } + if ($vmIdentifiers.count -eq 0) { + $validSettings = $false + Write-Error "No valid VM names were passed or all passed VMs are already protected and cannot be further protected." + } + + if ( -not $validSettings ) { + Write-Error "One or more parameters passed do not have valid identifiers or 0 valid VMs were found. Please check your settings and try again." + Break + } } process { @@ -295,7 +313,9 @@ function New-ZertoVpg { $baseSettings.Journal.Limitation.HardLimitInMB = $journalHardLimitInMb $baseSettings.Journal.Limitation.WarningThresholdInMB = $journalWarningThresholdInMb $settingsURI = "{0}/{1}" -f $baseUri, $vpgSettingsIdentifier - Invoke-ZertoRestRequest -uri $settingsURI -body $($baseSettings | ConvertTo-Json -Depth 10) -method "PUT" | Out-Null + if ($PSCmdlet.ShouldProcess($($baseSettings | ConvertTo-Json -Depth 10))) { + $results = Invoke-ZertoRestRequest -uri $settingsURI -body $($baseSettings | ConvertTo-Json -Depth 10) -method "PUT" + } } end {