Created
September 16, 2015 06:04
-
-
Save WilliamBerryiii/5bf63a2601341ac49b63 to your computer and use it in GitHub Desktop.
Powershell v3.0+ to toggle the medium type of a Scriptrock node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$target = "https://my.scriptrock.com" | |
$apiKey = "api_key" | |
$secretKey = "secret_key" | |
$nodeName = 'node_name' | |
$ErrorActionPreference = "Stop" | |
# A reuseable enum for meduim types | |
Add-Type -TypeDefinition @" | |
public enum MediumType | |
{ | |
AGENT = 1, | |
SSH = 3, | |
TELNET = 5, | |
HTTPS = 6, | |
WINRM = 7, | |
SERVICE = 8, | |
WEB = 9, | |
} | |
"@ | |
# build authorization header | |
$token = $apiKey + $secretKey | |
$authToken = 'Token token="' + $token + '"' | |
$headers = @{'Authorization'=$authToken;'Content-Type'='application/json'; 'Accept'='application/json'} | |
# get host node id from host name | |
$resource = "/api/v1/nodes/lookup.json?name=$nodeName" | |
$lookUpUrl = $applianceUrl + $resource | |
$hostId = (Invoke-RestMethod -Method Get -Uri $lookUpUrl -Headers $headers -TimeoutSec 300).node_id | |
# get nodes current medium type for restoration later | |
$resource = "api/v1/nodes/$hostId.json" | |
$nodeUrl = $applianceUrl + $resource | |
$currentMediumType = (Invoke-RestMethod -Method GET -Uri $nodeUrl -Headers $headers -TimeoutSec 300).medium_type | |
$agentMediumType = ([MediumType]::AGENT -as [int]) | |
# set node medium type to 'agent' | |
Invoke-WebRequest -Method PUT -Uri $nodeUrl -Headers $headers -Body "{`"node`": {`"medium_type`": $agentMediumType }}" | |
# do deployment stuff | |
if((Invoke-RestMethod -Method GET -Uri $nodeUrl -Headers $headers -TimeoutSec 300).medium_type -eq $agentMediumType){ | |
write-host 'Do Deployment things here.' | |
} | |
# reset node to original medium type | |
Invoke-WebRequest -Method put -Uri $nodeUrl -Headers $headers -Body "{`"node`": {`"medium_type`": $currentMediumType}}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment