@@ -119,32 +119,29 @@ Set-PSDebug -Trace 0
119
119
120
120
Write-Output " Creating buildkite-agent user account in Administrators group"
121
121
122
- $UserName = " buildkite-agent"
122
+ $lowerChars = [char []](97 .. 122 ) # a-z
123
+ $upperChars = [char []](65 .. 90 ) # A-Z
124
+ $numbers = [char []](48 .. 57 ) # 0-9
125
+ $specialChars = [char []](40 , 41 , 33 , 64 , 36 , 37 , 45 , 61 , 46 , 63 , 42 , 59 , 38 ) # ()!@$%-=.?*;&
123
126
124
- $StopLoop = $false
125
- [ int ] $RetryCount = " 0 "
127
+ $minPasswordLength = 32
128
+ $randomChars = @ ()
126
129
127
- # a Try/Catch block is used in a loop to make a few extra attempts at creating the user account before finally giving up and failing
128
- # because sometimes the generated random password does not satisfy the system's password policy
129
130
Do {
130
- Try {
131
- $Count = Get-Random - min 24 - max 32
132
- $Password = -join ((65 .. 90 ) + (97 .. 122 ) + (48 .. 57 ) | Get-Random - Count $Count | ForEach-Object {[char ]$_ })
131
+ $randomChars += Get-Random - Count 1 - InputObject $lowerChars
132
+ $randomChars += Get-Random - Count 1 - InputObject $upperChars
133
+ $randomChars += Get-Random - Count 1 - InputObject $numbers
134
+ $randomChars += Get-Random - Count 1 - InputObject $specialChars
133
135
134
- New-LocalUser - Name $UserName - PasswordNeverExpires - Password ($Password | ConvertTo-SecureString - AsPlainText - Force) | out-null
135
- $StopLoop = $true
136
- }
137
- Catch {
138
- If ($RetryCount -gt 10 ){
139
- Write-Output " Could not create $UserName user after 10 retries."
140
- exit 1
141
- }
142
- Else {
143
- Write-Output " Could not create $UserName user, retrying..."
144
- $RetryCount = $RetryCount + 1
145
- }
146
- }
147
- } While ($StopLoop -eq $false )
136
+ # randomize the order of the random characters
137
+ $randomChars = Get-Random - Count $randomChars.Length - InputObject $randomChars
138
+ } While ($randomChars.Length -lt $minPasswordLength )
139
+
140
+ $Password = -join $randomChars
141
+
142
+ $UserName = " buildkite-agent"
143
+
144
+ New-LocalUser - Name $UserName - PasswordNeverExpires - Password ($Password | ConvertTo-SecureString - AsPlainText - Force) | out-null
148
145
149
146
If ($Env: BUILDKITE_WINDOWS_ADMINISTRATOR -eq " true" ) {
150
147
Add-LocalGroupMember - Group " Administrators" - Member $UserName | out-null
0 commit comments