Powershell: check variable for null

Powershell Header

If your working a lot with parameters and inputs you need to check if variables have the right value and are not “null”.

The normal if -eq “$null” doesn’t work:

if ($varibalename -eq $null) { Write-Host "variable is null" }

Now how you can check that (check if $variablename has $null as value):

if (!$variablename) { Write-Host "variable is null" }

And here if you wanna check if $variablename has any value except $null:

if ($variablename) { Write-Host "variable is NOT null" }

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.

Powershell: Invoke-Expression

Powershell Header

Using the Invoke-Expression Cmdlet:

The Invoke-Expression cmdlet provides one way to run a script from within Windows PowerShell.

Invoke-Expression c:\scripts\test.ps1

or

& c:\scripts\test.ps1

And you also can run a function in your Powershell Script by a variable. This is something really important when you work with dynamic scripts:

Invoke-Expression -command ($Function_Name)

you even can send dynamic parameters to this function:

Invoke-Expression -command ($Function_Name + ' $Function_Parameters')