diff --git a/ZertoApiWrapper/Public/Edit-ZertoVra.ps1 b/ZertoApiWrapper/Public/Edit-ZertoVra.ps1 index 0b5ef84..acf1693 100644 --- a/ZertoApiWrapper/Public/Edit-ZertoVra.ps1 +++ b/ZertoApiWrapper/Public/Edit-ZertoVra.ps1 @@ -38,6 +38,9 @@ function Edit-ZertoVra { $baseUri = "vras/{0}" -f $vraIdentifier # Get the current VRA information for use if an updated parameter is not supplied $vra = Get-ZertoVra -vraIdentifier $vraIdentifier + if ( -not $vra ) { + Write-Error "VRA with Identifier: $vraIdentifier could not be found. Please check the ID and try again." + } } process { diff --git a/ZertoApiWrapper/Public/Invoke-ZertoFailover.ps1 b/ZertoApiWrapper/Public/Invoke-ZertoFailover.ps1 index bc8aae6..f266a8a 100644 --- a/ZertoApiWrapper/Public/Invoke-ZertoFailover.ps1 +++ b/ZertoApiWrapper/Public/Invoke-ZertoFailover.ps1 @@ -2,6 +2,7 @@ function Invoke-ZertoFailover { [cmdletbinding( SupportsShouldProcess = $true )] param( + #TODO - Refactor? [Parameter( Mandatory = $true, HelpMessage = "Name of the VPG to Failover" diff --git a/ZertoApiWrapper/Public/Invoke-ZertoFailoverCommit.ps1 b/ZertoApiWrapper/Public/Invoke-ZertoFailoverCommit.ps1 index 05f40b5..5d87c56 100644 --- a/ZertoApiWrapper/Public/Invoke-ZertoFailoverCommit.ps1 +++ b/ZertoApiWrapper/Public/Invoke-ZertoFailoverCommit.ps1 @@ -25,8 +25,12 @@ function Invoke-ZertoFailoverCommit { process { foreach ($name in $vpgName) { $vpgId = $(Get-ZertoVpg -name $name).vpgIdentifier - $uri = "{0}/{1}/FailoverCommit" -f $baseUri, $vpgId - Invoke-ZertoRestRequest -uri $uri -body $($body | convertto-json) -method "POST" + if ( -not $vpgId ) { + Write-Error "VPG: $name could not be found. Please check the name and try again. Skipping." + } else { + $uri = "{0}/{1}/FailoverCommit" -f $baseUri, $vpgId + Invoke-ZertoRestRequest -uri $uri -body $($body | convertto-json) -method "POST" + } } } diff --git a/ZertoApiWrapper/Public/Invoke-ZertoFailoverRollback.ps1 b/ZertoApiWrapper/Public/Invoke-ZertoFailoverRollback.ps1 index e6bc6cb..7ff5c3d 100644 --- a/ZertoApiWrapper/Public/Invoke-ZertoFailoverRollback.ps1 +++ b/ZertoApiWrapper/Public/Invoke-ZertoFailoverRollback.ps1 @@ -16,8 +16,12 @@ function Invoke-ZertoFailoverRollback { process { foreach ($name in $vpgName) { $vpgId = $(Get-ZertoVpg -name $name).vpgIdentifier - $uri = "{0}/{1}/FailoverRollback" -f $baseUri, $vpgId - Invoke-ZertoRestRequest -uri $uri -method "POST" + if ( -not $vpgId ) { + Write-Error "VPG: $name not found. Please check the name and try again. Skipping" + } else { + $uri = "{0}/{1}/FailoverRollback" -f $baseUri, $vpgId + Invoke-ZertoRestRequest -uri $uri -method "POST" + } } } diff --git a/ZertoApiWrapper/Public/Invoke-ZertoForceSync.ps1 b/ZertoApiWrapper/Public/Invoke-ZertoForceSync.ps1 index 5dac2eb..2e75c96 100644 --- a/ZertoApiWrapper/Public/Invoke-ZertoForceSync.ps1 +++ b/ZertoApiWrapper/Public/Invoke-ZertoForceSync.ps1 @@ -16,8 +16,12 @@ function Invoke-ZertoForceSync { process { foreach ($name in $vpgName) { $id = $(Get-ZertoVpg -name $name).vpgIdentifier - $uri = "{0}/{1}/forcesync" -f $baseUri, $id - Invoke-ZertoRestRequest -uri $uri -method "POST" + if ( -not $id ) { + Write-Error "VPG: $name not found. Please check the name and try again. Skipping" + } else { + $uri = "{0}/{1}/forcesync" -f $baseUri, $id + Invoke-ZertoRestRequest -uri $uri -method "POST" + } } } diff --git a/ZertoApiWrapper/Public/Invoke-ZertoMove.ps1 b/ZertoApiWrapper/Public/Invoke-ZertoMove.ps1 index 1007b5d..7166225 100644 --- a/ZertoApiWrapper/Public/Invoke-ZertoMove.ps1 +++ b/ZertoApiWrapper/Public/Invoke-ZertoMove.ps1 @@ -49,6 +49,7 @@ function Invoke-ZertoMove { begin { $baseUri = "vpgs" $body = [ordered]@{} + #TODO - use a foreach loop to populate the body without all the if statments if ($PSBoundParameters.ContainsKey('commitPolicy')) { $body['commitPolicy'] = $commitPolicy } @@ -72,8 +73,12 @@ function Invoke-ZertoMove { process { foreach ($name in $vpgName) { $vpgId = $(Get-ZertoVpg -name $name).vpgIdentifier - $uri = "{0}/{1}/move" -f $baseUri, $vpgId - Invoke-ZertoRestRequest -uri $uri -method "POST" -body $($body | ConvertTo-Json) + if ( -not $vpgId ) { + Write-Error "VPG: $name not found. Please check the name and try again. Skipping" + } else { + $uri = "{0}/{1}/move" -f $baseUri, $vpgId + Invoke-ZertoRestRequest -uri $uri -method "POST" -body $($body | ConvertTo-Json) + } } } diff --git a/ZertoApiWrapper/Public/Invoke-ZertoMoveCommit.ps1 b/ZertoApiWrapper/Public/Invoke-ZertoMoveCommit.ps1 index eedb889..59ee712 100644 --- a/ZertoApiWrapper/Public/Invoke-ZertoMoveCommit.ps1 +++ b/ZertoApiWrapper/Public/Invoke-ZertoMoveCommit.ps1 @@ -29,8 +29,12 @@ function Invoke-ZertoMoveCommit { process { foreach ($name in $vpgName) { $vpgId = $(Get-ZertoVpg -name $name).vpgIdentifier - $uri = "{0}/{1}/MoveCommit" -f $baseUri, $vpgId - Invoke-ZertoRestRequest -uri $uri -body $($body | convertto-json) -method "POST" + if ( -not $vpgId ) { + Write-Error "VPG: $name not found. Please check the name and try again. Skipping." + } else { + $uri = "{0}/{1}/MoveCommit" -f $baseUri, $vpgId + Invoke-ZertoRestRequest -uri $uri -body $($body | convertto-json) -method "POST" + } } } diff --git a/ZertoApiWrapper/Public/Invoke-ZertoMoveRollback.ps1 b/ZertoApiWrapper/Public/Invoke-ZertoMoveRollback.ps1 index d51ecc5..d3c11ff 100644 --- a/ZertoApiWrapper/Public/Invoke-ZertoMoveRollback.ps1 +++ b/ZertoApiWrapper/Public/Invoke-ZertoMoveRollback.ps1 @@ -16,8 +16,12 @@ function Invoke-ZertoMoveRollback { process { foreach ($name in $vpgName) { $id = $(Get-ZertoVpg -name $name).vpgIdentifier - $uri = "{0}/{1}/moveRollBack" -f $baseUri, $id - Invoke-ZertoRestRequest -uri $uri -method "POST" + if ( -not $id ) { + Write-Error "VPG: $name not found. Please check the name and try again." + } else { + $uri = "{0}/{1}/moveRollBack" -f $baseUri, $id + Invoke-ZertoRestRequest -uri $uri -method "POST" + } } } diff --git a/ZertoApiWrapper/Public/New-ZertoVpg.ps1 b/ZertoApiWrapper/Public/New-ZertoVpg.ps1 index 48c96b3..21ea609 100644 --- a/ZertoApiWrapper/Public/New-ZertoVpg.ps1 +++ b/ZertoApiWrapper/Public/New-ZertoVpg.ps1 @@ -241,8 +241,7 @@ function New-ZertoVpg { } 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 + 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." -ErrorAction Stop } } diff --git a/ZertoApiWrapper/Public/New-ZertoVpgSettingsIdentifier.ps1 b/ZertoApiWrapper/Public/New-ZertoVpgSettingsIdentifier.ps1 index 9925d30..cd52e5b 100644 --- a/ZertoApiWrapper/Public/New-ZertoVpgSettingsIdentifier.ps1 +++ b/ZertoApiWrapper/Public/New-ZertoVpgSettingsIdentifier.ps1 @@ -36,10 +36,9 @@ function New-ZertoVpgSettingsIdentifier { if ($PSCmdlet.ShouldProcess("Creating VPG Settings Object")) { Invoke-ZertoRestRequest -uri $baseUri -body $body -Method "POST" } - } end { - + #Nothing to do } } diff --git a/ZertoApiWrapper/Public/Remove-ZertoVpg.ps1 b/ZertoApiWrapper/Public/Remove-ZertoVpg.ps1 index b3cedcf..8aafb91 100644 --- a/ZertoApiWrapper/Public/Remove-ZertoVpg.ps1 +++ b/ZertoApiWrapper/Public/Remove-ZertoVpg.ps1 @@ -55,7 +55,7 @@ function Remove-ZertoVpg { Invoke-ZertoRestRequest -uri $uri -body $($body | ConvertTo-Json) -Method "DELETE" } } else { - Write-Output "VPG with name $vpgName not found. Please check the name and try again" + Write-Error "VPG with name $vpgName not found. Please check the name and try again" } } } diff --git a/ZertoApiWrapper/Public/Resume-ZertoVpg.ps1 b/ZertoApiWrapper/Public/Resume-ZertoVpg.ps1 index f5e1181..e258d0c 100644 --- a/ZertoApiWrapper/Public/Resume-ZertoVpg.ps1 +++ b/ZertoApiWrapper/Public/Resume-ZertoVpg.ps1 @@ -16,8 +16,12 @@ function Resume-ZertoVpg { process { foreach ($name in $vpgName) { $id = $(Get-ZertoVpg -name $name).vpgIdentifier - $uri = "{0}/{1}/resume" -f $baseUri, $id - Invoke-ZertoRestRequest -uri $uri -method "POST" + if ( -not $id ) { + Write-Error "VPG: $name not found. Please check the name and try again. Skipping." + } else { + $uri = "{0}/{1}/resume" -f $baseUri, $id + Invoke-ZertoRestRequest -uri $uri -method "POST" + } } } diff --git a/ZertoApiWrapper/Public/Start-ZertoCloneVpg.ps1 b/ZertoApiWrapper/Public/Start-ZertoCloneVpg.ps1 index 03a9677..adf82ff 100644 --- a/ZertoApiWrapper/Public/Start-ZertoCloneVpg.ps1 +++ b/ZertoApiWrapper/Public/Start-ZertoCloneVpg.ps1 @@ -24,18 +24,35 @@ function Start-ZertoCloneVpg { begin { $baseUri = "vpgs" - $vpgInfo = Get-ZertoVpg -name $vpgName + if ( -not $vpgInfo ) { + Write-Error "VPG: $vpgName could not be found. Please check the name and try again." + } $vpgIdentifier = $vpgInfo.vpgIdentifier if ( $PSBoundParameters.ContainsKey('datastoreName') ) { $recoverysiteIdentifier = $vpgInfo.recoverysite.identifier $recoverySiteDatastores = Get-ZertoVirtualizationSite -siteIdentifier $recoverysiteIdentifier -datastores $datastoreIdentifier = $($recoverySiteDatastores | Where-Object {$_.datastoreName -like $datastoreName}).DatastoreIdentifier + if ( -not $datastoreIdentifier ) { + Write-Error "Datastore: $datastoreName is not a valid datastore. Please check the name and try again." -ErrorAction Stop + } } if ( $PSBoundParameters.ContainsKey('vmName') ) { - $vmIdentifiers = @() - $vmIdentifiers = foreach ( $name in $vmName ) { - $(Get-ZertoProtectedVm -vmName $name).vmIdentifier + $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 + if ($checkpointIdentifier) { + $body['CheckpointIdentifier'] = $checkpointIdentifier } } } diff --git a/ZertoApiWrapper/Public/Start-ZertoFailoverTest.ps1 b/ZertoApiWrapper/Public/Start-ZertoFailoverTest.ps1 index 51d1e38..6d5ecc5 100644 --- a/ZertoApiWrapper/Public/Start-ZertoFailoverTest.ps1 +++ b/ZertoApiWrapper/Public/Start-ZertoFailoverTest.ps1 @@ -21,32 +21,34 @@ function Start-ZertoFailoverTest { begin { $baseUri = "vpgs" $vpgIdentifier = $(Get-ZertoVpg -name $vpgName).vpgIdentifier + if ( -not $vpgIdentifier) { + Write-Error "VPG: $vpgName Not Found. Please check the name and try again!" -ErrorAction Stop + } if ( $PSBoundParameters.ContainsKey('vmName') ) { - $vmIdentifiers = @() - $vmIdentifiers = foreach ( $name in $vmName ) { - $(Get-ZertoProtectedVm -vmName $name).vmIdentifier + $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 + if ($checkpointIdentifier) { + $body['CheckpointIdentifier'] = $checkpointIdentifier } } } process { $uri = "{0}/{1}/FailoverTest" -f $baseUri, $vpgIdentifier - $body = [ordered]@{} - if ($checkpointIdentifier) { - $body['CheckpointIdentifier'] = $checkpointIdentifier - } - if ( $PSBoundParameters.ContainsKey('vmName') ) { - $vmIdentifiers = @() - $vmIdentifiers = foreach ( $name in $vmName ) { - $(Get-ZertoProtectedVm -vmName $name).vmIdentifier - } - $body['VmIdentifiers'] = $vmIdentifiers - } - if ($PSCmdlet.ShouldProcess("Starting Failover Test")) { + if ($PSCmdlet.ShouldProcess($vpgName)) { Invoke-ZertoRestRequest -uri $uri -method "POST" -body $($body | ConvertTo-Json) } - - } end { diff --git a/ZertoApiWrapper/Public/Stop-ZertoCloneVpg.ps1 b/ZertoApiWrapper/Public/Stop-ZertoCloneVpg.ps1 index 72e09f4..746238e 100644 --- a/ZertoApiWrapper/Public/Stop-ZertoCloneVpg.ps1 +++ b/ZertoApiWrapper/Public/Stop-ZertoCloneVpg.ps1 @@ -12,6 +12,9 @@ function Stop-ZertoCloneVpg { begin { $baseUri = "vpgs" $vpgIdentifier = $(Get-ZertoVpg -name $vpgName).vpgIdentifier + if ( -not $vpgIdentifier ) { + Write-Error "VPG: $vpgName could not be found. Please check the name and try again." -ErrorAction Stop + } } process { diff --git a/ZertoApiWrapper/Public/Stop-ZertoFailoverTest.ps1 b/ZertoApiWrapper/Public/Stop-ZertoFailoverTest.ps1 index c8c63c9..add6b89 100644 --- a/ZertoApiWrapper/Public/Stop-ZertoFailoverTest.ps1 +++ b/ZertoApiWrapper/Public/Stop-ZertoFailoverTest.ps1 @@ -6,6 +6,7 @@ function Stop-ZertoFailoverTest { HelpMessage = "Name(s) of VPG(s) to stop testing.", Mandatory = $true )] + [ValidateNotNullOrEmpty()] [string[]]$vpgName, [Parameter( HelpMessage = "Was the test successful? True or False. True is Default." @@ -25,11 +26,13 @@ function Stop-ZertoFailoverTest { process { foreach ($name in $vpgName) { $vpgId = $(Get-ZertoVpg -name $name).vpgIdentifier + if ( -not $vpgId) { + Write-Error "VPG: $vpgName Not Found. Please check the name and try again!" -ErrorAction Stop + } $uri = "{0}/{1}/FailoverTestStop" -f $baseUri, $vpgId if ($PSCmdlet.ShouldProcess("Stopping Failover Test")) { Invoke-ZertoRestRequest -uri $uri -method "POST" -body $($body | ConvertTo-Json) } - } } diff --git a/ZertoApiWrapper/Public/Suspend-ZertoVpg.ps1 b/ZertoApiWrapper/Public/Suspend-ZertoVpg.ps1 index e19bf3f..68e8bee 100644 --- a/ZertoApiWrapper/Public/Suspend-ZertoVpg.ps1 +++ b/ZertoApiWrapper/Public/Suspend-ZertoVpg.ps1 @@ -16,8 +16,12 @@ function Suspend-ZertoVpg { process { foreach ($name in $vpgName) { $id = $(Get-ZertoVpg -name $name).vpgIdentifier - $uri = "{0}/{1}/pause" -f $baseUri, $id - Invoke-ZertoRestRequest -uri $uri -method "POST" + if ( -not $id ) { + Write-Error "VPG: $name not found. Skipping." + } else { + $uri = "{0}/{1}/pause" -f $baseUri, $id + Invoke-ZertoRestRequest -uri $uri -method "POST" + } } } diff --git a/ZertoApiWrapper/Public/Uninstall-ZertoVra.ps1 b/ZertoApiWrapper/Public/Uninstall-ZertoVra.ps1 index dc5b8b4..31496ff 100644 --- a/ZertoApiWrapper/Public/Uninstall-ZertoVra.ps1 +++ b/ZertoApiWrapper/Public/Uninstall-ZertoVra.ps1 @@ -17,8 +17,12 @@ function Uninstall-ZertoVra { foreach ($name in $hostName) { $vraName = "Z-VRA-{0}" -f $name $vraIdentifier = get-zertovra -vraName $vraName | Select-Object vraIdentifier -ExpandProperty vraIdentifier - $uri = "{0}/{1}" -f $baseUri, $vraIdentifier.toString() - Invoke-ZertoRestRequest -uri $uri -method "DELETE" + if ( -not $vraIdentifier ) { + Write-Error "Host: $hostName either does not have a VRA or was not found. Please check the name and try again. Skipping." + } else { + $uri = "{0}/{1}" -f $baseUri, $vraIdentifier.toString() + Invoke-ZertoRestRequest -uri $uri -method "DELETE" + } if ($hostName.Count -gt 1) { Start-Sleep 1 }