-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGet-Pass.ps1
1182 lines (1053 loc) · 42.8 KB
/
Get-Pass.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
###############################################################################
#### Created with the help of cyberark epv api scripts helper functions #####
## This can export password of accounts from cyberark. It has 3 switches #####
## 1) Accoutns from a safename #####
## 2) Accounts from list of safes from a CSV file #####
## 3) All the accounts exported #####
## order of precedence is also same. #####
## for exaple if safename & csv file both are given only safename will work####
###############################################################################
[CmdletBinding()]
param(
[Parameter(Mandatory = $false, HelpMessage = "Export Items")]
[switch]$export,
[Parameter(Mandatory = $false, HelpMessage = "Export Items")]
[switch]$disableSSLVerify = $false,
[Parameter(Mandatory = $false, HelpMessage = "Full Accounts")]
[switch]$full,
[Parameter(Mandatory = $false, HelpMessage = "Enter the Authentication type (Default:CyberArk)")]
[ValidateSet("cyberark", "ldap", "radius")]
[String]$AuthType = "cyberark",
[Parameter(Mandatory = $false, HelpMessage = "Enter the RADIUS OTP")]
[ValidateScript({ $AuthType -eq "radius" })]
[String]$OTP,
[Parameter(Mandatory = $false, HelpMessage = "Enter the safename")]
[String]$safeName,
[Parameter(Mandatory = $true, HelpMessage = "Please enter your source PVWA address (For example: https://pvwa.mydomain.com/PasswordVault)")]
#[ValidateScript({Invoke-WebRequest -UseBasicParsing -DisableKeepAlive -Uri $_ -Method 'Head' -ErrorAction 'stop' -TimeoutSec 30})]
[Alias("srcurl")]
[String]$PVWAURL,
[Parameter(Mandatory = $false,
HelpMessage = "Path and file name of the objects.csv file containing safenames",
ValueFromPipelineByPropertyName = $true)]
[ValidateScript({ Test-Path -Path $_ -PathType Leaf -IsValid })]
[ValidatePattern('\.csv$')]
$safesCSV,
[Parameter(Mandatory = $false,
HelpMessage = "Path and file name of the objects.csv file created by the export",
ValueFromPipelineByPropertyName = $true)]
[ValidateScript({ Test-Path -Path $_ -PathType Leaf -IsValid })]
[ValidatePattern('\.csv$')]
$exportCSV = "$($env:TEMP)\ExportOfAccounts.csv"
)
# Global URLS
# -----------
#region Global Variables
$URL_PVWAAPI = $global:PVWAURL + "/api"
$URL_Authentication = $URL_PVWAAPI + "/auth"
$URL_Logon = $URL_Authentication + "/$global:AuthType/Logon"
$URL_Logoff = $URL_Authentication + "/Logoff"
$URL_UserSearch = $URL_PVWAAPI + "/Users?filter=componentUser&search={0}"
$URL_UserResetPassword = $URL_PVWAAPI + "/Users/{0}/ResetPassword"
$URL_Activate = $URL_PVWAAPI + "/Users/{0}/Activate"
$URL_Accounts = $URL_PVWAAPI + "/Accounts"
$URL_AccountsDetails = $URL_PVWAAPI + "/Accounts/{0}"
$URL_Platforms = $URL_PVWAAPI + "/Platforms/{0}"
if ($InVerbose) {
$VerbosePreference = "continue"
}
#endregion
# Initialize Script Variables
# ---------------------------
# @FUNCTION@ ======================================================================================================================
# Name...........: Write-LogMessage
# Description....: Writes the message to log and screen
# Parameters.....: LogFile, MSG, (Switch)Header, (Switch)SubHeader, (Switch)Footer, Type
# Return Values..: None
# =================================================================================================================================
Function Write-LogMessage {
<#
.SYNOPSIS
Method to log a message on screen and in a log file
.DESCRIPTION
Logging The input Message to the Screen and the Log File.
The Message Type is presented in colours on the screen based on the type
.PARAMETER LogFile
The Log File to write to. By default using the LOG_FILE_PATH
.PARAMETER MSG
The message to log
.PARAMETER Header
Adding a header line before the message
.PARAMETER SubHeader
Adding a Sub header line before the message
.PARAMETER Footer
Adding a footer line after the message
.PARAMETER Type
The type of the message to log (Info, Warning, Error, Debug)
#>
param(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[AllowEmptyString()]
[String]$MSG,
[Parameter(Mandatory = $false)]
[Switch]$Header,
[Parameter(Mandatory = $false)]
[Switch]$SubHeader,
[Parameter(Mandatory = $false)]
[Switch]$Footer,
[Parameter(Mandatory = $false)]
[Bool]$WriteLog = $true,
[Parameter(Mandatory = $false)]
[ValidateSet("Info", "Warning", "Error", "Debug", "Verbose", "Success", "LogOnly")]
[String]$type = "Info",
[Parameter(Mandatory = $false)]
[String]$LogFile = $LOG_FILE_PATH
)
If (![string]::IsNullOrEmpty($PSSenderInfo)) {
$WriteLog = $false
}
Try {
If ([string]::IsNullOrEmpty($LogFile) -and $WriteLog) {
# User wanted to write logs, but did not provide a log file - Create a temporary file
$LogFile = Join-Path -Path $ENV:Temp -ChildPath "$((Get-Date).ToShortDateString().Replace('/','_')).log"
Write-Host "No log file path inputted, created a temporary file at: '$LogFile'"
}
If ($Header -and $WriteLog) {
"=======================================" | Out-File -Append -FilePath $LogFile
Write-Host "=======================================" -ForegroundColor Magenta
}
ElseIf ($SubHeader -and $WriteLog) {
"------------------------------------" | Out-File -Append -FilePath $LogFile
Write-Host "------------------------------------" -ForegroundColor Magenta
}
# Replace empty message with 'N/A'
if ([string]::IsNullOrEmpty($Msg)) {
$Msg = "N/A"
}
$msgToWrite = ""
# Change SecretType if password to prevent masking issues
$Msg = $Msg.Replace('"secretType":"password"', '"secretType":"pass"')
# Mask Passwords
if ($Msg -match '((?:password|credentials|secret)\s{0,}["\:=]{1,}\s{0,}["]{0,})(?=([\w`~!@#$%^&*()-_\=\+\\\/|;:\.,\[\]{}]+))') {
$Msg = $Msg.Replace($Matches[2], "****")
}
$Msg = $Msg.Replace('"secretType":"pass"', '"secretType":"password"')
# Check the message type
switch ($type) {
{ ($_ -eq "Info") -or ($_ -eq "LogOnly") } {
If ($_ -eq "Info") {
Write-Host $MSG.ToString() -ForegroundColor $(If ($Header -or $SubHeader) {
"Magenta"
}
Else {
"Gray"
})
}
$msgToWrite = "[INFO]`t$Msg"
break
}
"Success" {
Write-Host $MSG.ToString() -ForegroundColor Green
$msgToWrite = "[SUCCESS]`t$Msg"
break
}
"Warning" {
Write-Host $MSG.ToString() -ForegroundColor Yellow
$msgToWrite = "[WARNING]`t$Msg"
break
}
"Error" {
Write-Host $MSG.ToString() -ForegroundColor Red
$msgToWrite = "[ERROR]`t$Msg"
break
}
"Debug" {
if ($InDebug -or $InVerbose) {
Write-Debug -Msg $MSG
$msgToWrite = "[Debug]`t$Msg"
}
break
}
"Verbose" {
if ($InVerbose) {
Write-Verbose -Msg $MSG
$msgToWrite = "[VERBOSE]`t$Msg"
}
break
}
}
If ($WriteLog) {
If (![string]::IsNullOrEmpty($msgToWrite)) {
"[$(Get-Date -Format "yyyy-MM-dd hh:mm:ss")]`t$msgToWrite" | Out-File -Append -FilePath $LogFile
}
}
If ($Footer -and $WriteLog) {
"=======================================" | Out-File -Append -FilePath $LogFile
Write-Host "=======================================" -ForegroundColor Magenta
}
}
catch {
Throw $(New-Object System.Exception ("Cannot write message"), $_.Exception)
}
}
# @FUNCTION@ ======================================================================================================================
# Name...........: Join-ExceptionMessage
# Description....: Formats exception messages
# Parameters.....: Exception
# Return Values..: Formatted String of Exception messages
# =================================================================================================================================
Function Join-ExceptionMessage {
<#
.SYNOPSIS
Formats exception messages
.DESCRIPTION
Formats exception messages
.PARAMETER Exception
The Exception object to format
#>
param(
[Exception]$e
)
Begin {
}
Process {
$msg = "Source:{0}; Message: {1}" -f $e.Source, $e.Message
while ($e.InnerException) {
$e = $e.InnerException
$msg += "`n`t->Source:{0}; Message: {1}" -f $e.Source, $e.Message
}
return $msg
}
End {
}
}
#endregion
#region Helper Functions
# @FUNCTION@ ======================================================================================================================
# Name...........: Test-CommandExists
# Description....: Tests if a command exists
# Parameters.....: Command
# Return Values..: True / False
# =================================================================================================================================
Function Test-CommandExists {
<#
.SYNOPSIS
Tests if a command exists
.DESCRIPTION
Tests if a command exists
.PARAMETER Command
The command to test
#>
Param ($command)
$oldPreference = $ErrorActionPreference
$ErrorActionPreference = 'stop'
try {
if (Get-Command $command) {
RETURN $true
}
}
Catch {
Write-Host "$command does not exist"; RETURN $false
}
Finally {
$ErrorActionPreference = $oldPreference
}
} #end function test-CommandExists
# @FUNCTION@ ======================================================================================================================
# Name...........: ConvertTo-URL
# Description....: HTTP Encode test in URL
# Parameters.....: Text to encode
# Return Values..: Encoded HTML URL text
# =================================================================================================================================
Function Convert-ToURL($sText) {
<#
.SYNOPSIS
HTTP Encode test in URL
.DESCRIPTION
HTTP Encode test in URL
.PARAMETER sText
The text to encode
#>
if ($sText.Trim() -ne "") {
Write-LogMessage -type Verbose -Msg "Returning URL Encode of $sText"
return [URI]::EscapeDataString($sText)
}
else {
return $sText
}
}
# @FUNCTION@ ======================================================================================================================
# Name...........: Convert-ToBool
# Description....: Converts text to Bool
# Parameters.....: Text
# Return Values..: Boolean value of the text
# =================================================================================================================================
Function Convert-ToBool {
<#
.SYNOPSIS
Converts text to Bool
.DESCRIPTION
Converts text to Bool
.PARAMETER txt
The text to convert to bool (True / False)
#>
param (
[string]$txt
)
$retBool = $false
if ($txt -match "^y$|^yes$") {
$retBool = $true
}
elseif ($txt -match "^n$|^no$") {
$retBool = $false
}
else {
[bool]::TryParse($txt, [ref]$retBool) | Out-Null
}
return $retBool
}
# @FUNCTION@ ======================================================================================================================
# Name...........: Get-TrimmedString
# Description....: Returns the trimmed text from a string
# Parameters.....: Text
# Return Values..: Trimmed text
# =================================================================================================================================
Function Get-TrimmedString($sText) {
<#
.SYNOPSIS
Returns the trimmed text from a string
.DESCRIPTION
Returns the trimmed text from a string
.PARAMETER txt
The text to handle
#>
if ($null -ne $sText) {
return $sText.Trim()
}
# Else
return $sText
}
# @FUNCTION@ ======================================================================================================================
# Name...........: Invoke-Rest
# Description....: Invoke REST Method
# Parameters.....: Command method, URI, Header, Body
# Return Values..: REST response
# =================================================================================================================================
Function Invoke-Rest {
<#
.SYNOPSIS
Invoke REST Method
.DESCRIPTION
Invoke REST Method
.PARAMETER Command
The REST Command method to run (GET, POST, PATCH, DELETE)
.PARAMETER URI
The URI to use as REST API
.PARAMETER Header
The Header as Dictionary object
.PARAMETER Body
(Optional) The REST Body
.PARAMETER ErrAction
(Optional) The Error Action to perform in case of error. By default "Continue"
#>
param (
[Parameter(Mandatory = $true)]
[ValidateSet("GET", "POST", "DELETE", "PATCH", "PUT")]
[String]$Command,
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[String]$URI,
[Parameter(Mandatory = $false)]
$Header,
[Parameter(Mandatory = $false)]
[String]$Body,
[Parameter(Mandatory = $false)]
[ValidateSet("Continue", "Ignore", "Inquire", "SilentlyContinue", "Stop", "Suspend")]
[String]$ErrAction = "Continue"
)
If ((Test-CommandExists Invoke-RestMethod) -eq $false) {
Throw "This script requires PowerShell version 3 or above"
}
$restResponse = ""
# Write-LogMessage -Type Warning -Msg "It is not Recommended to disable SSL verification" -WarningAction Inquire
# # Using Proxy Default credentials if the Server needs Proxy credentials
# [System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials
# # Using TLS 1.2 as security protocol verification
# [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12 -bor [System.Net.SecurityProtocolType]::Tls11
# # Disable SSL Verification
# [System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }
try {
if ([string]::IsNullOrEmpty($Body)) {
Write-LogMessage -Type Verbose -Msg "Invoke-RestMethod -Uri $URI -Method $Command -Header $Header -ContentType ""application/json"" -TimeoutSec 2700"
$restResponse = Invoke-RestMethod -Uri $URI -Method $Command -Header $Header -ContentType "application/json" -TimeoutSec 2700 -ErrorAction $ErrAction
}
else {
Write-LogMessage -Type Verbose -Msg "Invoke-RestMethod -Uri $URI -Method $Command -Header $Header -ContentType ""application/json"" -Body $Body -TimeoutSec 2700"
$restResponse = Invoke-RestMethod -Uri $URI -Method $Command -Header $Header -ContentType "application/json" -Body $Body -TimeoutSec 2700 -ErrorAction $ErrAction
}
}
catch [System.Net.WebException] {
if ($ErrAction -match ("\bContinue\b|\bInquire\b|\bStop\b|\bSuspend\b")) {
IF (![string]::IsNullOrEmpty($(($_.ErrorDetails.Message | ConvertFrom-Json).ErrorCode))) {
If (($($_.ErrorDetails.Message | ConvertFrom-Json).ErrorCode -eq "ITATS127E")) {
Write-LogMessage -Type Error -Msg "Was able to connect to the PVWA successfully, but the account was locked"
Write-LogMessage -Type Error -Msg "URI: $URI"
Throw "Account Locked"
}
ElseIf (!($($_.ErrorDetails.Message | ConvertFrom-Json).ErrorCode -in $global:SkipErrorCode)) {
Write-LogMessage -Type Error -Msg "Was able to connect to the PVWA successfully, but the command resulted in a error"
Write-LogMessage -Type Error -Msg "URI: $URI"
Write-LogMessage -Type Error -Msg "Command: $Command"
Write-LogMessage -Type Error -Msg "Body: $Body"
Write-LogMessage -Type Error -Msg "Returned ErrorCode: $(($_.ErrorDetails.Message|ConvertFrom-Json).ErrorCode)"
Write-LogMessage -Type Error -Msg "Returned ErrorMessage: $(($_.ErrorDetails.Message|ConvertFrom-Json).ErrorMessage)"
}
}
Else {
Write-LogMessage -Type Error -Msg "Error Message: $_"
Write-LogMessage -Type Error -Msg "Exception Message: $($_.Exception.Message)"
Write-LogMessage -Type Error -Msg "Status Code: $($_.Exception.Response.StatusCode.value__)"
Write-LogMessage -Type Error -Msg "Status Description: $($_.Exception.Response.StatusDescription)"
}
}
$restResponse = $null
}
catch {
Throw $(New-Object System.Exception ("Invoke-Rest: Error in running $Command on '$URI'", $_.Exception))
}
Write-LogMessage -Type Verbose -Msg "Invoke-REST Response: $restResponse"
return $restResponse
}
If ((Test-CommandExists Invoke-RestMethod) -eq $false) {
Write-LogMessage -Type Error -MSG "This script requires PowerShell version 3 or above"
return
}
Function New-SearchCriteria {
param ([string]$sURL, [string]$sSearch, [string]$sSortParam, [string]$sSafeName, [boolean]$startswith, [int]$iLimitPage, [int]$iOffsetPage = 0)
[string]$retURL = $sURL
$retURL += "?"
if (![string]::IsNullOrEmpty($sSearch)) {
Write-LogMessage -Type Debug -Msg "Search: $sSearch"
$retURL += "search=$(Convert-ToURL $sSearch)&"
}
if (![string]::IsNullOrEmpty($sSafeName)) {
Write-LogMessage -Type Debug -Msg "Safe: $sSafeName"
$retURL += "filter=safename eq $(Convert-ToURL $sSafeName)&"
}
if (![string]::IsNullOrEmpty($sSortParam)) {
Write-LogMessage -Type Debug -Msg "Sort: $sSortParam"
$retURL += "sort=$(Convert-ToURL $sSortParam)&"
}
if ($startswith) {
Write-LogMessage -Type Debug -Msg "startswith: $sSortParam"
$retURL += "searchtype=startswith"
}
if ($iLimitPage -gt 0) {
Write-LogMessage -Type Debug -Msg "Limit: $iLimitPage"
$retURL += "limit=$iLimitPage&"
}
if ($retURL[-1] -eq '&') {
$retURL = $retURL.substring(0, $retURL.length - 1)
}
Write-LogMessage -Type Debug -Msg "URL: $retURL"
return $retURL
}
Function Update-SearchCriteria {
param (
[string]$nextLinkURL,
[int]$counter = 1,
[int]$limit
)
# In order to get all the results, we need to increase the Limit
$newNextLink = $nextLinkURL
# First find the limit in the next link URL
if ($nextLinkURL -match "(?:limit=)(\d{1,})") {
$limitText = $Matches[0]
$limitNumber = [int]$Matches[1]
# Verify that we have an increased the limit
if ($limitNumber -ge $limit) {
$newNextLink = $nextLinkURL.Replace($limitText, "limit={0}" -f "1000")
}
else {
Write-LogMessage -Type Debug -Msg "Limits are not correct. Next Link limit: $limitNumber; current limit: $limit; Next limit should be: $($limit * $counter)"
# No change to the next link URL
}
}
return $newNextLink
}
Function Get-AccountDetail {
param (
[Parameter(Mandatory = $false)]
[string]$url = $global:PVWAURL,
[Parameter(Mandatory = $true)]
[string]$AccountID,
[Parameter(Mandatory = $false)]
[hashtable]$logonHeader = $g_LogonHeader
)
$URL_AccountsDetails = "$url/api/Accounts/$AccountID"
return Invoke-Rest -Command Get -Uri $URL_AccountsDetails -Header $logonHeader
}
Function Get-Accounts {
param (
[Parameter(Mandatory = $false)]
[string]$url = $global:PVWAURL,
[Parameter(Mandatory = $false)]
[string]$Keywords,
[Parameter(Mandatory = $false)]
[string]$SortBy,
[Parameter(Mandatory = $false)]
[string]$SafeName,
[Parameter(Mandatory = $false)]
[string]$Limit,
[Parameter(Mandatory = $false)]
[boolean]$startswith,
[Parameter(Mandatory = $false)]
[hashtable]$logonHeader = $g_LogonHeader
)
Write-LogMessage -Type Debug -Msg "Retrieving accounts..."
$URL_Accounts = "$URL/api/accounts"
try {
$AccountsURLWithFilters = ""
$AccountsURLWithFilters = $(New-SearchCriteria -sURL $URL_Accounts -sSearch $Keywords -sSortParam $SortBy -sSafeName $SafeName -iLimitPage $Limit -startswith $startswith)
Write-LogMessage -Type Debug -Msg $AccountsURLWithFilters
}
catch {
Write-LogMessage -Type Error -Msg $_.Exception
}
try {
$GetAccountsResponse = Invoke-Rest -Command Get -Uri $AccountsURLWithFilters -Header $logonHeader
}
catch {
Write-LogMessage -Type Error -Msg $_.Exception.Response.StatusDescription
}
$GetAccountsList = @()
$counter = 1
$GetAccountsList += $GetAccountsResponse.value
Write-LogMessage -Type debug -Msg "Found $($GetAccountsList.count) accounts so far..."
$nextLink = $("$URL/$($GetAccountsResponse.nextLink)")
If (![string]::IsNullOrEmpty($GetAccountsResponse.nextLink)) {
$nextLink = $("$URL/$($GetAccountsResponse.nextLink)")
Write-LogMessage -Type Debug -Msg "Getting accounts next link: $nextLink"
}
else {
$nextLink = $null
}
While (-not [string]::IsNullOrEmpty($nextLink)) {
$GetAccountsResponse = Invoke-Rest -Command Get -Uri $nextLink -Header $logonHeader
$GetAccountsList += $GetAccountsResponse.value
Write-LogMessage -Type info -Msg "Found $($GetAccountsList.count) accounts so far..."
# Increase the counter
$counter++
If (![string]::IsNullOrEmpty($GetAccountsResponse.nextLink)) {
$nextLink = $("$URL/$($GetAccountsResponse.nextLink)")
Write-LogMessage -Type Debug -Msg "Getting accounts next link: $nextLink"
}
else {
$nextLink = $null
}
}
Write-LogMessage -Type debug -Msg "Completed retriving $($GetAccountsList.count) accounts"
$response = $GetAccountsList
return $response
}
Function Set-SSLVerify {
[Parameter(Mandatory = $false)]
[switch]$DisableSSLVerify = $false
If ($DisableSSLVerify) {
try {
Write-LogMessage -Type Warning -Msg "It is not Recommended to disable SSL verification" -WarningAction Inquire
# Using Proxy Default credentials if the Server needs Proxy credentials
[System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials
# Using TLS 1.2 as security protocol verification
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12 -bor [System.Net.SecurityProtocolType]::Tls11
# Disable SSL Verification
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $DisableSSLVerify }
}
catch {
Write-LogMessage -Type Error -MSG "Could not change SSL validation"
Write-LogMessage -Type Error -MSG (Join-ExceptionMessage $_.Exception) -ErrorAction "SilentlyContinue"
return
}
}
Else {
try {
Write-LogMessage -Type Debug -MSG "Setting script to use TLS 1.2"
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
}
catch {
Write-LogMessage -Type Error -MSG "Could not change SSL settings to use TLS 1.2"
Write-LogMessage -Type Error -MSG (Join-ExceptionMessage $_.Exception) -ErrorAction "SilentlyContinue"
}
}
}
Function Test-PVWA {
param(
[Parameter(Mandatory = $true)]
[String]$PVWAURL
)
If (![string]::IsNullOrEmpty($PVWAURL)) {
If ($PVWAURL.Substring($PVWAURL.Length - 1) -eq "/") {
$PVWAURL = $PVWAURL.Substring(0, $PVWAURL.Length - 1)
}
try {
# Validate PVWA URL is OK
Write-LogMessage -Type Debug -MSG "Trying to validate URL: $PVWAURL"
Invoke-WebRequest -UseBasicParsing -DisableKeepAlive -Uri $PVWAURL -Method 'Head' -TimeoutSec 30 | Out-Null
}
catch [System.Net.WebException] {
If (![string]::IsNullOrEmpty($_.Exception.Response.StatusCode.Value__)) {
Write-LogMessage -Type Error -MSG "Received error $($_.Exception.Response.StatusCode.Value__) when trying to validate PVWA URL"
Write-LogMessage -Type Error -MSG "Check your connection to PVWA and the PVWA URL"
return
}
}
catch {
Write-LogMessage -Type Error -MSG "PVWA URL could not be validated"
Write-LogMessage -Type Error -MSG (Join-ExceptionMessage $_.Exception) -ErrorAction "SilentlyContinue"
}
}
else {
Write-LogMessage -Type Error -MSG "PVWA URL can not be empty"
return
}
}
Function Invoke-Logon {
param(
[Parameter(Mandatory = $true)]
[PSCredential]$Credentials,
[Parameter(Mandatory = $false)]
[String]$url = $global:PVWAURL,
[Parameter(Mandatory = $false)]
[String]$AuthType = $global:AuthType
)
# Get Credentials to Login
# ------------------------
$caption = "Reset Remote Cred File Utility"
$msg = "Enter your $AuthType User name and Password";
if ($null -eq $Credentials) {
$Credentials = $Host.UI.PromptForCredential($caption, $msg, "", "")
}
if ($null -ne $Credentials) {
if ($AuthType -eq "radius" -and ![string]::IsNullOrEmpty($OTP)) {
Set-Variable -Scope Global -Force -Name g_LogonHeader -Value $(Get-LogonHeader -Credentials $Credentials -AuthType $AuthType -RadiusOTP $OTP )
}
else {
Set-Variable -Scope Global -Force -Name g_LogonHeader -Value $(Get-LogonHeader -Credentials $Credentials -AuthType $AuthType)
}
# Verify that we successfully logged on
If ($null -eq $g_LogonHeader) {
return # No logon header, end script
}
}
else {
Write-LogMessage -Type Error -MSG "No Credentials were entered" -Footer
return
}
}
Function Get-Logon {
param(
[Parameter(Mandatory = $true)]
[PSCredential]$Credentials,
[Parameter(Mandatory = $false)]
[string]$url = $global:PVWAURL,
[Parameter(Mandatory = $false)]
[string]$AuthType = $global:AuthType
)
$URL_Logon = "$url/api/auth/$AuthType/Logon"
# Get Credentials to Login
# ------------------------
$caption = "Reset Remote Cred File Utility"
$msg = "Enter your $AuthType User name and Password";
if ($null -eq $Credentials) {
$Credentials = $Host.UI.PromptForCredential($caption, $msg, "", "")
}
if ($null -ne $Credentials) {
if ($AuthType -eq "radius" -and ![string]::IsNullOrEmpty($OTP)) {
return $(Get-LogonHeader -Credentials $Credentials -RadiusOTP $OT -URL $URL_Logon)
}
else {
return $(Get-LogonHeader -Credentials $Credentials -URL $URL_Logon)
}
}
else {
Write-LogMessage -Type Error -MSG "No Credentials were entered" -Footer
return
}
}
Function Invoke-Logoff {
param(
[Parameter(Mandatory = $false)]
[String]$url = $global:PVWAURL,
[Parameter(Mandatory = $false)]
[hashtable]$logonHeader = $global:g_LogonHeader
)
$URL_Logoff = $url + "/api/auth/Logoff"
$null = Invoke-Rest -Uri $URL_Logoff -Header $logonHeader -Command "Post"
}
# @FUNCTION@ ======================================================================================================================
# Name...........: Get-LogonHeader
# Description....: Invoke REST Method
# Parameters.....: Credentials
# Return Values..: Logon Header
# =================================================================================================================================
Function Get-LogonHeader {
<#
.SYNOPSIS
Get-LogonHeader
.DESCRIPTION
Get-LogonHeader
.PARAMETER Credentials
The REST API Credentials to authenticate
#>
param(
[Parameter(Mandatory = $true)]
[PSCredential]$Credentials,
[Parameter(Mandatory = $false)]
[string]$RadiusOTP,
[Parameter(Mandatory = $false)]
[string]$URL = $URL_Logon
)
# Create the POST Body for the Logon
# ----------------------------------
$logonBody = @{ username = $Credentials.username.Replace('\', ''); password = $Credentials.GetNetworkCredential().password; concurrentSession = "true" } | ConvertTo-Json -Compress
If (![string]::IsNullOrEmpty($RadiusOTP)) {
$logonBody.Password += ",$RadiusOTP"
}
try {
# Logon
$logonToken = Invoke-Rest -Command Post -Uri $URL -Body $logonBody
# Clear logon body
$logonBody = ""
}
catch {
Throw $(New-Object System.Exception ("Get-LogonHeader: $($_.Exception.Response.StatusDescription)", $_.Exception))
}
$logonHeader = $null
If ([string]::IsNullOrEmpty($logonToken)) {
Throw "Get-LogonHeader: Logon Token is Empty - Cannot login"
}
# Create a Logon Token Header (This will be used through out all the script)
# ---------------------------
$logonHeader = @{Authorization = $logonToken }
return $logonHeader
}
# @FUNCTION@ ======================================================================================================================
# Name...........: Get-LogonHeader
# Description....: Invoke REST Method
# Parameters.....: Credentials
# Return Values..: Logon Header
# =================================================================================================================================
Function Set-LogonHeader {
<#
.SYNOPSIS
Get-LogonHeader
.DESCRIPTION
Get-LogonHeader
.PARAMETER Credentials
The REST API Credentials to authenticate
#>
param(
[Parameter(Mandatory = $true)]
[PSCredential]$Credentials,
[Parameter(Mandatory = $false)]
[string]$RadiusOTP
)
# Create the POST Body for the Logon
# ----------------------------------
$logonBody = @{ username = $Credentials.username.Replace('\', ''); password = $Credentials.GetNetworkCredential().password; concurrentSession = "true" } | ConvertTo-Json -Compress
If (![string]::IsNullOrEmpty($RadiusOTP)) {
$logonBody.Password += ",$RadiusOTP"
}
try {
# Logon
$logonToken = Invoke-Rest -Command Post -Uri $URL_Logon -Body $logonBody
# Clear logon body
$logonBody = ""
}
catch {
Throw $(New-Object System.Exception ("Get-LogonHeader: $($_.Exception.Response.StatusDescription)", $_.Exception))
}
$logonHeader = $null
If ([string]::IsNullOrEmpty($logonToken)) {
Throw "Get-LogonHeader: Logon Token is Empty - Cannot login"
}
# Create a Logon Token Header (This will be used through out all the script)
# ---------------------------
$logonHeader = @{Authorization = $logonToken }
return $logonHeader
}
# @FUNCTION@ ======================================================================================================================
# Name...........: Set-SSLVerify
# Description....: Controls if SSL should be verified REST Method
# Parameters.....: Command method, URI, Header, Body
# Return Values..: REST response
# =================================================================================================================================
Function Set-DisableSSLVerify {
<#
.SYNOPSIS
Invoke REST Method
.DESCRIPTION
Controls if SSL should be verified REST Method
.PARAMETER DisableSSLVerify
Boolean to determine if SSL should be verified
.PARAMETER ErrAction
(Optional) The Error Action to perform in case of error. By default "Continue"
#>
[Parameter(Mandatory = $true)]
[Switch]$DisableSSLVerify
If ($DisableSSLVerify) {
try {
Write-LogMessage -Type Warning -Msg "It is not Recommended to disable SSL verification" -WarningAction Inquire
# Using Proxy Default credentials if the Server needs Proxy credentials
[System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials
# Using TLS 1.2 as security protocol verification
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12 -bor [System.Net.SecurityProtocolType]::Tls11
# Disable SSL Verification
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $DisableSSLVerify }
}
catch {
Write-LogMessage -Type Error -MSG "Could not change SSL validation"
Write-LogMessage -Type Error -MSG (Join-ExceptionMessage $_.Exception) -ErrorAction "SilentlyContinue"
return
}
}
Else {
try {
Write-LogMessage -type Verbose -MSG "Setting script to use TLS 1.2"
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
}
catch {
Write-LogMessage -Type Error -MSG "Could not change SSL settings to use TLS 1.2"
Write-LogMessage -Type Error -MSG (Join-ExceptionMessage $_.Exception) -ErrorAction "SilentlyContinue"
}
}
}
#endregion
# @FUNCTION@ ======================================================================================================================
# Name...........: Get-LogonTimeUnixTime
# Description....: Translates Unix time to readable time
# Parameters.....: Unixtime stamp
# Return Values..: Data/Time object
# =================================================================================================================================
Function Get-LogonTimeUnixTime {
param (
[Parameter()]
[string]$unixTime
)
[datetime]$origin = '1970-01-01 00:00:00'
return $origin.AddSeconds($unixTime).ToLocalTime()
}
# @FUNCTION@ ======================================================================================================================
# Name...........: Get-FileVersion
# Description....: Method to return a file version
# Parameters.....: File Path
# Return Values..: File version
# =================================================================================================================================
Function Get-FileVersion {
<#
.SYNOPSIS
Method to return a file version
.DESCRIPTION
Returns the File version and Build number
Returns Null if not found
.PARAMETER FilePath
The path to the file to query
#>
param ($filePath)
Begin {
}
Process {
$retFileVersion = $Null
try {
If (($null -ne $filePath) -and (Test-Path $filePath)) {
$path = Resolve-Path $filePath
$retFileVersion = ($path | Get-Item | Select-Object VersionInfo).VersionInfo.ProductVersion
}
else {
throw "File path is empty"
}
return $retFileVersion
}
catch {
Throw $(New-Object System.Exception ("Cannot get File ($filePath) version", $_.Exception))
}
finally {
}
}
End {
}
}
# Function for colorized Write-Output
function Use-Color ($fc) {
process {
Write-Host $_ -ForegroundColor $fc
}
}
Function Format-URL($sText) {
if ($sText.Trim() -ne "") {
Write-LogMessage -Type Debug -Msg "Returning URL Encode of $sText"
return [System.Web.HttpUtility]::UrlEncode($sText.Trim())
}
else {
return ""
}
}
Function Get-Secret {
[OutputType([SecureString])]
Param
(
[Parameter(Mandatory = $false)]
[string]$url = $global:PVWAURL,
[Parameter(Mandatory = $false)]
[String]$ID,
[Parameter(Mandatory = $false)]
[hashtable]$logonHeader = $g_LogonHeader
)
$URL_GetSecret = "$url/api/Accounts/$id/Password/Retrieve"
$SecretBody = @{reason = "Pulled for migration via REST" } | ConvertTo-Json -Compress
try {
$secret = Invoke-Rest -Command Post -Uri $URL_GetSecret -Body $SecretBody -header $logonHeader
return $secret