From ad8f06c6e1870e727e83b15b9becd4e64fe305ae Mon Sep 17 00:00:00 2001 From: Wes Carroll Date: Mon, 24 Jun 2019 12:42:10 -0400 Subject: [PATCH 1/6] Add Logic for token tests --- .../Private/Invoke-ZARestRequest.ps1 | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/ZertoApiWrapper/Private/Invoke-ZARestRequest.ps1 b/ZertoApiWrapper/Private/Invoke-ZARestRequest.ps1 index 599bad8..70e8951 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:zaLastAction).addMinutes(60) -lt $(get-date) ) { + Write-Error -Message "Authorization Token has Expired. Please re-authorize to the Zerto Virtual Manager" + 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 +} From 9d579a5785906512694024a83810d17af5fd505d Mon Sep 17 00:00:00 2001 From: Wes Carroll Date: Mon, 24 Jun 2019 12:42:23 -0400 Subject: [PATCH 2/6] Update VSCode settings --- .vscode/settings.json | 4 ++++ 1 file changed, 4 insertions(+) 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" } From 83649fb72a582da6138f6ace665350f46ccfb267 Mon Sep 17 00:00:00 2001 From: Wes Carroll Date: Mon, 24 Jun 2019 12:47:50 -0400 Subject: [PATCH 3/6] Add information to release notes. --- RELEASENOTES.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) 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. From 44ed5767ea00bcab0782379d4c803b080beaf47d Mon Sep 17 00:00:00 2001 From: Wes Carroll Date: Tue, 25 Jun 2019 08:30:54 -0400 Subject: [PATCH 4/6] Update Expired Token Error Message --- ZertoApiWrapper/Private/Invoke-ZARestRequest.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ZertoApiWrapper/Private/Invoke-ZARestRequest.ps1 b/ZertoApiWrapper/Private/Invoke-ZARestRequest.ps1 index 70e8951..8581a2f 100644 --- a/ZertoApiWrapper/Private/Invoke-ZARestRequest.ps1 +++ b/ZertoApiWrapper/Private/Invoke-ZARestRequest.ps1 @@ -12,7 +12,7 @@ function Invoke-ZARestRequest { 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:zaLastAction).addMinutes(60) -lt $(get-date) ) { - Write-Error -Message "Authorization Token has Expired. Please re-authorize to the Zerto Virtual Manager" + Write-Error -Message "Authorization Token has Expired. Please re-authorize to the Zerto Analytics Portal" break } else { # Update the last action time and submit the request based on PS Version. From b7d4664d6b6cb92c4fc9307cd32e2c3060f0503c Mon Sep 17 00:00:00 2001 From: Wes Carroll Date: Tue, 25 Jun 2019 14:00:40 -0400 Subject: [PATCH 5/6] Correct Last Action Variable Name --- ZertoApiWrapper/Private/Invoke-ZARestRequest.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ZertoApiWrapper/Private/Invoke-ZARestRequest.ps1 b/ZertoApiWrapper/Private/Invoke-ZARestRequest.ps1 index 8581a2f..1e35829 100644 --- a/ZertoApiWrapper/Private/Invoke-ZARestRequest.ps1 +++ b/ZertoApiWrapper/Private/Invoke-ZARestRequest.ps1 @@ -11,7 +11,7 @@ function Invoke-ZARestRequest { 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:zaLastAction).addMinutes(60) -lt $(get-date) ) { + } 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 { From 0b1f2c4d1b1933c00b07d815996391ecfd789968 Mon Sep 17 00:00:00 2001 From: Wes Carroll Date: Tue, 25 Jun 2019 14:02:07 -0400 Subject: [PATCH 6/6] Update psd1 to export all functions This will not happen in built releases, but just for in-house testing scenarios. --- ZertoApiWrapper/ZertoApiWrapper.psd1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 = @()