290 likes | 412 Views
PowerShell: Automation for Everyone. ComCamp Auckland. Gavin Barron, Solution Architect March 22, 2014. Our agenda. Intro. The only five commands you need. Demos. Rules for writing scripts. Wrap up. Your presenter. Gavin Barron Solution Architect. SharePoint Server MVP Developer
E N D
PowerShell: Automation for Everyone • ComCampAuckland • Gavin Barron, Solution ArchitectMarch 22, 2014
Our agenda Intro The only five commands you need Demos Rules for writing scripts Wrap up
Your presenter Gavin Barron Solution Architect • SharePoint Server MVP • Developer • Skier • http://gavinb.net • @gavinbarron • gavin.barron@intergen.co.nz
Why Automate? Repeatability Reliability Save time? Eliminate boring manual tasks
Why PowerShell? Product specific cmdlets Able to run .NET code Hooks into WMI/COM Object pipeline Readable scripts
File renaming in .bat @echo off for /f "delims=" %%a in ('dir /b *.txt') do call :dot "%%a" pause goto :EOF :dot set "var=%~n1" set "var=%var:.=_%" echo ren %1 "%var%%~x1"
Consider error cases What should you do when it goes pear shaped? Default behaviour is Ignore and Continue $ErrorActionPreference -ErrorAction try{} catch{}
Consider your outputs Write-Host Write-Output Write-Progress Add-Content Out-File
Have style! • Use a coding convention • Naming • Casing • Bracing • Avoid aliases • Be Consistent!
Leverage functions for re-use Small tasks Compose a script from functions Functions are highly reusable CmdletBinding Attribute Parameters Attribute Default Values
Use configuration files $file =[xml] (Get-ChildItem $constantsFilePath) $root = "setup-config" $ConstantsNodeList = $file.$root.Constants foreach($property in $ConstantsNodeList.Property) { $key= $property.Key $value=$property.Value $webApp.Properties.Add($key,$value) } $webApp.Update()
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
Thank you • Email: gavin.barron@intergen.co.nz • Blog: http://gavinb.net • Twitter: @gavinbarron