Introduction to PowerShell Michael Blumenthal, MVP
Who is Michael Blumenthal? Tenant Admin at a leading broadcast media company Office 365 MVP Dev/ITPro Mix In IT Consulting since 1995 PowerShelling since 2007 Contact Michael@Blumenthalit.com Twitter: @MichaelBL LinkedIn: http://bit.ly/MBB-LI https://michaelblumenthal.me TechCommunity.Microsoft.com
What is PowerShell?
Why is PowerShell AWESOME? Write-Host “SO easy to use!” No Compiling! Servers, Desktop OS’s, any .NET API, web services, you name it! No Compliling!
PowerShell puts .NET at your fingertips! It’s Easy to Get Started! 1 Learn the PowerShell Syntax 2 Working with O365 3 Best Practices 4 Who would like another tool in their toolbox? How about the swiss army knife of tools? It will make you a more productive developer!
It’s Easy to get Started! Chapter 1 It’s Easy to get Started!
Getting Started with PowerShell 2003 2008,R2 2012, R2 2016 7, 8, 8.1, 10 Who here already has installed and PowerShell? Who’s using Server 2003? 2008? Win 2008 – It’s an OS Feature – “servermanagercmd –install PowerShell”
The Command Line Window
Windows Feature Win 8-10 Win 8.x
The Integrated Script Editor V2
PowerShell V3-5 ISE
Intellisense! Intellisense!
Learn the PowerShell Syntax! Chapter 2 Learn the PowerShell Syntax!
Symbols, Keywords, and Syntax! Oh My! Variables 1 Commands 2 Piping 3 Comparisons 4 Flow Control 5 Filtering 6
Reading Symbols in Code (tal Guidance Moe, Larry, and } The universe started with the Big ! !Important A # of Bacon and # Browns
1 $something Case Insensitive, Dynamic typing Variables Case Insensitive, Dynamic typing $something $true, $false, $null, $profile $myMessage= “Hello, World” begin with a $ Name that Language… Similar to @variables in SQL, apparently also Ruby, PHP. Arrays start at 0
Remove $opposite; add write-host; get-item
Commands are called cmdlets. 2 Commands are called cmdlets. Verb-Noun Built-in, Extensible Get-Help Get-Member Get-Command Mention Aliases
Help!
Discoverability Get-Member examples
Get-Command Find Cmdlets Get details of cmdlets and applications
Aliases Alias cmdlet Dir Get-ChildItem Sort Sort-object Select Foreach, also % Get-ChildItem Sort-object Select-object Foreach-object
3 The Power of Piping! Output Of Command 1 Input of Command 2 |
Example Can you imagine trying to do this if this was just text?
Dial zero for an… -eq -le -ne -like -gt -notlike -ge -match -lt 4 Dial zero for an… Operator -eq -le -ne -like -gt -notlike -ge -match -lt -notmatch
Example
Taking Control of the Flow 5 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 Get-Help about_flow_control Mention that collections start at 0
Example
Where {<Test>} Where-Object Syntax Example 6 V1&2: Dir | Where {$_.Name –like “B*”} V3+: Dir | where Name –like B* Example
Set-ExecutionPolicy Unrestricted Executing Scripts .\filename.ps1 Set-ExecutionPolicy Unrestricted
Chapter 3 Connecting To O365
Get the Cmdlets! AAD EXO PNP SK4B SPO Verb-MSOL* Remote Session Verb-PNP* PNP new-CSOnlineSession; Remote SK4B Verb-SPO* SPO
Demo
Chapter 4 Best Practices
Use Functions Function global:Do-Something(){ } Function global:Do-Something($someParameter){ Function global:Do-Something{ param ([type]$someParameter=$(“Default Expression”)) Single purpose vs multitool
Follow the Naming Convention! Verb-Noun 98 Verbs
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. #>
Search for Commands Refresh the command list Actions you can take
Self Announcing Functions …
Source Code Control
Always read scripts before running them More Good Ideas Always read scripts before running them Be a tool maker
Make yours safe when others don’t More Good Ideas Always read scripts before running them. Make yours safe when others don’t
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
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
Resources http://bit.ly/poshres
Script something today! It’s Easy to Get Started! PowerShell Syntax Office 365 You owe it to yourself to try out PowerShell. It will make you a better developer!