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

More Related Content

PowerShell 8tips

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

Editor's Notes

  1. MGB 2003 © 2003 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.