Last active
May 7, 2018 08:05
-
-
Save henkmeulekamp/78ac923abad62c62adaa to your computer and use it in GitHub Desktop.
Powershell script to inform NewRelic of deployment; parameterized
This file contains hidden or 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
param ( | |
[string]$appname = "<your-newrelic-appname>", | |
[string]$appid = "<your-newrelic-appid>", | |
[string]$apiKey = "<your-newrelic-api-key>" | |
) | |
function Execute-HTTPPostCommand() { | |
param( | |
[string] $target = $null, | |
[string] $postParam, | |
[string] $key | |
) | |
$webRequest = [System.Net.WebRequest]::Create($target) | |
$PostStr = [System.Text.Encoding]::UTF8.GetBytes($postParam) | |
$webrequest.ContentLength = $PostStr.Length | |
$webRequest.ServicePoint.Expect100Continue = $false | |
$webRequest.Headers.Add("x-api-key", $key); | |
$webRequest.Method = "POST" | |
$requestStream = $webRequest.GetRequestStream() | |
$requestStream.Write($PostStr, 0,$PostStr.length) | |
$requestStream.Close() | |
[System.Net.WebResponse] $resp = $webRequest.GetResponse(); | |
#response check | |
if ([int]$resp.StatusCode -eq 201) { | |
Write-Host "NewRelic Deploy API called succeeded." | |
$rs = $resp.GetResponseStream(); | |
[System.IO.StreamReader] $sr = New-Object System.IO.StreamReader -argumentList $rs; | |
[string] $results = $sr.ReadToEnd(); | |
return $results; | |
} else { | |
Write-Host "NewRelic Deploy API called failed." | |
Write-Host $resp.StatusCode.ToString() + " " $resp.StatusDescription | |
} | |
return "Failed"; | |
} | |
# set post parameters | |
$post = "deployment[app_name]=" + $appname +"&deployment[application_id]=" + $appid +"&deployment[user]=" + $env:username | |
# post url | |
$URL = "https://api.newrelic.com/deployments.xml" | |
# show waht to post | |
Write-Host $post | |
# send data | |
Execute-HTTPPostCommand $URL $post $apiKey | |
#all done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment