Created
July 23, 2012 16:58
-
-
Save kfrancis/3164709 to your computer and use it in GitHub Desktop.
PowerShell Method for Telling NewRelic of Deployment
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
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." | |
} | |
} |
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
thanks for the starting point