10 PowerShell Mistakes, Trips and Traps

16
10 POWERSHELL MISTAKES, TRIP-UPS AND TRAPS JEFFERY HICKS PROF. POWERSHELL

description

Common mistakes and traps that catch many PowerShell beginners.

Transcript of 10 PowerShell Mistakes, Trips and Traps

Page 1: 10 PowerShell Mistakes, Trips and Traps

10 POWERSHELL MISTAKES, TRIP-UPS

AND TRAPSJEFFERY HICKS

PROF. POWERSHELL

Page 2: 10 PowerShell Mistakes, Trips and Traps

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

Page 3: 10 PowerShell Mistakes, Trips and Traps

#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

Page 4: 10 PowerShell Mistakes, Trips and Traps

#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

Page 5: 10 PowerShell Mistakes, Trips and Traps

#3 BOOLEANS ARE OBJECTS, TOO

• $True and $False are not strings• The cool PowerShell kids don’t compare

Page 6: 10 PowerShell Mistakes, Trips and Traps

#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

Page 7: 10 PowerShell Mistakes, Trips and Traps

#5 VARIABLE VULNERABILITY

• This isn’t VBScript• Variable names should be meaningful• Avoid re-using variables for different object types

Page 8: 10 PowerShell Mistakes, Trips and Traps

#6 POWERSHELL IS NOT .NET PROGRAMMING

• Use cmdlets wherever possible• .NET classes and methods are for exceptions• …or large scale performance benefits

Page 9: 10 PowerShell Mistakes, Trips and Traps

#7 POWERSHELL IS NOT VBSCRIPT

• PowerShell requires a new paradigm• Don’t turn VBScript into PowerShell• Objects in the pipeline

Page 10: 10 PowerShell Mistakes, Trips and Traps

#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

Page 11: 10 PowerShell Mistakes, Trips and Traps

#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

Page 12: 10 PowerShell Mistakes, Trips and Traps

#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

Page 13: 10 PowerShell Mistakes, Trips and Traps

QUESTIONS AND ANSWERS

Page 14: 10 PowerShell Mistakes, Trips and Traps

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

Page 15: 10 PowerShell Mistakes, Trips and Traps

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

Page 16: 10 PowerShell Mistakes, Trips and Traps

THANK YOU

http://jdhitsolutions.com/blog

[email protected]

@JeffHicks

http://gplus.to/jeffhicks