Skip to content

Instantly share code, notes, and snippets.

@henkmeulekamp
Last active May 7, 2018 08:05
Show Gist options
  • Save henkmeulekamp/78ac923abad62c62adaa to your computer and use it in GitHub Desktop.
Save henkmeulekamp/78ac923abad62c62adaa to your computer and use it in GitHub Desktop.
Powershell script to inform NewRelic of deployment; parameterized
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 +"&amp;deployment[application_id]=" + $appid +"&amp;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