This document contains a slide deck presentation about eight tips and tricks for using PowerShell. The presentation covers remote control using WinRM and PSRemoting, parameter binding, splatting, tracing commands, suppressing errors, making reusable tools, comment-based help, and creating GUI apps. The presentation encourages attendees to download the slides and scripts from the Concentrated Technology website.
1 of 15
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
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