Skip to content

Instantly share code, notes, and snippets.

@Nora-Ballard
Created April 26, 2014 06:39
Show Gist options
  • Select an option

  • Save Nora-Ballard/11313369 to your computer and use it in GitHub Desktop.

Select an option

Save Nora-Ballard/11313369 to your computer and use it in GitHub Desktop.
Shorten URL using goo.gl
function Get-GoogleShortURL {
param(
[Parameter(Mandatory, ValueFromPipeline)]
[ValidateScript({
[System.Uri]::TryCreate($_, [System.UriKind]::RelativeOrAbsolute , [ref]$_)
})]
[string]$longUrl
)
PROCESS
{
$SystemWebProxy = [System.Net.WebRequest]::GetSystemWebProxy()
$WebRequest = @{
'Uri' = 'https://www.googleapis.com/urlshortener/v1/url'
'Method' = [System.Net.WebRequestMethods+Http]::Post
'Body' = '{"longUrl": "' + $longUrl + '"}'
'ContentType' = 'application/json'
}
if (!$SystemWebProxy.IsBypassed($WebRequest.Uri)) {
$Proxy = $SystemWebProxy.GetProxy($WebRequest.Uri)
$WebRequest.Add('Proxy',$Proxy.AbsoluteUri)
$WebRequest.Add('ProxyUseDefaultCredentials',$true)
}
$Response = Invoke-WebRequest @WebRequest
if ($Response.statusCode -eq 200) {
$Content = ConvertFrom-Json $Response.Content
Write-Output $Content
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment