Skip to content

Commit 00f49c9

Browse files
authored
Add logonToken parameter to Platform scripts (#425)
* Add logonToken parameter Add an optional logonToken parameter to pass a pre-existing authorization token. * indent * Add logonToken parameter Add an optional logonToken parameter to pass a pre-existing authorization token. * indent * minor
1 parent 12ed254 commit 00f49c9

File tree

2 files changed

+101
-46
lines changed

2 files changed

+101
-46
lines changed

Platforms/Get-PlatformDetails.ps1

Lines changed: 60 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ param
2828

2929
[Parameter(Mandatory=$true,HelpMessage="Enter the platform ID to export")]
3030
[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
3236
)
3337

3438
# Global URLS
@@ -45,7 +49,6 @@ $URL_PlatformDetails = $URL_PVWAAPI+"/Platforms/{0}"
4549
# Initialize Script Variables
4650
# ---------------------------
4751
$rstusername = $rstpassword = ""
48-
$logonToken = ""
4952

5053
#region Functions
5154
Function Test-CommandExists
@@ -90,41 +93,52 @@ If (Test-CommandExists Invoke-RestMethod)
9093
}
9194

9295
#region [Logon]
93-
# Get Credentials to Login
94-
# ------------------------
9596
$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
99106
{
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 }
104117

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)
117141
}
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)
128142
#endregion
129143

130144
#region Get Platform details
@@ -137,20 +151,26 @@ If (Test-CommandExists Invoke-RestMethod)
137151
{
138152
Write-verbose $platformDetails
139153
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+
}
143157
} catch {
144158
Write-Error $_.Exception.Response
145159
Write-Error $_.Exception.Response.StatusDescription
146160
}
147161
#endregion
148162
# 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+
}
152172
}
153173
else
154174
{
155175
Write-Error "This script requires PowerShell version 3 or above"
156-
}
176+
}

Platforms/Get-PlatformReport.ps1

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ param
3838

3939
[Parameter(Mandatory=$false,HelpMessage="Path to a CSV file to export data to")]
4040
[Alias("path")]
41-
[string]$CSVPath
41+
[string]$CSVPath,
42+
43+
# Use this parameter to pass a pre-existing authorization token. If passed the token is NOT logged off
44+
[Parameter(Mandatory = $false)]
45+
$logonToken
4246
)
4347

4448
# Get Script Location
@@ -536,9 +540,35 @@ else
536540
# Get Credentials to Login
537541
# ------------------------
538542
$caption = "Platforms Report"
539-
$msg = "Enter your PAS User name and Password ($AuthType)";
540-
$creds = $Host.UI.PromptForCredential($caption,$msg,"","")
541543

544+
#region [Logon]
545+
try {
546+
If (![string]::IsNullOrEmpty($logonToken)) {
547+
if ($logonToken.GetType().name -eq 'String') {
548+
$logonHeader = @{Authorization = $logonToken }
549+
Set-Variable -Name g_LogonHeader -Value $logonHeader -Scope global
550+
}
551+
else {
552+
Set-Variable -Name g_LogonHeader -Value $logonToken -Scope global
553+
}
554+
}
555+
elseif ($null -eq $creds) {
556+
# Get Credentials to Login
557+
# ------------------------
558+
$msg = "Enter your PAS User name and Password ($AuthType)";
559+
$creds = $Host.UI.PromptForCredential($caption,$msg,"","")
560+
Get-LogonHeader -Credentials $creds
561+
}
562+
else {
563+
Write-LogMessage -type Error -MSG 'No Credentials were entered'
564+
return
565+
}
566+
}
567+
catch {
568+
Write-LogMessage -type Error -MSG "Error Logging on. Error: $(Join-ExceptionMessage $_.Exception)"
569+
return
570+
}
571+
#endregion
542572

543573
#region Get all active Platforms
544574
try{
@@ -549,15 +579,15 @@ try{
549579
} else {
550580
$urlActivePlatforms = $URL_TargetPlatforms
551581
}
552-
$activePlatforms = Invoke-Rest -Command Get -Uri $urlActivePlatforms -Header $(Get-LogonHeader -Credentials $creds)
582+
$activePlatforms = Invoke-Rest -Command Get -Uri $urlActivePlatforms -Header $g_LogonHeader
553583
If($activePlatforms)
554584
{
555585
Write-LogMessage -Type Info -Msg "Found $($activePlatforms.Total) active Platforms"
556586
$reportPlatforms = @()
557587
Write-LogMessage -Type Debug -Msg "Getting Platfroms PSM Connectors information"
558588
ForEach($platform in $activePlatforms.Platforms)
559589
{
560-
$psmServerTargetInfo = Invoke-REST -Command Get -Uri ($URL_TargetPlatformPSMConnectors -f $platform.id) -Header (Get-LogonHeader -Credentials $creds)
590+
$psmServerTargetInfo = Invoke-REST -Command Get -Uri ($URL_TargetPlatformPSMConnectors -f $platform.id) -Header $g_LogonHeader
561591
if($null -ne $platform.PrivilegedSessionManagement)
562592
{
563593
$platform.PrivilegedSessionManagement | Add-Member -NotePropertyName PSMConnectors -NotePropertyValue $psmServerTargetInfo.PSMConnectors
@@ -612,4 +642,9 @@ try{
612642
}
613643
#endregion
614644
# Logoff the session
615-
Run-Logoff
645+
If (![string]::IsNullOrEmpty($logonToken)) {
646+
Write-Host 'LogonToken passed, session NOT logged off'
647+
}
648+
else {
649+
Run-Logoff
650+
}

0 commit comments

Comments
 (0)