introduction to powershell at chicago code camp 2014

47
2014 Introduction to PowerShell MICHAEL BLUMENTHAL PSC GROUP, LLC

Upload: michael-blumenthal

Post on 24-May-2015

195 views

Category:

Technology


1 download

DESCRIPTION

An introduction to Microsoft's PowerShell scripting language.

TRANSCRIPT

Page 1: Introduction to PowerShell at Chicago Code Camp 2014

2014

Introduction to PowerShell MICHAEL BLUMENTHALPSC GROUP, LLC

Page 2: Introduction to PowerShell at Chicago Code Camp 2014

2014

Who is Michael Blumenthal?

• Sr. Solution Architect at PSC Group• CSPUG Co-Leader• INETA Champ 2010-2013• 19 years in IT Consulting• 7 years working with PowerShell• Twitter: @MichaelBL

Page 3: Introduction to PowerShell at Chicago Code Camp 2014

2014

Why PowerShell?

Page 4: Introduction to PowerShell at Chicago Code Camp 2014

2014

PowerShell puts the SharePoint Engine at your fingertips!

• It’s Easy to Get Started!1• Learn the PowerShell Syntax2• Real World Examples3• Best Practices4• More Resources5

Page 5: Introduction to PowerShell at Chicago Code Camp 2014

2014

Chapter 1

IT’S EASY TO GET STARTED!

Page 6: Introduction to PowerShell at Chicago Code Camp 2014

2014

Getting Started with PowerShell

200320082008 R220127, 8, 8.1

Page 7: Introduction to PowerShell at Chicago Code Camp 2014

2014

Windows Feature

Page 8: Introduction to PowerShell at Chicago Code Camp 2014

2014

V2

Page 9: Introduction to PowerShell at Chicago Code Camp 2014

2014

PowerShell V3&4 ISE

Page 10: Introduction to PowerShell at Chicago Code Camp 2014

2014

Chapter 2

LEARN THE POWERSHELL SYNTAX!

Page 11: Introduction to PowerShell at Chicago Code Camp 2014

2014

Learn to use PowerShell with SharePoint!

Symbols & Keywords

Using the SharePoint API

Creating and Running Scripts

Page 12: Introduction to PowerShell at Chicago Code Camp 2014

2014

Symbols, Keywords, and Syntax! Oh My!

• Variables1• Commands2• Piping3• Comparisons4• Flow Control5• Filtering6

Page 13: Introduction to PowerShell at Chicago Code Camp 2014

2014

Punctuation Pronunciation

$ _ : %# [ ] ( ) ;| . + ={ } < > ! /, \“ -

Page 14: Introduction to PowerShell at Chicago Code Camp 2014

2014

Pop Quiz…

1$#|

Page 15: Introduction to PowerShell at Chicago Code Camp 2014

2014

Variables

• Case Insensitive, Dynamic typing

$foo

$true, $false, $profile, $null

$foo = “Hello, World”

1

Page 16: Introduction to PowerShell at Chicago Code Camp 2014

2014

Page 17: Introduction to PowerShell at Chicago Code Camp 2014

2014

Commands are called cmdlets.

Verb-Noun

Built-in, Extensible

Get-Help & Help

Get-Member

2

Page 18: Introduction to PowerShell at Chicago Code Camp 2014

2014

Help!

Page 19: Introduction to PowerShell at Chicago Code Camp 2014

2014

The Power of Piping!

Output Of Command 1

Input of Command 2|

3

Page 20: Introduction to PowerShell at Chicago Code Camp 2014

2014

Example

Page 21: Introduction to PowerShell at Chicago Code Camp 2014

2014

Dial zero for an…4

Operator

-eq -le-ne -like-gt -notlike-ge -match-lt -notmatch

Page 22: Introduction to PowerShell at Chicago Code Camp 2014

2014

Example

Page 23: Introduction to PowerShell at Chicago Code Camp 2014

2014

Taking Control of the Flow

• For (Init;Test;Repeat) {Commands}• for($i=1; $i -le 10; $i++) {Write-Host $i}For• Foreach (Item in Collection) {Commands}• Foreach ($web in $site.AllWebs) {$web.Title}ForEach• If (Test) {Commands} • if ($web.Title –ne “”) {Write-Host $web.Title}

If• While (Condition){Commands}• while($val -ne 3){$val++; Write-Host $val}While

5

Page 24: Introduction to PowerShell at Chicago Code Camp 2014

2014

Example

Page 25: Introduction to PowerShell at Chicago Code Camp 2014

2014

Where-Object

•Where {<Test>}Syntax

• V1&2:Dir | Where {$_.Name –like

“B*”}• V3:Dir | where Name –like B*

Example

6

Page 26: Introduction to PowerShell at Chicago Code Camp 2014

2014

Executing Scripts

.\filename.ps1

Set-ExecutionPolicy Unrestricted

Page 27: Introduction to PowerShell at Chicago Code Camp 2014

2014

Chapter 3

REAL WORLD EXAMPLES

Page 28: Introduction to PowerShell at Chicago Code Camp 2014

2014

Real World Examples

•Flash cards•Dell Service Tag•Audio Alerts•MP3 Metadata Management•Managing Microsoft Server Products•File Conversion & Text Manipulation

Page 29: Introduction to PowerShell at Chicago Code Camp 2014

2014

Flash Cards

Page 30: Introduction to PowerShell at Chicago Code Camp 2014

2014

Get-DellServiceTag

• Get-WmiObject win32_SystemEnclosure | select serialnumber

Page 31: Introduction to PowerShell at Chicago Code Camp 2014

2014

Audio Alerts

• Stick this at the end of your long running script:

$Voice = new-object -com SAPI.SpVoice $Voice.Speak(“Deployment is done!")

Page 32: Introduction to PowerShell at Chicago Code Camp 2014

2014

File and Text Wrangling

• Word• AutoDOCX

• RegEx• PSObjTXT

• Export-• CSVCSV

Page 33: Introduction to PowerShell at Chicago Code Camp 2014

2014

Chapter 4

BEST PRACTICES

Page 34: Introduction to PowerShell at Chicago Code Camp 2014

2014

Comment your functions

• <#• .SYNOPSIS –a brief explanation of what the script or function does.• .DESCRIPTION – a more detailed explanation of what the script or

function does.• .PARAMETER name – an explanation of a specific parameter. Replace name with the parameter name. You can have one of these sections for each parameter the script or function uses.

• .EXAMPLE – an example of how to use the script or function. You can have multiple .EXAMPLE sections if you want to provide more than one example.

• .NOTES – any miscellaneous notes on using the script or function.• .LINK – a cross-reference to another help topic; you can have more

than one of these. If you include a URL beginning with http:// or https://, the shell will open that URL when the Help command’s –online parameter is used.

• #>

Page 35: Introduction to PowerShell at Chicago Code Camp 2014

2014

Find custom commands this way

Refresh the command list

Actions you can take once you fill in parameters

Page 36: Introduction to PowerShell at Chicago Code Camp 2014

2014

Self Announcing Functions

Page 37: Introduction to PowerShell at Chicago Code Camp 2014

2014

Source Code Control

Page 38: Introduction to PowerShell at Chicago Code Camp 2014

2014

More Good Ideas

• Always read scripts before running them• Make yours safe when others don’t• Check for valid parameter values • get-help about_Functions_Advanced_Parameters

• Do error handling • get-help about_Try_Catch_Finally• get-help about_CommonParameters • -ErrorAction and -ErrorVariable

Page 39: Introduction to PowerShell at Chicago Code Camp 2014

2014

Chapter 5

MORE RESOURCES

Page 41: Introduction to PowerShell at Chicago Code Camp 2014

2014

Page 42: Introduction to PowerShell at Chicago Code Camp 2014

2014

Page 43: Introduction to PowerShell at Chicago Code Camp 2014

2014

Page 45: Introduction to PowerShell at Chicago Code Camp 2014

2014

Script something today!

It’s Easy to Get Started!

Learn & Use the PowerShell Syntax

More Resources

In Review…

Page 46: Introduction to PowerShell at Chicago Code Camp 2014

2014

Questions

• Michael BlumenthalSharePoint ArchitectPSC Group, LLC

[email protected]• psclistens.com• www.cspug.org• Twitter: @MichaelBL• LinkedIn• Microsoft Midwest Customers

Yammer Network

Thank you for your time today.

Page 47: Introduction to PowerShell at Chicago Code Camp 2014

2014

In Memory of Dave Bost6/30/1970-4/7/2014

Developer Evangelist at Microsoft for 8 yearsSeptember 2005 – October 2013He was a great supporter of the Chicago technical community.

Please donate to his family’s chosen cause, Pitt Hopkins Research Foundationhttp://pitthopkins.org/donate/

More about Dave Bost’s passing: Obit: http://bit.ly/1msYRLRDavid Giard’s blog: http://bit.ly/1fhIvR2