Intergen's Solution Architect and Microsoft MVP Gavin Barron presented "PowerShell: Automation for everyone" during the MVP CompCamp 2014, a worldwide event ran during the weekend of 22-23 March 2014.
Gavin's blog: http://gavinb.net/
5. PowerShell: Automation for Everyone | 5
Why PowerShell?
Product specific cmdlets
Able to run .NET code
Hooks into WMI/COM
Object pipeline
Readable scripts
15. PowerShell: Automation for Everyone | 15
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"
17. 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{}
18. PowerShell: Automation for Everyone | 18
Consider your outputs
Write-Host
Write-Output
Write-Progress
Add-Content
Out-File
19. PowerShell: Automation for Everyone | 19
Have style!
Use a coding convention
Naming
Casing
Bracing
Avoid aliases
Be Consistent!
20. 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
21. PowerShell: Automation for Everyone | 21
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()