Download - CNUG June 2016 introduction to PowerShell
© 2016 PSC Group, LLC
Introduction to PowerShell MICHAEL BLUMENTHALPSC 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!
© 2016 PSC Group, LLC
This is about you
• Anyone used PowerShell before?
• Who’s done scripting before? Windows? Unix?
• Developer or Administrator?
© 2016 PSC Group, LLC
What is PowerShell?
© 2016 PSC Group, LLC
Why use PowerShell?
DevOps
Machine config
Server & Service Management:Exchange, SQL, SharePoint, Azure, O365
Rapid Prototyping
© 2016 PSC Group, LLC
Why is PowerShell fun?
SCALABLE Command line control of
EVERYTHING!
Without Compiling!
© 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!
© 2016 PSC Group, LLC
Chapter 1
IT’S EASY TO GET STARTED!
© 2016 PSC Group, LLC
Getting Started with PowerShell
20032008,R22012, R220167, 8, 8.1, 10
© 2016 PSC Group, LLC
The Command Line Window
© 2016 PSC Group, LLC
Windows Feature
Win 8.x
Win 8-10
© 2016 PSC Group, LLC
The Integrated Script Editor
V2
© 2016 PSC Group, LLC
PowerShell V3-5 ISE
© 2016 PSC Group, LLC
Intellisense!
© 2016 PSC Group, LLC
Chapter 2
LEARN THE POWERSHELL SYNTAX!
© 2016 PSC Group, LLC
Symbols, Keywords, and Syntax! Oh My!
• Variables1• Commands2• Piping3• Comparisons4• Flow Control5• Filtering6
© 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
© 2016 PSC Group, LLC
Variables
• Case Insensitive, Dynamic typing
$something
$true, $false, $null, $profile
$myMessage= “Hello, World”
1
© 2016 PSC Group, LLC
Commands are called cmdlets.
Verb-Noun
Built-in, Extensible
Get-Help & Help
Get-Member
2
© 2016 PSC Group, LLC
Help!
Discoverability
© 2016 PSC Group, LLC
The Power of Piping!
Output Of Command
1
Input of Command
2|
3
© 2016 PSC Group, LLC
Example
© 2016 PSC Group, LLC
Pipe Demo!
© 2016 PSC Group, LLC
Dial zero for an…4
Operator
-eq -le-ne -like-gt -notlike-ge -match-lt -notmatch
Example
© 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
© 2016 PSC Group, LLC
Example
© 2016 PSC Group, LLC
Where-Object
•Where {<Test>}Syntax
• V1&2:Dir | Where {$_.Name –like
“B*”}• V3+:Dir | where Name –like B*
Example
6
© 2016 PSC Group, LLC
Executing Scripts
.\filename.ps1
Set-ExecutionPolicy Unrestricted
© 2016 PSC Group, LLC
Chapter 3
REAL WORLD EXAMPLES
© 2016 PSC Group, LLC
Real World Examples
•Calling REST APIs•Dell Service Tag•Audio Alerts•File Conversion & Text Manipulation•Managing Servers at Scale
© 2016 PSC Group, LLC
Calling REST APIs
© 2016 PSC Group, LLC
Get-DellServiceTag
• Get-WmiObject win32_SystemEnclosure | select serialnumber
© 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!")
© 2016 PSC Group, LLC
File and Text Wrangling
• Word• AutoDOCX
• RegEx• PSObjTXT
• Export-• CSVCSV
© 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
© 2016 PSC Group, LLC
Chapter 4
BEST PRACTICES
© 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.
• #>
© 2016 PSC Group, LLC
Find custom commands this way
Refresh the command list
Actions you can take once you fill in parameters
© 2016 PSC Group, LLC
Self Announcing Functions
…
© 2016 PSC Group, LLC
Source Code Control
© 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
© 2016 PSC Group, LLC
Chapter 5
MORE RESOURCES
© 2016 PSC Group, LLC
© 2016 PSC Group, LLC
© 2016 PSC Group, LLC
© 2016 PSC Group, LLC
Script something today!
It’s Easy to Get Started!
PowerShell Syntax
More Resources
In Review…
© 2016 PSC Group, LLC
Survey and Raffle
• http://bit.ly/CNUGPOSH