@@ -9,8 +9,10 @@ function on_error {
9
9
$errorLine = $_.InvocationInfo.ScriptLineNumber
10
10
$errorMessage = $_.Exception
11
11
12
+ $instance_id = (Invoke-WebRequest - UseBasicParsing http:// 169.254 .169.254 / latest/ meta- data/ instance- id).content
13
+
12
14
aws autoscaling set-instance - health `
13
- -- instance- id " (Invoke-WebRequest -UseBasicParsing http://169.254.169.254/latest/meta-data/instance-id).content " `
15
+ -- instance- id " $instance_id " `
14
16
-- health- status Unhealthy
15
17
16
18
cfn- signal `
@@ -117,11 +119,32 @@ Set-PSDebug -Trace 0
117
119
118
120
Write-Output " Creating buildkite-agent user account in Administrators group"
119
121
120
- $Count = Get-Random - min 24 - max 32
121
- $Password = -join ((65 .. 90 ) + (97 .. 122 ) + (48 .. 57 ) | Get-Random - Count $Count | ForEach-Object {[char ]$_ })
122
122
$UserName = " buildkite-agent"
123
123
124
- New-LocalUser - Name $UserName - PasswordNeverExpires - Password ($Password | ConvertTo-SecureString - AsPlainText - Force) | out-null
124
+ $StopLoop = $false
125
+ [int ]$RetryCount = " 0"
126
+
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
+ 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 ]$_ })
133
+
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 )
125
148
126
149
If ($Env: BUILDKITE_WINDOWS_ADMINISTRATOR -eq " true" ) {
127
150
Add-LocalGroupMember - Group " Administrators" - Member $UserName | out-null
0 commit comments