Power Shell-isms: Tales from a Linux Developer

I will admit, I am a GNU/Linux developer and proud of it, but my customers run windows systems. So where does that leave me? Generally, either programming in portable C/C++, Perl, Python, or TCL. However, I wanted to give Power Shell a try and outside of a few minor (okay major) issues it works quite well.

I documented some of my issues on my PowerShell for Windows and Linux developers TechTarget blog post, but since this post I have discovered other powershell issues.

First there does not seem to be a well defined manual. I expected to find something like http://www.php.net and http://www.perl.org but that is missing. So how do you get help while programming? By having a Powershell window open and creating your object then running $object | Get-Member. So for hash functions you would use the following:

$h=@{}
$h|Get-Member

While laborious it does work, yet it does not document everything. Once more for hashes, it does not document that you can layer hashes within hashes or that the hash keys must not only match in value, but must match in type. Some more examples:

$h=@{}
$h.$type=@{}

is the same as

$h=@{}
$h.Add($type,$null)
$h.$type=@{}

and is the same as

$h=@{}
$h[$type]=@{}

But to reference hash element represented by $type, you must use the same type as you did when creating the element. So if you did something like:

$h=@{}
$type=”L”
$h.$type=”2.3″
foreach($c in “LT”.GetEnumerator()) {
$type=$h[$c] }

$type would contain NULL as $c is of type [char] and not of type [string] which was used to define the element of the hash in the first place. To solve this you need to cast $c to a [string] first using something like the following:

$h=@{}
$type=”L”
$h.$type=”2.3″
foreach($a in “LT”.GetEnumerator()) {
$c=[string]$a
$type=$h[$c] }

There you have it, some quick notes on hash and help that will hopefully help other powershell developers who are also Linux developers. I would also like to thank the Powershell folks on Twitter and irc.freenode.net #powershell IRC room for their help in understanding these issues.

Edward Haletky

Edward L. Haletky, aka Texiwill, is an author, analyst, developer, technologist, and business owner. Edward owns AstroArch Consulting, Inc., providing virtualization, security, network consulting and development and TVP Strategy where he is also an Analyst. Edward is the Moderator and Host of the Virtualization Security Podcast as well as a guru and moderator for the VMware Communities Forums, providing answers to security and configuration questions. Edward is working on new books on Virtualization.

Leave a Reply

Your email address will not be published. Required fields are marked *

11 + seventeen =