introduction to powershell (sharepoint fest chicago 2016 workshop)

86
© 2016 PSC Group, LLC © 2016 PSC Group, LLC Introduction to PowerShell MICHAEL BLUMENTHAL AND JACK FRUH

Upload: michael-blumenthal

Post on 13-Jan-2017

169 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Introduction to PowerShell

MICHAEL BLUMENTHAL AND JACK FRUH

Page 2: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Who is Michael Blumenthal?

• Technical Solution Evangelist at PSC Group

• Office 365 MVP

• Dev/ITPro Mix

• In IT Consulting since 1995

• PowerShelling since 2007

We’re

Hiring!

Page 3: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Who is Jack Fruh?

• Cloud Architect• Fortune 500 Company

• Big on community• SPS Chicago Suburbs

Co-Leader• SharePointJack.com• SharePoint-

Community.org

Page 4: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

This is about you

• Everyone have a laptop with you?

• Do you have PowerShell installed?

• Anyone used PowerShell before?

• Who’s done scripting before?

• Admin or Developer?

Page 5: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Introduce Yourself

• Your Name• Company Size• Your Role• O365 or SharePoint On Prem Version?• Where you are in your PowerShell journey• One thing you want PowerShell to solve?

Page 6: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

What is PowerShell?

Page 7: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Why is PowerShell AWESOME?

Write-Host “SO easy to use!”

No Compiling!

Page 8: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

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

PowerShell puts .NET at your fingertips!

Page 9: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Chapter 1

IT’S EASY TO GET STARTED!

Page 10: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Getting Started with PowerShell

20032008,R22012, R220167, 8, 8.1, 10

Page 11: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

The Command Line Window

Page 12: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Windows Feature

Win 8.x

Win 8-10

Page 13: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

The Integrated Script Editor

V2

Page 14: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

PowerShell V3-5 ISE

Page 15: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Intellisense!

Page 16: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Now Run PowerShell

• $PSVerTable

Page 17: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Chapter 2

LEARN THE POWERSHELL SYNTAX!

Page 18: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Symbols, Keywords, and Syntax! Oh My!

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

Page 19: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Reading Symbols in Code

• (tal Guidance• Moe, Larry, and }• The universe started with the Big !• !Important• A # of Bacon and # Browns

Page 20: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Variables

• Case Insensitive, Dynamic typing

$something

$true, $false, $null, $profile

$myMessage= “Hello, World”

1

Page 21: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)
Page 22: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Your Turn

• Create a Variable• Assign something to it• Get its value

Page 23: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Commands are called cmdlets.

Verb-Noun

Built-in, Extensible

Get-Help

Get-Member

Get-Command

2

Page 24: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Help!

Page 25: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Your Turn

• Get-Help About_<CTRL+Space>

Page 26: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

Discoverability

Page 27: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Get-Command

• Find Cmdlets• Get details of cmdlets and applications

Page 28: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Your Turn

• Get-command get-date• Get-date | Get-member• Name a method!

• Get-command *SPO*

Page 29: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Our Turn

• Get-help• Get-command –noun• Get-member

Page 30: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Aliases

Alias• Dir• Sort• Select• Foreach, also %

cmdlet• Get-ChildItem• Sort-object• Select-object• Foreach-object

Page 31: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Your Turn

• Get-Alias

Page 32: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

The Power of Piping!

Output Of Command

1

Input of Command

2|

3

Page 33: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Example

Page 34: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Pipe Demo!

Page 35: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Your Turn

• Get-ChildItem | select –first 10

• Get-ChildItem| select -first 10 | Sort-Object -Property LastAccessTime | Select-Object -Property LastAccessTime, Name

• •

Page 36: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Dial zero for an…4

Operator

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

Page 37: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

Example

Page 38: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Taking Control of the Flow

• If (Test) {Commands} else {Commands}• if ($web.Title –ne “”) {Write-Host $web.Title}

If

• For (Init;Test;Repeat) {Commands}• for($i=1; $i -le 10; $i++) {Write-Host $i}For

• Foreach (Item in Collection) {Commands}• Foreach ($gumball in $CandyBag) {$gumball.color}

• Collection | Foreach {Commands}ForEach

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

5

Page 39: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Example

Page 40: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Your Turn

• Comparisons• Arrays and Loops

Page 41: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Where-Object

•Where {<Test>}Syntax

• V1&2: Dir | Where {$_.Name –like “B*”}

• V3+: Dir | where Name –like B*

Example

6

Page 42: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Your Turn

• Where

Page 43: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Executing Scripts

.\filename.ps1

Set-ExecutionPolicy Unrestricted

Page 44: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Use Functions

Function global:Do-Something(){}

Function global:Do-Something($someParameter){}

Function global:Do-Something{param ([type]$someParameter=$(“Default Expression”))

}

Page 45: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Follow the Naming Convention!

Verb-Noun

• 98 Verbs

Page 46: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Modules

• .ps1m• Profile Path - $Profile• $env:PSModulePath

Page 47: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Chapter 3

CONNECTING TO SP AND O365

Page 48: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

SharePoint On Prem

• SharePoint On Prem• SharePoint Management Console• 15 Hive POSH Script

Page 49: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Office 365

• SharePoint Online Management Console• Office Dev PnP PowerShell• Connect-SPOService

Page 50: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

DEMO – SPO Sites

Page 51: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Parking Lot Question Check

Page 52: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Chapter 4

REAL WORLD EXAMPLES

Page 53: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Jack’s Scripts

• Examples

Page 54: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Real World Examples

• Dell Service Tag• Flashcards• Audio Alerts• File Conversion & Text

Manipulation• Managing Servers at Scale

Page 55: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Flash Cards

Page 56: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Get-DellServiceTag

• Get-WmiObject win32_SystemEnclosure | select serialnumber

Page 57: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Audio Alerts

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

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

Page 58: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

File and Text Wrangling

• Word• AutoDOCX

• RegEx• PSObjTXT

• Export-• CSVCSV

Page 59: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Server Management

Server/Service Examples of What you can script

Azure Virtual Machines

Exchange Mailboxes

Office 365 Sites

SharePoint Everything

SQL Queries

Windows Server File system

Page 60: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Chapter 5

BEST PRACTICES

Page 61: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

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 62: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Search for Commands

Refresh the command list

Actions you can take

Page 63: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Self Announcing Functions

Page 64: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Source Code Control

Page 65: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Always read scripts before running them

More Good Ideas

Page 66: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Always read scripts before running them.Make yours safe when others don’t

More Good Ideas

Page 67: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Always read scripts before running themMake yours safe when others don’tCheck for valid parameter values get-help about_Functions_Advanced_Parameters

More Good Ideas

Page 68: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Always read scripts before running themMake yours safe when others don’tCheck 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

More Good Ideas

Page 69: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Chapter 6

RESOURCES

Page 71: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Page 72: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Page 73: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Page 74: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

JEFF HICKS

Page 75: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Page 76: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Another Book:

Page 77: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

TechCommunity.microsoft.com

Page 78: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

PowerShell.org

Page 79: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

SharePoint Jack

Page 80: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Get Jack’s Cheat Sheet

http://bit.ly/poshref

Page 81: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Blumenthal’s Blog: MichaelBlumenthal.me

Page 82: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Interview with Don Jones

• http://www.runasradio.com/Shows/Show/505

Page 83: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Contact Us

[email protected]

• 847-275-7247• @MichaelBL• http://bit.ly/MBB-LI• https://Michaelblumenthal.

me

• http://bit.ly/JoinChiITEvents

[email protected]• @sharepointjack

Page 84: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Script something today!

It’s Easy to Get Started!

PowerShell Syntax

More Resources

In Review…

Page 85: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

© 2016 PSC Group, LLC© 2016 PSC Group, LLC

Raffle

Page 86: Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)

Surveys

86

Feedback Please!Session Surveys via Event AppSelect “Schedule” -> Select Session -> Scroll to “Session Survey”

Download the App:Event URL https://crowd.cc/chi2016 Your App URL https://crowd.cc/s/mGoA Or search for “SharePoint Fest” in App Store

Learn from the Top SharePoint Experts

SharePoint FestChicago 2016

WWW.SHAREPOINTFEST.COM