Jump to content
Welcome to our new Citrix community!
  • 0

Citrix Cloud REST API - Reboot of machines with parameter force


Stefan van Boxmer-Fischer

Question

Hi,

 

how can I reboot VMs trough REST API with parameter force=true?  I try this with the REST API Reboot CVAD REST APIs (cloud.com) with parameter. But it dont't work as expected. Reboot of machine happens but it is a normal reboot sequence without parameter force.

 

Here is my Script (based on the example from Citrix Website for rebooting Idle Machines):

 

# Command-line parameters
# Note use of the Guid type - invalid GUID's will be automatically rejected
  [Parameter(Mandatory=$true)][string]$customerId,
  [Parameter(Mandatory=$true)][Guid]$siteId,
  [Parameter(Mandatory=$true)][Guid]$catalogId,
  [Parameter(Mandatory=$true)][int]$MaxBootCount
)

$proxy = 'http://xyz.example.com:8080'

# Use endpoints based on the geographical region you selected while creating the Citrix Cloud account.
# api-ap-s.cloud.com – If your Citrix Cloud account is set to the Asia Pacific South region.
# api-eu.cloud.com – If your Citrix Cloud account is set to the European Union region.
# api-us.cloud.com – If your Citrix Cloud account is set to the United States region.
$tokenUrl = 'https://api-eu.cloud.com/cctrustoauth2/root/tokens/clients'

# Obtain bearer token from authorization server
$response = Invoke-WebRequest $tokenUrl -Proxy $proxy -Method POST -Body @{
  grant_type = "client_credentials"
  client_id = $env:CLIENT_ID
  client_secret = $env:CLIENT_SECRET
}
$token = $response.Content | ConvertFrom-Json

# Create our headers hash table
$headers = @{
  Authorization = "CwsAuth Bearer=$($token.access_token)"
  'Citrix-CustomerId' = $customerId
  Accept = 'application/json'
}

$body = @{
force = "true";
}

# Get a list of machines in the specified catalog
$response = Invoke-WebRequest "https://api-eu.cloud.com/cvadapis/$siteId/MachineCatalogs/$catalogId/Machines" -Proxy $proxy -Headers $Headers
$machines = $response | convertFrom-Json
$counter = 0
foreach ($machine in $machines.Items) {
#  if ($machine.RegistrationState -like "*Unregistered*" -and $machine.FaultState -like "*Unregistered*" -and $machine.SummaryState -like "*Unregistered*" -and $machine.SessionCount -eq 0 -and $machine.PowerActionPending -eq "True" -and $machine.PowerState -eq "TurningOff") {
   if ($machine.RegistrationState -like "*Unregistered*" -and $machine.FaultState -like "*Unregistered*" -and $machine.SummaryState -like "*Unregistered*" -and $machine.SessionCount -eq 0) {
        if ($counter -lt $MaxBootCount) {
            $counter++
            $status = 'success'
            try {
                # Note - the $ in the URL must be escaped with a ` and the content type must be JSON
                $response = Invoke-WebRequest "https://api-eu.cloud.com/cvadapis/$siteId/Machines/$($machine.Id)/`$reboot" `
                -Method POST `
                -Headers $headers `
                -Body ($body | ConvertTo-Json) `
                -Proxy $proxy `
                -ContentType "application/json"
            } catch [Microsoft.PowerShell.Commands.HttpResponseException] {
                # Catch HTTP errors from the API (400, 500 etc)
                $msg = $_.ErrorDetails.Message | ConvertFrom-Json
                $status = "failure: $($msg.ErrorMessage)"
            } catch {
                # Catch anything else
                $status = "failure: $($_.ErrorDetails.Message)"
            }
            # It's not possible to Write-Output without a new-line in PowerShell, so output the whole line at once
            Write-Output "Requesting reboot for stalled host $counter $($machine.Name)... $status"
        } else {
            Write-Host "Reached MaxBootCount which is $MaxBootCount - We will stop here for now..."
            break
        }
    }
}

 

Greetings,

Stefan

 

Link to comment

1 answer to this question

Recommended Posts

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...