10 PowerShell Mistakes, Trips and Traps
-
Upload
jeffery-hicks -
Category
Technology
-
view
1.308 -
download
1
description
Transcript of 10 PowerShell Mistakes, Trips and Traps
10 POWERSHELL MISTAKES, TRIP-UPS
AND TRAPSJEFFERY HICKS
PROF. POWERSHELL
WHO AM I?• Windows PowerShell MVP• PowerShell Author
• Learn PowerShell 3 in a Month of Lunches (with Don Jones)
• Learn PowerShell Toolmaking in a Month of Lunches (with Don Jones)
• PowerShell in Depth (with Don Jones & Richard Siddaway)
• Managing Active Directory with Windows PowerShell: TFM 2nd Ed.
• IT trainer and consultant• http://jdhitsolutions.com/blog• http://twitter.com/jeffhicks
#1 DON’T USE WRITE-HOST
• Write-Host writes to the console, not the pipeline• You can only work with objects written to the pipeline• Puppies are hurt when you use Write-Host• Unless you use as intended
#2 IT’S ALL ABOUT THE OBJECTS
• Don’t try to parse text• Don’t try to concatenate• Everything in PowerShell is an object• Learn how to use Get-Member
#3 BOOLEANS ARE OBJECTS, TOO
• $True and $False are not strings• The cool PowerShell kids don’t compare
#4 DON’T FALL INTO THE ALIAS TRAP
• Aliases are fine for interactive sessions• You know what you’re typing• Aliases disguise the real command
#5 VARIABLE VULNERABILITY
• This isn’t VBScript• Variable names should be meaningful• Avoid re-using variables for different object types
#6 POWERSHELL IS NOT .NET PROGRAMMING
• Use cmdlets wherever possible• .NET classes and methods are for exceptions• …or large scale performance benefits
#7 POWERSHELL IS NOT VBSCRIPT
• PowerShell requires a new paradigm• Don’t turn VBScript into PowerShell• Objects in the pipeline
#8 DON’T MAKE ERRORS GO AWAY
• Don’t turn off error pipeline• $ErrorActionPreference = "SilentlyContinue"
• Makes troubleshooting more difficult• Use Try/Catch and learn how to handle errors
#9 DON’T RE-INVENT THE WHEEL
• Don’t try to duplicate PowerShell functionality• No need to write your own help routine• Don’t try to re-invent cmdlets using .NET
#10 NOT EVERYTHING IS A ONE-LINER
• One-line commands can be efficient• Great for interactive console• Not necessarily a benefit in scripting• Don’t code yourself into a corner
QUESTIONS AND ANSWERS
RECOMMENDATIONS
• Write objects to the pipeline• Use cmdlets where possible• No aliases or shortcuts in scripts• Learn how to handle errors• Read the help and examples
RESOURCES• Learn PowerShell 3 in a Month of Lunches by Don Jones and Jeffery
Hicks • Learn PowerShell Toolmaking in a Month of Lunches by Don Jones
and Jeffery Hicks• The Lonely Administrator (http://jdhitsolutions.com/blog)• Prof. PowerShell (http://mcpmag.com/articles/list/prof-
powershell.aspx• PowerShell.org