PowerShell 8tips

Preview:

DESCRIPTION

 

Citation preview

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

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 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

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

2A: Parameter Binding ByValue2A: Parameter Binding ByValue

5 • Don Jones • ConcentratedTech.com

ServiceController

Stop-ServiceStop-

ServiceGet-ServiceGet-Service

-inputObject-Name

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

3: Splatting3: Splatting

Assemble parameters into a dictionary

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

Splat the dictionary

Ps @p

7 • Don Jones • ConcentratedTech.com

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

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

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

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

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

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

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

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

Recommended