Skip to content

Instantly share code, notes, and snippets.

@kfrancis
Created July 23, 2012 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save kfrancis/3164709 to your computer and use it in GitHub Desktop.
Save kfrancis/3164709 to your computer and use it in GitHub Desktop.
PowerShell Method for Telling NewRelic of Deployment
if ($OctopusEnvironmentName -eq "Production") {
# in production, we need to
#Create a URI instance since the HttpWebRequest.Create Method will escape the URL by default.
$URL = "http://api.newrelic.com/deployments.xml"
$post = "deployment[account_id]=<id here>&deployment[user]=OctopusDeploy&deployment[app_id]=<app id here>&deployment[revision]=($OctopusPackageVersion)"
$URI = New-Object System.Uri($URL,$true)
#Create a request object using the URI
$request = [System.Net.HttpWebRequest]::Create($URI)
#Build up a nice User Agent
$request.UserAgent = $(
"{0} (PowerShell {1}; .NET CLR {2}; {3})" -f $UserAgent,
$(if($Host.Version){$Host.Version}else{"1.0"}),
[Environment]::Version,
[Environment]::OSVersion.ToString().Replace("Microsoft Windows ", "Win")
)
# $creds = New-Object System.Net.NetworkCredential($Username,$Password)
# $request.Credentials = $creds
#Since this is a POST we need to set the method type
$request.Method = "POST"
$request.Headers.Add("x-api-key", "<api key here>");
#Set the Content Type as text/xml since the content will be a block of xml.
$request.ContentType = "application/x-www-form-urlencoded"
$request.Accept = "text/xml"
try {
#Create a new stream writer to write the xml to the request stream.
$stream = New-Object IO.StreamWriter $request.GetRequestStream()
$stream.AutoFlush = $True
$PostStr = [System.Text.Encoding]::UTF8.GetBytes($Post)
$stream.Write($PostStr, 0,$PostStr.length)
$stream.Close()
#Make the request and get the response
$response = $request.GetResponse()
$response.Close()
if ([int]$response.StatusCode -eq 201) {
Write-Host "NewRelic Deploy API called succeeded."
} else {
Write-Host "NewRelic Deploy API called failed."
}
} catch [System.Net.WebException] {
$res = $_.Exception.Response
Write-Host "NewRelic Deploy API called failed."
}
}
@tbenade
Copy link

tbenade commented Apr 13, 2013

thanks for the starting point

@wilsontayar
Copy link

This is awesome, thanks!
Soon I'll try to do it with the Invoke-WebRequest cmdlet for posh 3+. :)
http://technet.microsoft.com/en-us/library/hh849901(v=wps.630).aspx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment