28
28
29
29
[Parameter (Mandatory = $true , HelpMessage = " Enter the platform ID to export" )]
30
30
[Alias (" id" )]
31
- [string ]$PlatformID
31
+ [string ]$PlatformID ,
32
+
33
+ # Use this parameter to pass a pre-existing authorization token. If passed the token is NOT logged off
34
+ [Parameter (Mandatory = $false )]
35
+ $logonToken
32
36
)
33
37
34
38
# Global URLS
@@ -45,7 +49,6 @@ $URL_PlatformDetails = $URL_PVWAAPI+"/Platforms/{0}"
45
49
# Initialize Script Variables
46
50
# ---------------------------
47
51
$rstusername = $rstpassword = " "
48
- $logonToken = " "
49
52
50
53
# region Functions
51
54
Function Test-CommandExists
@@ -90,41 +93,52 @@ If (Test-CommandExists Invoke-RestMethod)
90
93
}
91
94
92
95
# region [Logon]
93
- # Get Credentials to Login
94
- # ------------------------
95
96
$caption = " Get accounts"
96
- $msg = " Enter your User name and Password" ;
97
- $creds = $Host.UI.PromptForCredential ($caption , $msg , " " , " " )
98
- if ($null -ne $creds )
97
+ if (! [string ]::IsNullOrEmpty($logonToken )) {
98
+ if ($logonToken.GetType ().name -eq ' String' ) {
99
+ $logonHeader = @ {Authorization = $logonToken }
100
+ }
101
+ else {
102
+ $logonHeader = $logonToken
103
+ }
104
+ }
105
+ else
99
106
{
100
- $rstusername = $creds.username.Replace (' \' , ' ' );
101
- $rstpassword = $creds.GetNetworkCredential ().password
102
- }
103
- else { exit }
107
+ # Get Credentials to Login
108
+ # ------------------------
109
+ $msg = " Enter your User name and Password" ;
110
+ $creds = $Host.UI.PromptForCredential ($caption , $msg , " " , " " )
111
+ if ($null -ne $creds )
112
+ {
113
+ $rstusername = $creds.username.Replace (' \' , ' ' );
114
+ $rstpassword = $creds.GetNetworkCredential ().password
115
+ }
116
+ else { exit }
104
117
105
- # Create the POST Body for the Logon
106
- # ----------------------------------
107
- $logonBody = @ { username = $rstusername ;password = $rstpassword }
108
- $logonBody = $logonBody | ConvertTo-Json
109
- try {
110
- # Logon
111
- $logonToken = Invoke-RestMethod - Method Post - Uri $URL_Logon - Body $logonBody - ContentType " application/json"
112
- }
113
- catch
114
- {
115
- Write-Host - ForegroundColor Red $_.Exception.Response.StatusDescription
116
- $logonToken = " "
118
+ # Create the POST Body for the Logon
119
+ # ----------------------------------
120
+ $logonBody = @ { username = $rstusername ; password = $rstpassword }
121
+ $logonBody = $logonBody | ConvertTo-Json
122
+ try {
123
+ # Logon
124
+ $logonToken = Invoke-RestMethod - Method Post - Uri $URL_Logon - Body $logonBody - ContentType " application/json"
125
+ }
126
+ catch
127
+ {
128
+ Write-Host - ForegroundColor Red $_.Exception.Response.StatusDescription
129
+ $logonToken = " "
130
+ }
131
+ If ($logonToken -eq " " )
132
+ {
133
+ Write-Host - ForegroundColor Red " Logon Token is Empty - Cannot login"
134
+ exit
135
+ }
136
+
137
+ # Create a Logon Token Header (This will be used through out all the script)
138
+ # ---------------------------
139
+ $logonHeader = New-Object " System.Collections.Generic.Dictionary[[String],[String]]"
140
+ $logonHeader.Add (" Authorization" , $logonToken )
117
141
}
118
- If ($logonToken -eq " " )
119
- {
120
- Write-Host - ForegroundColor Red " Logon Token is Empty - Cannot login"
121
- exit
122
- }
123
-
124
- # Create a Logon Token Header (This will be used through out all the script)
125
- # ---------------------------
126
- $logonHeader = New-Object " System.Collections.Generic.Dictionary[[String],[String]]"
127
- $logonHeader.Add (" Authorization" , $logonToken )
128
142
# endregion
129
143
130
144
# region Get Platform details
@@ -137,20 +151,26 @@ If (Test-CommandExists Invoke-RestMethod)
137
151
{
138
152
Write-verbose $platformDetails
139
153
Write-Host " $ ( $platformDetails.Details.PolicyName ) (ID: $ ( $platformDetails.PlatformID ) ) is currently $ ( if ($platformDetails.Active ) { " Activated" } else { " Inactive" }) "
140
- Write-Host " Platform details:"
141
- $platformDetails.Details | Select-Object PolicyID, AllowedSafes, AllowManualChange, PerformPeriodicChange, @ {Name = ' AllowManualVerification' ; Expression = { $_.VFAllowManualVerification }}, @ {Name = ' PerformPeriodicVerification' ; Expression = { $_.VFPerformPeriodicVerification }}, @ {Name = ' AllowManualReconciliation' ; Expression = { $_.RCAllowManualReconciliation }}, @ {Name = ' PerformAutoReconcileWhenUnsynced' ; Expression = { $_.RCAutomaticReconcileWhenUnsynched }}, PasswordLength, MinUpperCase, MinLowerCase, MinDigit, MinSpecial
142
- }
154
+ Write-Host " Platform details:"
155
+ $platformDetails.Details | Select-Object PolicyID, AllowedSafes, AllowManualChange, PerformPeriodicChange, @ {Name = ' AllowManualVerification' ; Expression = { $_.VFAllowManualVerification }}, @ {Name = ' PerformPeriodicVerification' ; Expression = { $_.VFPerformPeriodicVerification }}, @ {Name = ' AllowManualReconciliation' ; Expression = { $_.RCAllowManualReconciliation }}, @ {Name = ' PerformAutoReconcileWhenUnsynced' ; Expression = { $_.RCAutomaticReconcileWhenUnsynched }}, PasswordLength, MinUpperCase, MinLowerCase, MinDigit, MinSpecial
156
+ }
143
157
} catch {
144
158
Write-Error $_.Exception.Response
145
159
Write-Error $_.Exception.Response.StatusDescription
146
160
}
147
161
# endregion
148
162
# Logoff the session
149
- # ------------------
150
- Write-Host " Logoff Session..."
151
- Invoke-RestMethod - Method Post - Uri $URL_Logoff - Headers $logonHeader - ContentType " application/json" | Out-Null
163
+ If (! [string ]::IsNullOrEmpty($logonToken )) {
164
+ Write-Host ' LogonToken passed, session NOT logged off'
165
+ }
166
+ else {
167
+ # Logoff the session
168
+ # ------------------
169
+ Write-Host " Logoff Session..."
170
+ Invoke-RestMethod - Method Post - Uri $URL_Logoff - Headers $logonHeader - ContentType " application/json" | Out-Null
171
+ }
152
172
}
153
173
else
154
174
{
155
175
Write-Error " This script requires PowerShell version 3 or above"
156
- }
176
+ }
0 commit comments