Of recent I have been swimming in the deep end of the PowerShell pool. I discovered to my chagrin how some commands in PowerShell are unsafe. Let me describe what I mean by unsafe, as there are multiple interpretations depending on the context.
A unsafe command is one that interprets empty or undefined variable as to mean the wildcard * .
The example I got into this with is the Exchange 2010 Module's get-mailbox command. In this context "Get-MailBox $_" is equal to "Get-Mailbox" when $_ is undefined or blank.
This means the command pulls all mailboxes, rather that erring out, or querying for data.
One thing I realized is that you could protect the code with a conditional statement.
A unsafe command is one that interprets empty or undefined variable as to mean the wildcard * .
The example I got into this with is the Exchange 2010 Module's get-mailbox command. In this context "Get-MailBox $_" is equal to "Get-Mailbox" when $_ is undefined or blank.
This means the command pulls all mailboxes, rather that erring out, or querying for data.
One thing I realized is that you could protect the code with a conditional statement.
' where {$_ -NE ""} | Get-Mailbox $_ 'or for those of you who like to use short alias in your code
' ? {$_ -NE ""} | Get-Mailbox $_ '
No comments:
Post a Comment