-
Notifications
You must be signed in to change notification settings - Fork 0
/
ODReboot.ps1
46 lines (39 loc) · 1.71 KB
/
ODReboot.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
$OctopusURL = "http://localhost/"
$OctopusAPIKey = "#{OD_Api_Key}"
$MachineName = "#{Octopus.machine.name}" #Machine Display Name \
$SpaceId = "#{Octopus.Space.Id}"
$header = @{ "X-Octopus-ApiKey" = $OctopusAPIKey }
$allVms = Invoke-RestMethod $OctopusURL/api/$SpaceID/machines/all -Method Get -Headers $header
$machineid = ($allVms | Where-Object name -eq "$MachineName").id
if(!$machineid){
throw "Machine ID can't be located."
}
$body = @{
Name = "Health"
Description = "Checking health of $MachineName"
Arguments = @{
Timeout= "00:05:00"
MachineIds = @($machineId) #$MachinID could contain an array of machines too
SpaceId = $SpaceId
}
} | ConvertTo-Json
$time = 0
while($($HC.state -ne "Success" -and $time -le 5)){
if($null -eq $HC.state -or $HC.state -eq "Failed"){
if($null -eq $HC.state){
write-host "Initialising check."
}else{
write-host "Since State is failed, trying again."
}
$initHealthCheck = Invoke-RestMethod $OctopusURL/api/$SpaceId/tasks -Method Post -Body $body -Headers $header
$time += 1
write-host "INFO: Number of runs so far: $time, State: $($initHealthCheck.State) , TaskId $($initHealthCheck.id), Description: $($initHealthCheck.Description) "
}
$HC = Invoke-RestMethod $OctopusURL/api/$SpaceId/tasks/$($initHealthCheck.Id) -Method get -Headers $header
Write-Host "State: $($HC.state), Description: $($initHealthCheck.Description), Space: $($HC.SpaceId), Times: $time"
}
if($HC.state -eq "Success"){
write-host "SUCCESS: $($initHealthCheck.Description)"
}else{
Write-Error "FAILED: $($initHealthCheck.Description)"
}