Skip to content

Instantly share code, notes, and snippets.

@tadcrazio
Created September 3, 2017 22:32
Show Gist options
  • Save tadcrazio/392a4fb45d11fc1ec7bbbfe5c302ba55 to your computer and use it in GitHub Desktop.
Save tadcrazio/392a4fb45d11fc1ec7bbbfe5c302ba55 to your computer and use it in GitHub Desktop.
URL Shortener
function URL
{
[PoshBot.BotCommand(
Command = $false,
CommandName = 'URL',
TriggerType = 'Regex',
Regex = '^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)|[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}[^ ]+'
)]
[cmdletbinding()]
param(
[parameter(ValueFromRemainingArguments)]
[object[]]$Arguments
)
try {
$key = '{APIKEYHERE}'
$toshorten = (new-object PSObject |
Add-Member -Passthru NoteProperty longUrl $url )|convertto-json
$goo = Invoke-RestMethod -Uri https://www.googleapis.com/urlshortener/v1/url?key=$key -Body $toshorten -ContentType application/json -Method Post
Write-Output "Nice link, I made it better $($goo.id)"
}
catch [System.Net.Webexception]
{
$Error | Out-File C:\Scripts\logs\t.log -Append
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment