Skip to content

Instantly share code, notes, and snippets.

@jheiselman
Created December 14, 2016 18:57
Show Gist options
  • Select an option

  • Save jheiselman/df983ea5f9f34ea59b3e370333b6655c to your computer and use it in GitHub Desktop.

Select an option

Save jheiselman/df983ea5f9f34ea59b3e370333b6655c to your computer and use it in GitHub Desktop.
Example of how to use JSON in PowerShell
if ($username -eq "" -or $api_key -eq "" -or $serverId -eq "") {
echo "Please edit this script and fill in the variables at the top before using it"
exit
}
$auth_body = @{
auth = @{
"RAX-KSKEY:apiKeyCredentials" = @{
username = ""
apiKey = ""
}
}
}
$auth_uri = "https://identity.api.rackspacecloud.com/v2.0/tokens"
$auth_body.auth.'RAX-KSKEY:apiKeyCredentials'.username = $username
$auth_body.auth.'RAX-KSKEY:apiKeyCredentials'.apiKey = $api_key
$authentication_response = Invoke-RestMethod -Uri $auth_uri -Method Post -Body (ConvertTo-Json $auth_body) -Headers @{"Accept"= "application/json"; "Content-Type" = "application/json"}
$tenantId = ($authentication_response.access.user.roles | Where-Object -Property name -EQ "compute:default").tenantId
$console_uri = "https://ord.servers.api.rackspacecloud.com/v2/$tenantId/servers/$serverId/action"
$console_body = @{
"os-getVNCConsole" = @{
type = "novnc"
}
}
$console_response = Invoke-RestMethod -Uri $console_uri -Method Post -Headers @{"Accept" = "application/json"; "Content-Type" = "application/json"; "X-Auth-Token" = $authentication_response.access.token.id} -Body (ConvertTo-Json $console_body)
echo $console_response.console.url
#start $console_response.console.url
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment