15
Windows PowerShell: Eight Tips and Windows PowerShell: Eight Tips and Tricks to Make You a Guru Tricks to Make You a Guru Don Jones Don Jones Senior Partner & Principal Technologist Concentrated Technology, LLC

PowerShell 8tips

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: PowerShell 8tips

Windows PowerShell: Eight Tips and Windows PowerShell: Eight Tips and Tricks to Make You a GuruTricks to Make You a GuruDon JonesDon JonesSenior Partner & Principal TechnologistConcentrated Technology, LLC

Page 2: PowerShell 8tips

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

Page 3: PowerShell 8tips

CAUTION: DEMOS AHEADCAUTION: 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)

3 • Don Jones • ConcentratedTech.com

Page 4: PowerShell 8tips

1: Remote Control1: 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

4 • Don Jones • ConcentratedTech.com

Page 5: PowerShell 8tips

2A: Parameter Binding ByValue2A: Parameter Binding ByValue

5 • Don Jones • ConcentratedTech.com

ServiceController

Stop-ServiceStop-

ServiceGet-ServiceGet-Service

-inputObject-Name

Page 6: PowerShell 8tips

2B: Parameter Binding 2B: Parameter Binding ByPropertyNameByPropertyName

6 • Don Jones • ConcentratedTech.com

Computer Object

Get-ProcessGet-ProcessGet-

ADComputer

Get-ADCompute

r

-computerName-id-name

Name

cn

osversion

Page 7: PowerShell 8tips

3: Splatting3: Splatting

Assemble parameters into a dictionary

$p = @{'name'='svchost';'computername'='server-r2’}

Splat the dictionary

Ps @p

7 • Don Jones • ConcentratedTech.com

Page 8: PowerShell 8tips

4: Tracing Commands4: Tracing Commands

See exactly how parameter binding is working…

trace-command -name parameterbinding -pshost -exp { ps @p }

8 • Don Jones • ConcentratedTech.com

Page 9: PowerShell 8tips

5: Suppressing Errors5: 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

9 • Don Jones • ConcentratedTech.com

Page 10: PowerShell 8tips

6: Making a Tool6: 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

10 • Don Jones • ConcentratedTech.com

Page 11: PowerShell 8tips

7: Puncutation Puzzle7: 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”11 • Don Jones • ConcentratedTech.com

Page 12: PowerShell 8tips

8: Comment-Based Help8: 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

12 • Don Jones • ConcentratedTech.com

Page 13: PowerShell 8tips

BONUS: GUI-Based AppsBONUS: GUI-Based Apps

PrimalForms / Visual PowerShell www.primaltools.com Create GUI, add code to events Let’s see!

13 • Don Jones • ConcentratedTech.com

Page 14: PowerShell 8tips

Final Notes…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!

14 • Don Jones • ConcentratedTech.com

Page 15: PowerShell 8tips

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