PowerShell for Windows Admins

May 6 2013   7:53AM GMT

Scripting games-major dislike

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

One of the things we were asked to blog about as Scripting Games judges was things we liked and disliked. This code is a major dislike

Get-ChildItem $sourceDirectory | ? {$_.PsISContainer } |
% { $subDirectory = $_ ; Get-ChildItem (“$sourceDirectory\$subDirectory”) -Include *.LOG -Recurse } |
? { $logFile = $_ ; $logFile.LastWriteTime -le $modifiedCutOffDate } |
% { $logFileAndSubDirDictionary.Add($logFile, $subDirectory) }

Two things really make this stand out as how not to do things:

Using % & ? as aliases in a script. They are tolerable (just) in an interactive command but have no place in a script. Tab completion is so easy. Use the proper command.
Putting multiple commands on a line separated by ; It makes the code hard to read and awkward to work out whats going on. It also makes script testing, debug and maintenance much more difficult
Avoid these two things in your scripts

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: