diff --git a/.vscode/settings.json b/.vscode/settings.json index d6dea32..77c8224 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,4 +1,8 @@ { "files.trimTrailingWhitespace": true, + "files.insertFinalNewline": true, + "editor.insertSpaces": true, + "editor.tabSize": 4, + "powershell.codeFormatting.preset": "OTBS", "terminal.integrated.shell.windows": "c:/Program Files/PowerShell/6/pwsh.exe" } diff --git a/RELEASENOTES.md b/RELEASENOTES.md index 3178e32..4e5cafc 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -1,11 +1,9 @@ -## Initial Release - ### Zerto Virtual Manager -* Updated `Invoke-ZertoRestRequest` to work in Powershell 5.1 as well as Powershell core +* ### Zerto Analytics -* Implemented Zerto Analytics Functionality. Please see [Getting Started with Zerto Analytics](https://github.com/ZertoPublic/ZertoApiWrapper/wiki/Getting-Started-with-Zerto-Analytics) +* Fixed an issue where the Zerto Analytics Rest Request function was not checking for the token before attempting a connection. diff --git a/ZertoApiWrapper/Private/Invoke-ZARestRequest.ps1 b/ZertoApiWrapper/Private/Invoke-ZARestRequest.ps1 index 599bad8..1e35829 100644 --- a/ZertoApiWrapper/Private/Invoke-ZARestRequest.ps1 +++ b/ZertoApiWrapper/Private/Invoke-ZARestRequest.ps1 @@ -7,14 +7,26 @@ function Invoke-ZARestRequest { [string]$contentType = "application/json" ) - $submittedUri = "https://analytics.api.zerto.com/v2/{0}" -f $uri - if ($PSVersionTable.PSVersion.Major -ge 6) { - Invoke-RestMethod -Uri $submittedUri -Method $method -Body $body -Headers $Script:zaHeaders -ContentType $contentType -TimeoutSec 100 + # Check to see if the required variables are present and currently valid + if ( -not ((Test-Path variable:script:zaLastActionTime) -and (Test-Path variable:script:zaHeaders)) ) { + Write-Error -Message "Zerto Analytics Connection does not Exist. Please run Connect-ZertoAnalytics first to establish a connection" + break + } elseif ( (Test-Path variable:script:zaHeaders) -and $([datetime]$script:zaLastActionTime).addMinutes(60) -lt $(get-date) ) { + Write-Error -Message "Authorization Token has Expired. Please re-authorize to the Zerto Analytics Portal" + break } else { - if ([String]::IsNullOrEmpty($body)) { - Invoke-RestMethod -Uri $submittedUri -Method $method -Headers $Script:zaHeaders -ContentType $contentType -TimeoutSec 100 + # Update the last action time and submit the request based on PS Version. + Set-Variable -Name zaLastActionTime -Scope Script -Value $(Get-date).Ticks + $submittedUri = "https://analytics.api.zerto.com/v2/{0}" -f $uri + if ($PSVersionTable.PSVersion.Major -ge 6) { + Invoke-RestMethod -Uri $submittedUri -Method $method -Body $body -Headers $Script:zaHeaders -ContentType $contentType -TimeoutSec 100 } else { - Invoke-RestMethod -Uri $submittedUri -Method $method -Headers $Script:zaHeaders -ContentType $contentType -TimeoutSec 100 -Body $body + # With PS 5, you cannot ship a $null body, check for $body variable and select correct Invoke request. + if ([String]::IsNullOrEmpty($body)) { + Invoke-RestMethod -Uri $submittedUri -Method $method -Headers $Script:zaHeaders -ContentType $contentType -TimeoutSec 100 + } else { + Invoke-RestMethod -Uri $submittedUri -Method $method -Headers $Script:zaHeaders -ContentType $contentType -TimeoutSec 100 -Body $body + } } } -} \ No newline at end of file +} diff --git a/ZertoApiWrapper/ZertoApiWrapper.psd1 b/ZertoApiWrapper/ZertoApiWrapper.psd1 index af87c16..44754ec 100644 --- a/ZertoApiWrapper/ZertoApiWrapper.psd1 +++ b/ZertoApiWrapper/ZertoApiWrapper.psd1 @@ -69,7 +69,7 @@ # NestedModules = @() # Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export. - FunctionsToExport = 'Add-ZertoPeerSite', 'Checkpoint-ZertoVpg', 'Connect-ZertoServer', 'Disconnect-ZertoServer', 'Edit-ZertoVra', 'Export-ZertoVpg', 'Get-ZertoAlert', 'Get-ZertoDatastore', 'Get-ZertoEvent', 'Get-ZertoLicense', 'Get-ZertoLocalSite', 'Get-ZertoPeerSite', 'Get-ZertoProtectedVm', 'Get-ZertoRecoveryReport', 'Get-ZertoResourcesReport', 'Get-ZertoServiceProfile', 'Get-ZertoTask', 'Get-ZertoUnprotectedVm', 'Get-ZertoVirtualizationSite', 'Get-ZertoVolume', 'Get-ZertoVpg', 'Get-ZertoVpgSetting', 'Get-ZertoVra', 'Get-ZertoZorg', 'Get-ZertoZsspSession', 'Import-ZertoVpg', 'Install-ZertoVra', 'Invoke-ZertoFailover', 'Invoke-ZertoFailoverCommit', 'Invoke-ZertoFailoverRollback', 'Invoke-ZertoForceSync', 'Invoke-ZertoMove', 'Invoke-ZertoMoveCommit', 'Invoke-ZertoMoveRollback', 'New-ZertoVpg', 'New-ZertoVpgSettingsIdentifier', 'Remove-ZertoPeerSite', 'Remove-ZertoVpg', 'Resume-ZertoVpg', 'Save-ZertoVpgSettings', 'Set-ZertoAlert', 'Set-ZertoLicense', 'Start-ZertoCloneVpg', 'Start-ZertoFailoverTest', 'Stop-ZertoCloneVpg', 'Stop-ZertoFailoverTest', 'Suspend-ZertoVpg', 'Uninstall-ZertoVra' + FunctionsToExport = '*' # Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. CmdletsToExport = @()