cnug june 2016 introduction to powershell

50
© 2016 PSC Group, LLC Introduction to PowerShell MICHAEL BLUMENTHAL PSC GROUP, LLC

Upload: michael-blumenthal

Post on 13-Jan-2017

140 views

Category:

Technology


4 download

TRANSCRIPT

Page 1: CNUG June 2016   introduction to PowerShell

© 2016 PSC Group, LLC

Introduction to PowerShell MICHAEL BLUMENTHALPSC GROUP, LLC

Page 2: CNUG June 2016   introduction to PowerShell

© 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: CNUG June 2016   introduction to PowerShell

© 2016 PSC Group, LLC

This is about you

• Anyone used PowerShell before?

• Who’s done scripting before? Windows? Unix?

• Developer or Administrator?

Page 4: CNUG June 2016   introduction to PowerShell

© 2016 PSC Group, LLC

What is PowerShell?

Page 5: CNUG June 2016   introduction to PowerShell

© 2016 PSC Group, LLC

Why use PowerShell?

DevOps

Machine config

Server & Service Management:Exchange, SQL, SharePoint, Azure, O365

Rapid Prototyping

Page 6: CNUG June 2016   introduction to PowerShell

© 2016 PSC Group, LLC

Why is PowerShell fun?

SCALABLE Command line control of

EVERYTHING!

Without Compiling!

Page 7: CNUG June 2016   introduction to PowerShell

© 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 8: CNUG June 2016   introduction to PowerShell

© 2016 PSC Group, LLC

Chapter 1

IT’S EASY TO GET STARTED!

Page 9: CNUG June 2016   introduction to PowerShell

© 2016 PSC Group, LLC

Getting Started with PowerShell

20032008,R22012, R220167, 8, 8.1, 10

Page 10: CNUG June 2016   introduction to PowerShell

© 2016 PSC Group, LLC

The Command Line Window

Page 11: CNUG June 2016   introduction to PowerShell

© 2016 PSC Group, LLC

Windows Feature

Win 8.x

Win 8-10

Page 12: CNUG June 2016   introduction to PowerShell

© 2016 PSC Group, LLC

The Integrated Script Editor

V2

Page 13: CNUG June 2016   introduction to PowerShell

© 2016 PSC Group, LLC

PowerShell V3-5 ISE

Page 14: CNUG June 2016   introduction to PowerShell

© 2016 PSC Group, LLC

Intellisense!

Page 15: CNUG June 2016   introduction to PowerShell

© 2016 PSC Group, LLC

Chapter 2

LEARN THE POWERSHELL SYNTAX!

Page 16: CNUG June 2016   introduction to PowerShell

© 2016 PSC Group, LLC

Symbols, Keywords, and Syntax! Oh My!

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

Page 17: CNUG June 2016   introduction to PowerShell

© 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 18: CNUG June 2016   introduction to PowerShell

© 2016 PSC Group, LLC

Variables

• Case Insensitive, Dynamic typing

$something

$true, $false, $null, $profile

$myMessage= “Hello, World”

1

Page 19: CNUG June 2016   introduction to PowerShell
Page 20: CNUG June 2016   introduction to PowerShell

© 2016 PSC Group, LLC

Commands are called cmdlets.

Verb-Noun

Built-in, Extensible

Get-Help & Help

Get-Member

2

Page 21: CNUG June 2016   introduction to PowerShell

© 2016 PSC Group, LLC

Help!

Page 22: CNUG June 2016   introduction to PowerShell

Discoverability

Page 23: CNUG June 2016   introduction to PowerShell

© 2016 PSC Group, LLC

The Power of Piping!

Output Of Command

1

Input of Command

2|

3

Page 24: CNUG June 2016   introduction to PowerShell

© 2016 PSC Group, LLC

Example

Page 25: CNUG June 2016   introduction to PowerShell

© 2016 PSC Group, LLC

Pipe Demo!

Page 26: CNUG June 2016   introduction to PowerShell

© 2016 PSC Group, LLC

Dial zero for an…4

Operator

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

Page 27: CNUG June 2016   introduction to PowerShell

Example

Page 28: CNUG June 2016   introduction to PowerShell

© 2016 PSC Group, LLC

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 ($gumball in $CandyBag) {$gumball.color}

• Collection | Foreach {Commands}ForEach

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

If

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

5

Page 29: CNUG June 2016   introduction to PowerShell

© 2016 PSC Group, LLC

Example

Page 30: CNUG June 2016   introduction to PowerShell

© 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 31: CNUG June 2016   introduction to PowerShell

© 2016 PSC Group, LLC

Executing Scripts

.\filename.ps1

Set-ExecutionPolicy Unrestricted

Page 32: CNUG June 2016   introduction to PowerShell

© 2016 PSC Group, LLC

Chapter 3

REAL WORLD EXAMPLES

Page 33: CNUG June 2016   introduction to PowerShell

© 2016 PSC Group, LLC

Real World Examples

•Calling REST APIs•Dell Service Tag•Audio Alerts•File Conversion & Text Manipulation•Managing Servers at Scale

Page 34: CNUG June 2016   introduction to PowerShell

© 2016 PSC Group, LLC

Calling REST APIs

Page 35: CNUG June 2016   introduction to PowerShell

© 2016 PSC Group, LLC

Get-DellServiceTag

• Get-WmiObject win32_SystemEnclosure | select serialnumber

Page 36: CNUG June 2016   introduction to PowerShell

© 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 37: CNUG June 2016   introduction to PowerShell

© 2016 PSC Group, LLC

File and Text Wrangling

• Word• AutoDOCX

• RegEx• PSObjTXT

• Export-• CSVCSV

Page 38: CNUG June 2016   introduction to PowerShell

© 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 39: CNUG June 2016   introduction to PowerShell

© 2016 PSC Group, LLC

Chapter 4

BEST PRACTICES

Page 40: CNUG June 2016   introduction to PowerShell

© 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 41: CNUG June 2016   introduction to PowerShell

© 2016 PSC Group, LLC

Find custom commands this way

Refresh the command list

Actions you can take once you fill in parameters

Page 42: CNUG June 2016   introduction to PowerShell

© 2016 PSC Group, LLC

Self Announcing Functions

Page 43: CNUG June 2016   introduction to PowerShell

© 2016 PSC Group, LLC

Source Code Control

Page 44: CNUG June 2016   introduction to PowerShell

© 2016 PSC Group, LLC

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 45: CNUG June 2016   introduction to PowerShell

© 2016 PSC Group, LLC

Chapter 5

MORE RESOURCES

Page 47: CNUG June 2016   introduction to PowerShell

© 2016 PSC Group, LLC

Page 48: CNUG June 2016   introduction to PowerShell

© 2016 PSC Group, LLC

Page 49: CNUG June 2016   introduction to PowerShell

© 2016 PSC Group, LLC

Script something today!

It’s Easy to Get Started!

PowerShell Syntax

More Resources

In Review…

Page 50: CNUG June 2016   introduction to PowerShell

© 2016 PSC Group, LLC

Survey and Raffle

• http://bit.ly/CNUGPOSH