powershell: automation for everyone

28
Gavin Barron, Solution Architect March 22, 2014 ComCamp Auckland PowerShell: Automation for Everyone

Upload: gavin-barron

Post on 25-Dec-2014

77 views

Category:

Technology


1 download

DESCRIPTION

PowerShell is a rich and powerful automation language. Due to its self describing nature it's also dead easy to learn.

TRANSCRIPT

Page 1: PowerShell: Automation for everyone

Gavin Barron, Solution ArchitectMarch 22, 2014

ComCamp Auckland

PowerShell: Automation for Everyone

Page 2: PowerShell: Automation for everyone

The only five commands you need

Rules for writing scripts

Demos

Intro

Wrap up

Our agenda

Page 3: PowerShell: Automation for everyone

PowerShell: Automation for Everyone | 3

SharePoint Server MVP

Developer

Skier

http://gavinb.net

@gavinbarron

[email protected]

Gavin BarronSolution Architect

Your presenter

Page 4: PowerShell: Automation for everyone

PowerShell: Automation for Everyone | 4

Why Automate?

Repeatability

Reliability

Save time?

Eliminate boring manual tasks

Page 5: PowerShell: Automation for everyone

PowerShell: Automation for Everyone | 5

Why PowerShell?

Product specific cmdlets

Able to run .NET code

Hooks into WMI/COM

Object pipeline

Readable scripts

Page 6: PowerShell: Automation for everyone

The only five commands you need

Page 7: PowerShell: Automation for everyone

Your Awesome Presentation Title {Segoe UI 12 pt.}

Get-Command

Page 8: PowerShell: Automation for everyone

Your Awesome Presentation Title {Segoe UI 12 pt.}

Get-Help

Page 9: PowerShell: Automation for everyone

Your Awesome Presentation Title {Segoe UI 12 pt.}

Get-Member

Page 10: PowerShell: Automation for everyone

Your Awesome Presentation Title {Segoe UI 12 pt.}

Where-Object

Page 11: PowerShell: Automation for everyone

Your Awesome Presentation Title {Segoe UI 12 pt.}

ForEach-Object

Page 12: PowerShell: Automation for everyone

Demos

Page 13: PowerShell: Automation for everyone

Your Awesome Presentation Title {Segoe UI 12 pt.}

Find the command you need

Page 14: PowerShell: Automation for everyone

Your Awesome Presentation Title {Segoe UI 12 pt.}

Simple Pipeline Use

Page 15: PowerShell: Automation for everyone

PowerShell: Automation for Everyone | 15

File renaming in .bat

@echo offfor /f "delims=" %%a in ('dir /b *.txt') do call :dot "%%a"pausegoto :EOF:dotset "var=%~n1"set "var=%var:.=_%"  echo ren %1 "%var%%~x1"

Page 16: PowerShell: Automation for everyone

Rules for writing scripts

Page 17: PowerShell: Automation for everyone

PowerShell: Automation for Everyone | 17

Consider error cases

What should you do when it goes pear shaped? Default behaviour is Ignore and Continue $ErrorActionPreference -ErrorAction try{} catch{}

Page 18: PowerShell: Automation for everyone

PowerShell: Automation for Everyone | 18

Consider your outputs

Write-Host Write-Output Write-Progress Add-Content Out-File

Page 19: PowerShell: Automation for everyone

PowerShell: Automation for Everyone | 19

Have style!

Use a coding convention Naming Casing Bracing

Avoid aliases Be Consistent!

Page 20: PowerShell: Automation for everyone

PowerShell: Automation for Everyone | 20

Leverage functions for re-use

Small tasks Compose a script from functions Functions are highly reusable Cmdlet Binding Attribute Parameters Attribute Default Values

Page 21: PowerShell: Automation for everyone

PowerShell: Automation for Everyone | 21

Use configuration files

$file =[xml] (Get-ChildItem $constantsFilePath)$root = "setup-config"$ConstantsNodeList = $file.$root.Constantsforeach($property in $ConstantsNodeList.Property) {

$key= $property.Key$value=$property.Value $webApp.Properties.Add($key,$value)

}$webApp.Update()

Page 22: PowerShell: Automation for everyone

Your Awesome Presentation Title {Segoe UI 12 pt.}

Script Examples

Page 23: PowerShell: Automation for everyone

Wrap up

Page 24: PowerShell: Automation for everyone

Your Awesome Presentation Title {Segoe UI 12 pt.}

Start scripting, it’s easy!

Page 25: PowerShell: Automation for everyone

PowerShell: Automation for Everyone | 25

Handy resources

http://blogs.technet.com/b/heyscriptingguy http://gallery.technet.microsoft.com/scriptcenter http://social.technet.microsoft.com/Forums/scriptcenter/

en-US/home?forum=ITCG http://ss64.com/ps/ http://ss64.com/ps/syntax.html

Page 26: PowerShell: Automation for everyone

Any questions?

Page 27: PowerShell: Automation for everyone

Email: [email protected]

Blog: http://gavinb.net

Twitter: @gavinbarron

Thank you

Page 28: PowerShell: Automation for everyone

THE END