{"id":450,"date":"2008-06-23T05:59:45","date_gmt":"2008-06-23T13:59:45","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/2008\/06\/23\/out-twitter\/"},"modified":"2008-06-23T06:02:58","modified_gmt":"2008-06-23T14:02:58","slug":"out-twitter","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2008\/06\/23\/out-twitter\/","title":{"rendered":"Out-Twitter"},"content":{"rendered":"<p>I finally finished my version of a PowerShell Out-Twitter function. This allows me to send a quick tweet directly from the PowerShell console. The original and inspiring Out-Twitter can be found at <a href=\"http:\/\/cid-5dec3b62d9308943.skydrive.live.com\/browse.aspx\/PowerShell%20Scripts\/Twitter\">http:\/\/cid-5dec3b62d9308943.skydrive.live.com\/browse.aspx\/PowerShell%20Scripts\/Twitter<\/a>. My version will used cached credentials, check for tweet length and replace any URLs with Snurl&#8217;d URLs.<\/p>\n<p>Here&#8217;s the function, which is also <a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/file\/Out-Twitter.txt\">attached<\/a>.<\/p>\n<p><font face=\"Consolas\" size=\"2\">#Out-Twitter.ps1<br \/>\n#v1.0&nbsp; June 23, 2008 <\/font><\/p>\n<p><font face=\"Consolas\" size=\"2\">#Jeffery Hicks<br \/>\n#blog.sapien.com <\/font><\/p>\n<p><font face=\"Consolas\" size=\"2\">#Originally downloaded from<br \/>\n#http:\/\/cid-5dec3b62d9308943.skydrive.live.com\/browse.aspx\/PowerShell%20Scripts\/Twitter<\/font><\/p>\n<p><font face=\"Consolas\" size=\"2\">Function Out-Twitter { <\/font><\/p>\n<p><font face=\"Consolas\" size=\"2\">&nbsp;&nbsp;&nbsp; BEGIN {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #check for global Twitter credential<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!$global:Twitter_Credential) {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $global:Twitter_Credential=Get-Credential<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br \/>\n&nbsp;&nbsp;&nbsp; } <\/font><\/p>\n<p><font face=\"Consolas\" size=\"2\">&nbsp;&nbsp;&nbsp; PROCESS { <br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #turn off error pipeline<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $erroractionpreference=&quot;SilentlyContinue&quot;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [string]$tweet=$_<\/font><\/p>\n<p><font face=\"Consolas\" size=\"2\"> <\/font><\/p>\n<p><font face=\"Consolas\" size=\"2\"><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Function Get-Snurl {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Param([string]$link=&quot;<\/font><font face=\"Consolas\" size=\"2\">http:\/\/www.google.com&quot;)<\/font><br \/>\n<font face=\"Consolas\" size=\"2\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $webclient=New-Object Net.WebClient<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $url=&quot;<\/font><font face=\"Consolas\" size=\"2\">http:\/\/snipurl.com\/site\/snip?r=simple&amp;link=<\/font><font face=\"Consolas\" size=\"2\">$link&quot;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $response=$webclient.DownloadString(&quot;$url&quot;)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write $response<br \/>\n&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp; <br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #search for web links using Regex<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [regex]$regex=&quot;(http|ftp|https):\\\/\\\/[\\w\\-_]+(\\.[\\w\\-_]+)+([\\w\\-\\.,@?^=%&amp;amp;:\/~\\+#]*[\\w\\-\\@?^=%&amp;amp;\/~\\+#])?&quot;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If ($tweet -match $regex) {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $link=$matches[0]<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #if an embedded url was found, convert it to a snurl link<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ($link) {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $snurl=Get-Snurl $link<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $tweet=$tweet.Replace($link,$snurl)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ($tweet.Length -gt 140) {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Write-Warning (&quot;Your message is &quot; + (($tweet.length)-140)&nbsp; + &quot; characters too long.&quot;)<br \/>\n<\/font>&nbsp; <font face=\"Consolas\" size=\"2\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $clear=$True<\/font><font><br \/>\n<font face=\"Consolas\" size=\"2\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $url = &quot;http:\/\/twitter.com\/statuses\/update.xml&quot;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $data = &quot;status={0}&quot; -f $tweet<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $request = [Net.WebRequest]::Create($url)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $username=$global:Twitter_Credential.GetNetworkCredential().Username<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $password=$global:Twitter_Credential.GetNetworkCredential().Password<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $request.Credentials = New-Object System.Net.NetworkCredential($username,$password)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $request.ContentType = &quot;application\/x-www-form-urlencoded&quot;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $request.Method = &quot;POST&quot;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $bytes = [System.Text.Encoding]::UTF8.GetBytes($data)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $request.ContentLength = $bytes.Length<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $requestStream = [System.IO.Stream]$request.GetRequestStream()<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $requestStream.write($bytes, 0, $bytes.Length)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $response = $request.GetResponse()&nbsp;&nbsp;&nbsp; <br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ($response) {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $reader = [System.IO.StreamReader]$response.GetResponseStream()<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ([xml]$reader.ReadToEnd()).status | select Created_At,id<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Write-Warning &quot;Oops! There was a problem with your tweet.&quot;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Write-Warning $error[0] | select *<br \/>\n<\/font><\/font><font face=\"Consolas\" size=\"2\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $clear=$True<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br \/>\n&nbsp;&nbsp;&nbsp; END {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #if there was an error, then don&#8217;t keep the global Twitter <br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #credential<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ($clear) {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Remove-Variable Twitter_Credential -scope Global<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br \/>\n} #end Function <\/font><\/p>\n<p>&nbsp;<\/p>\n<p>The function is designed to take a string of text from the PowerShell console and accept it as pipelined input;<\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" size=\"2\">PS C:\\&gt; &quot;Drinking coffee&quot; | out-twitter<\/font><\/p>\n<p>The first time you use it you will be prompted for your Twitter credential using Get-Credential. Enter your Twitter name and password.&nbsp; This information is saved as a global variable. The Begin script block checks if the variable exists and prompts you if it doesn&#8217;t.<\/p>\n<p>The Process script block takes each piped in string and uses a regular expression pattern to see if it has a URL. If so, the link is sent to the Get-Snurl function which returns the short url, which in turn is inserted into the tweet, replacing the original URL. As written, this will only convert the first URL it finds.<\/p>\n<p>The tweet length is checked and if less than 140 characters it is sent using the Webclient object.&nbsp; If there is an error, it will be displayed and the saved credential variable will be cleared.&nbsp; I didn&#8217;t want to save the variable unless the tweet was successful.&nbsp; The credential variable will also be cleared if your tweet is too long. This is done in the End script block.<\/p>\n<p>One thing I like about this is that you can send a long message to Twitter by breaking it up into smaller tweets:<\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" size=\"2\">PS C:\\&gt; &quot;I have a lot to say.&quot;,&quot;Here is the second part&quot;,&quot;Here is the third part&quot; | out-twitter<\/font><\/p>\n<p>If the tweet is successful you&#8217;ll get some information from the web response:<\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" size=\"2\">created_at&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; id<br \/>\n&#8212;&#8212;&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;<br \/>\nMon Jun 23 13:56:25 +0000 2008&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 841650275<\/font><\/p>\n<p>I&#8217;m sure you can come up with additional improvements and enhancements. I&#8217;d love for the Tweet to say &quot;From Out-Twitter&quot;, but there&#8217;s no web app that I&#8217;m using, just a script that uses the WebClient object to send via the Twitter web site, which is why tweets say &quot;from web&quot;.<\/p>\n<p>You can follow me on Twitter <a target=\"_blank\" href=\"http:\/\/www.twitter.com\/JeffHicks\">@JeffHicks<\/a><\/p>\n<p>Enjoy!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I finally finished my version of a PowerShell Out-Twitter function. This allows me to send a quick tweet directly from the PowerShell console. The original and inspiring Out-Twitter can be found at http:\/\/cid-5dec3b62d9308943.skydrive.live.com\/browse.aspx\/PowerShell%20Scripts\/Twitter. My version will used cached credentials, check for tweet length and replace any URLs with Snurl&#8217;d URLs.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2,25],"tags":[28,185],"class_list":["post-450","post","type-post","status-publish","format-standard","hentry","category-general","category-windows-powershell","tag-powershell","tag-twitter"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/450","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=450"}],"version-history":[{"count":0,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/450\/revisions"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=450"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=450"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=450"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}