Powershell: Send Tweet

Powershell Header

For some reason your Powershell script should send a Tweet. With this function Powershell will send a message to Twitter.

function Send-Tweet($Message)
{
#Twitter Login
$Twitter_UserName = "yourname"
$Twitter_Password = "yourpassword"
$Twitter_URL = "https://twitter.com/statuses/update.xml?status="

$Twitter_Request = [System.Net.WebRequest]::Create($Twitter_URL + $Message)
$Twitter_Request.credentials= New-Object System.Net.NetworkCredential($Twitter_UserName,$Twitter_Password)
$Twitter_Request.method= "POST"
$Twitter_Request.contentType = "application/x-www-form-urlencoded"
$Twitter_Request.GetResponse().statusCode # return the status code of the request
}

You can now call this function and add a message

Send-Tweet "This is my first Tweet from Powershell"

Twitter

If you need a interactive script you can simply use this (seen by Bhargav’s IT Playground)

function Send-Tweet($Message,$UserName){
if($Message-eq$null){$Message=Read-Host"Enter your tweet"}
if($Username-eq$null){$Username=read-host"Enter your twitter username"}
if($Password-eq$null)
{
$Password=read-host-assecurestring"Enter your twitter password"
$marshal=[Runtime.InteropServices.Marshal]
$Password=$marshal::PtrToStringAuto($marshal::SecureStringToBSTR($Password))
}
$url="https://twitter.com/statuses/update.xml?status=$Message"
$request=[System.Net.WebRequest]::Create($url)
$request.credentials=New-Object System.Net.NetworkCredential($UserName,$Password)
$request.method="POST"
$request.contentType ="application/x-www-form-urlencoded"
$request.GetResponse().statusCode # return the status code of the request}

Powershell: Parsing XML part 1

Powershell Header

In my company we started to use XML files as configuration files. So we use it for some configurations of servers or automation for our robots. In Powershell there is a pretty easy way for parsing XML. In Powershell there is an object type for XML, so you just can use the get-content Cmdlet to read the XML file into the object.


$xmldata = get-content "C:\XMLFiles\mydata.xml"

When you create a function for reading XML files you also can set the XML object to global.


$global:xmldata = get-content "C:\XMLFiles\mydata.xml

Now $xmldata is a object which includes all the data of the mydata.xml.

Lets view the content of the mydata.xml file. We need this to understand the following commands.

<TodoList ID = “Week21″>

<Task action=”create” ID=”1″>
<Name>Peter</Name>
<Dept>Administration</Dept>
<email>peter@contoso.com</email>
</Task>

<Task action=”create” ID=”2″>
<Name>Thomas</Name>
<Dept>Administration</Dept>
<email>thomas@contoso.com</email>
</Task>

<Task action=”delete” ID=”3″>
<Name>Steve</Name>
<Dept>IT</Dept>
<email>steve@contoso.com</email>
</Task>

</TodoList>

Now we wanna get all unique Accounts listed

$xmldata.TodoList.Task | %{$_.Name} | select-object -unique

Peter
Thomas
Stefan

We can also create a new object for a specific Task

[Object]$xmltask2 = $xmldata.TodoList.Task | Where-Object {$_.ID -eq "2"}

Now we have a new object with data from Task ID 2. If we wanna see the name from this we just use the same command with the new object

$xmltask3 | %{$_.Name}

Thomas

We can do a lot more with this, but I will bring this up in the next blog posts.

Game Changer Safari 5

Safari 5

Apple just released version 5 of Safari. I work with Safari as my default browser since version 4. I use Safari because its one of the fastest and best integrated browsers out there. It’s not just the speed when you load a webpage, its how fast the browser feels if you open it or you open a new window or tab. It makes working so much easier.

Safari 5

The only thing I missed were extensions for it. Now Apple not just made Safari faster and added better Support for HTML5, they also added extensions. I could not be happier about that. Two days after the Safari 5 release there are a lot of extensions out there. You can find some on http://safariextensions.tumblr.com.

Btw, the integrated developer tools for web developers are just awesome. A lot of colleges in my company changed their default browser from Firefox, Chrome or even Internet Explorer to Safari 5.