Power Shell-isms continued: Tales from a Linux Developer Part II

I have been merrily going along my way developing a fairly massive XML file to be used by the PowerShell script I am crafting. After making several changes I have more comments on PowerShell to make.

  1. Do not call iex or Invoke-Expression from within a PowerShell script, these commands do odd things with the memory image of a script. Perhaps it is the nature of my script, where I am passing long Linux shell commands through the iex command.  For some reason, iex decided to interpret the Linux shell command as PowerShell but only for 5 of 400 or so commands.  This inconsistent behavior took several weeks to uncover. As an aside iex caused me similar problems when using it within a simple comparison statement, $cv=iex “$vc -$cmp $pval”, where $vc and $pval were both integers and $cmp was actually set to eq. So it was trying to do an equality comparison.
  2. Never use > as a means for doing a greater-than comparison. Instead the proper operator is -gt. This caused another week of debugging to discover. Once you use > then debuggers tend to have serious issues, like PowerGUI and they loose track of the program.

These two gotchas took nearly forever to find, the first apparently leads to some sort of memory overlay and the second causes debuggers and other things to behave oddly.

When you are running a program and suddenly the output is showing the actual PowerShell script instead of what was expected, you know there are serious memory issues going on. When programming Linux scripts we would call that a stack corruption.

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 *

7 − 6 =