Skip to content

Commit 522fe61

Browse files
committed
Add Update-UiPathADRobotPasswords
1 parent fc4a74b commit 522fe61

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
2+
<#
3+
.SYNOPSIS
4+
Updates AD passwords for UiPath Orchestrator robot credentials that are close to password expiration
5+
.DESCRIPTION
6+
You must run this script as an user that has the privilege to reset the robot passwords in the AD. This privilege can be delegated to you by a domain administrator.
7+
You should first import the UiPath.PowerShell module and authenticate yourself with your Orchestrator using Get-UiPathAuthToken before running this script.
8+
.PARAMETER DomainName
9+
The domain to sync users with. It does not necessarily has to be your current user or machine domain, but there must be some trust relationship so your Windows session can discover and interogate this domain AD.
10+
.PARAMETER Days
11+
Update the passwords in AD for the robot credentials that expire sooner than this number of days. Default is 1 Day.
12+
.PARAMETER PasswordLength
13+
The total number of characters for the generated password(s). Default is 16 characters.
14+
.PARAMETER NonAlphaCount
15+
Number of non-alphanumeric characters in the generated password(s). Default is 3 characters.
16+
.EXAMPLE
17+
Update-UiPathADRobotPasswords MyDomain
18+
Updates the passwords for all robots that use credentials from the domain MyDomain
19+
.EXAMPLE
20+
Update-UiPathADRobotPasswords MyDomain -Days 7
21+
Updates the passwords for all robots that use credentials that will expire sooner than 7 days
22+
#>
23+
param(
24+
[Parameter(Mandatory=$true, Position=0)] [string] $DomainName,
25+
[Parameter()] [int]$Days = 1,
26+
[Parameter()] [int]$PasswordLength = 16,
27+
[Parameter()] [int]$NonAlphaCount = 3
28+
)
29+
30+
$ErrorActionPreference = "Stop"
31+
32+
try
33+
{
34+
Write-Verbose "Get-ADDomain -Identity $domainName"
35+
$dc = Get-ADDomain -Identity $domainName
36+
37+
Write-Progress -Activity "Update robot expiring credential AD passwords" `
38+
-CurrentOperation "Discover robots with credentials in $DomainName" `
39+
-PercentComplete 33
40+
41+
42+
# Discover all robots with robot credentials from the requested domain, extract the distinct username used
43+
Write-Verbose "Get-UiPathRobot | Where-Object {`$_.Username -like '$DomainName\*'} | Select-Object -Property Username -Unique"
44+
$robotUserNames = Get-UiPathRobot | Where-Object {$_.Username -like "$DomainName\*"} | Select-Object -Property Username -Unique
45+
46+
Write-Progress -Activity "Update robot expiring credential AD passwords" `
47+
-CurrentOperation "Analyze robot AD users" `
48+
-PercentComplete 66
49+
50+
51+
$i = 1
52+
foreach($robotUserName in $robotUserNames)
53+
{
54+
Write-Progress -Id 1 `
55+
-Activity "Analyze robot AD users" `
56+
-CurrentOperation $robotUserName `
57+
-PercentComplete ($i/$robotUserNames.Count*100)
58+
$i += 1
59+
60+
# Discover the password expiration date for the AD user
61+
62+
$userNameParts = $robotUserName.Username.Split('\')
63+
$userName = $userNameParts[1]
64+
65+
# see https://msdn.microsoft.com/en-us/library/cc223410.aspx for msDS-UserPasswordExpiryTimeComputed attribute spec
66+
67+
Write-Verbose "Get-ADUser -Server $($dc.PDCEmulator) -Identity $userName -Properties `"SamAccountName`",`"msDS-UserPasswordExpiryTimeComputed`""
68+
$userObject = Get-ADUser -Server $dc.PDCEmulator -Identity $userName -Properties "SamAccountName","msDS-UserPasswordExpiryTimeComputed"
69+
70+
$expiration = [datetime]::FromFileTime($userObject."msDS-UserPasswordExpiryTimeComputed")
71+
72+
# skip users not expiring within the given time frame
73+
if (($userObject."msDS-UserPasswordExpiryTimeComputed" -eq 0) -or ($userObject."msDS-UserPasswordExpiryTimeComputed" -eq 0x7FFFFFFFFFFFFFFF))
74+
{
75+
Write-Verbose "Skip:$userName no expiration: $($userObject."msDS-UserPasswordExpiryTimeComputed")"
76+
continue
77+
}
78+
79+
$daysTillExpire = ($expiration - [datetime]::UtcNow).TotalDays
80+
if ( $daysTillExpire -gt $Days)
81+
{
82+
Write-Verbose "Skip:$userName expiration:$expiration days till expire:$daysTillExpire"
83+
continue
84+
}
85+
86+
# generate a random password
87+
# NB: this generation method does not guarantee to meet the AD required complexity rules
88+
# Feel free to fix it to meet whatever rules you have in place
89+
$password = [System.Web.Security.Membership]::GeneratePassword($PasswordLength, $NonAlphaCount)
90+
$securePassword = ConvertTo-SecureString -AsPlainText $password -Force
91+
92+
Write-Verbose "Set-ADAccountPassword -Server $($dc.PDCEmulator) -Identity $userObject -Reset -NewPassword ****"
93+
Set-ADAccountPassword -Server $dc.PDCEmulator -Identity $userObject -Reset -NewPassword $securePassword
94+
95+
# discover all robots that use this robot credential
96+
Write-Verbose "Get-UiPathRobot -Username $($robotUserName.Username)"
97+
$robots = Get-UiPathRobot -Username $robotUserName.Username
98+
$j = 1
99+
foreach($robot in $robots)
100+
{
101+
Write-Progress -Id 2 `
102+
-Activity "Update Orchestrator robots" `
103+
-CurrentOperation $robot.Name `
104+
-PercentComplete ($j/$robots.Count*100)
105+
$j += 1
106+
107+
#update the robot credential with the new password
108+
Write-Verbose "Edit-UiPathRobot -Id $($robot.Id) -Password ****"
109+
Edit-UiPathRobot -Id $robot.Id -Password $password
110+
}
111+
}
112+
}
113+
catch
114+
{
115+
$e = $_.Exception
116+
$klass = $e.GetType().Name
117+
$line = $_.InvocationInfo.ScriptLineNumber
118+
$script = $_.InvocationInfo.ScriptName
119+
$msg = $e.Message
120+
121+
Write-Error "$klass $msg ($script $line)"
122+
}

UiPath.Orchestrator.Powershell.sln

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ EndProject
2424
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Examples", "Examples", "{BBCF7871-E3BE-491E-B522-8616CFACB4E8}"
2525
ProjectSection(SolutionItems) = preProject
2626
Examples\Sync-UiPathADUsers.ps1 = Examples\Sync-UiPathADUsers.ps1
27+
Examples\Update-UiPathADRobotPasswords.ps1 = Examples\Update-UiPathADRobotPasswords.ps1
2728
EndProjectSection
2829
EndProject
2930
Global

0 commit comments

Comments
 (0)