PowerShell 8tips

917 views

Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

PowerShell 8tips

  1. 1. Windows PowerShell: Eight Tips and Tricks to Make You a Guru Don Jones Senior Partner & Principal Technologist Concentrated Technology, LLC
  2. 2. This slide deck was used in one of our many conference presentations. We hope you enjoy it, and invite you to use it within your own organization however you like. For more information on our company, including information on private classes and upcoming conference appearances, please visit our Web site, www.ConcentratedTech.com . For links to newly-posted decks, follow us on Twitter: @concentrateddon or @concentratdgreg This work is copyright ©Concentrated Technology, LLC
  3. 3. CAUTION: DEMOS AHEAD <ul><li>This is a demonstration-intensive session (very few slides) </li></ul><ul><li>I will capture a shell transcript and save all of my scripts </li></ul><ul><li>You can download these (in a week or so) from ConcentratedTech.com (there ’s a “Conference Materials” link in the menu) </li></ul> • Don Jones • ConcentratedTech.com
  4. 4. 1: Remote Control <ul><li>Uses WinRM / WS-MAN </li></ul><ul><li>Enable-PSRemoting (or GPO) </li></ul><ul><li>1:1 – Enter-PSSession </li></ul><ul><li>1:n – Invoke-Command </li></ul><ul><li>Persistent Connection – New-PSSession </li></ul><ul><li>Implicit – Import-PSSession </li></ul> • Don Jones • ConcentratedTech.com
  5. 5. 2A: Parameter Binding ByValue • Don Jones • ConcentratedTech.com Stop-Service Get-Service -inputObject -Name ServiceController
  6. 6. 2B: Parameter Binding ByPropertyName • Don Jones • ConcentratedTech.com Get-Process Get-ADComputer -computerName -id -name Computer Object Name cn osversion
  7. 7. 3: Splatting <ul><li>Assemble parameters into a dictionary </li></ul><ul><li>$p = @{ 'name'='svchost'; 'computername'='server-r2 ’ } </li></ul><ul><li>Splat the dictionary </li></ul><ul><li>Ps @p </li></ul> • Don Jones • ConcentratedTech.com
  8. 8. 4: Tracing Commands <ul><li>See exactly how parameter binding is working… </li></ul><ul><li>trace-command -name parameterbinding -pshost -exp { ps @p } </li></ul> • Don Jones • ConcentratedTech.com
  9. 9. 5: Suppressing Errors <ul><li>Use –EA or –ErrorAction </li></ul><ul><li>Do not set $ErrorActionPreference globally or in a script </li></ul><ul><li>-EA SilentlyContinue – suppress </li></ul><ul><li>-EA Stop – catch using a Try…Catch block </li></ul><ul><li>Use –EV to capture the error into a variable for logging or analysis </li></ul> • Don Jones • ConcentratedTech.com
  10. 10. 6: Making a Tool <ul><li>Move command into a function </li></ul><ul><li>Parameterize hardcoded stuff that might change each time </li></ul><ul><li>Save the file in a .psm1 file in… …/[My ]Documents/WindowsPowerShell/Modules/ module-name / module-name. psm1 </li></ul><ul><li>Load using Import-Module modulename </li></ul> • Don Jones • ConcentratedTech.com
  11. 11. 7: Puncutation Puzzle <ul><li>$_ = placeholder; only in designated areas </li></ul><ul><li>[int] = object type </li></ul><ul><li>$a[0] = array index </li></ul><ul><li>{get-process} = script block </li></ul><ul><li>“ string” and ‘string’ </li></ul><ul><li>(order (of) execution) </li></ul><ul><li>@(array,of,items) </li></ul><ul><li>@{key=value;key2=value2} hashtables </li></ul><ul><li>$variable </li></ul><ul><li>% alias to “ForEach-Object” </li></ul> • Don Jones • ConcentratedTech.com
  12. 12. 8: Comment-Based Help <ul><li>Create standard-formatted help… without doing any formatting </li></ul><ul><li>Help about_comment* </li></ul><ul><li>Add to the top of scripts to document the script; to a function to doc the function </li></ul> • Don Jones • ConcentratedTech.com
  13. 13. BONUS: GUI-Based Apps <ul><li>PrimalForms / Visual PowerShell </li></ul><ul><li>www.primaltools.com </li></ul><ul><li>Create GUI, add code to events </li></ul><ul><li>Let ’s see! </li></ul> • Don Jones • ConcentratedTech.com
  14. 14. Final Notes… <ul><li>Please be sure to submit a session evaluation form! </li></ul><ul><li>Download slides & materials from www.ConcentratedTech.com within one week! </li></ul><ul><li>Blog, URLs, and other information is also available at www.ConcentratedTech.com for your reference </li></ul><ul><li>Thank you very much! </li></ul> • Don Jones • ConcentratedTech.com
  15. 15. This slide deck was used in one of our many conference presentations. We hope you enjoy it, and invite you to use it within your own organization however you like. For more information on our company, including information on private classes and upcoming conference appearances, please visit our Web site, www.ConcentratedTech.com . For links to newly-posted decks, follow us on Twitter: @concentrateddon or @concentratdgreg This work is copyright ©Concentrated Technology, LLC

×