boulos dib september 21, 2011files.meetup.com/1585210/introductiontopowershell.pdf · wes...

40
Boulos Dib September 21, 2011

Upload: others

Post on 28-Mar-2020

17 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Boulos Dib September 21, 2011files.meetup.com/1585210/IntroductionToPowerShell.pdf · Wes Montgomery, Larry Carlton and Ritchie Blackmore (Deep Purple, Rainbow)

Boulos Dib September 21, 2011

Page 2: Boulos Dib September 21, 2011files.meetup.com/1585210/IntroductionToPowerShell.pdf · Wes Montgomery, Larry Carlton and Ritchie Blackmore (Deep Purple, Rainbow)

Independent Consultant – Napeague Inc. Software Development since 1983 Few Facts (@boulosdib)

First Personal Computer 1980 – TRS-80 III First Z80 based product (EPROM based Protocol Adpator – 1984) First Commercial PC-DOS product (Telex on PCs – 1985) Started 16-bit Windows Development using Win 3.1 Developed on: 8080/Z80, 68xxx, PDP/RSX,VAX-VMS and x86/x64

(C/C++/C#) Worked with PowerShell since Monad (2006) Worked with SharePoint since STS (2003) More facts

▪ Favorite sport – Windsurfing ▪ Favorite hobby – Playing my sunburst Fender Stratocaster+ guitar. ▪ Favorite guitar players

▪ Wes Montgomery, Larry Carlton and Ritchie Blackmore (Deep Purple, Rainbow)

Page 3: Boulos Dib September 21, 2011files.meetup.com/1585210/IntroductionToPowerShell.pdf · Wes Montgomery, Larry Carlton and Ritchie Blackmore (Deep Purple, Rainbow)

Overview of PowerShell Introduction to PowerShell Scripting

Language Tools Script Authoring SharePoint Management Console SharePoint CmdLets

Page 4: Boulos Dib September 21, 2011files.meetup.com/1585210/IntroductionToPowerShell.pdf · Wes Montgomery, Larry Carlton and Ritchie Blackmore (Deep Purple, Rainbow)

NYC Code Camp

I will be presenting on LightSwitch and Silverlight at the NYC Code Camp 6 (Autumn 2011)

Saturday October 1st

Pace University

Registration Still Open

http://CodeCampNyc.org

Page 5: Boulos Dib September 21, 2011files.meetup.com/1585210/IntroductionToPowerShell.pdf · Wes Montgomery, Larry Carlton and Ritchie Blackmore (Deep Purple, Rainbow)

Interactive Command Shell Programmatic (Script) Execution

Environment Dynamic Scripting Language Extensible (CmdLets, .Net etc…) Hosted (i.e. NuGet) Management tool for Servers

Page 6: Boulos Dib September 21, 2011files.meetup.com/1585210/IntroductionToPowerShell.pdf · Wes Montgomery, Larry Carlton and Ritchie Blackmore (Deep Purple, Rainbow)

“A shell is the piece of software that lets you access the functionality provided by the operating system. “ Bruce Payette - Co-Designer and Implementer of the PowerShell language. Example

Windows Explorer Command.com Cmd.exe Bash (Unix) PowerShell

Page 7: Boulos Dib September 21, 2011files.meetup.com/1585210/IntroductionToPowerShell.pdf · Wes Montgomery, Larry Carlton and Ritchie Blackmore (Deep Purple, Rainbow)

Interactive Environment with .Net Automation Tool Easy to use Available on all Windows SKUs starting with

XP SP2 and Windows 2003 Management tool for Servers

Productivity Gains – One Liner

Page 8: Boulos Dib September 21, 2011files.meetup.com/1585210/IntroductionToPowerShell.pdf · Wes Montgomery, Larry Carlton and Ritchie Blackmore (Deep Purple, Rainbow)

Source:Wikipedia

Page 9: Boulos Dib September 21, 2011files.meetup.com/1585210/IntroductionToPowerShell.pdf · Wes Montgomery, Larry Carlton and Ritchie Blackmore (Deep Purple, Rainbow)

Common Parameters -Verbose -Debug -WarningAction -WarningVariable -ErrorAction -ErrorVariable -OutVariable -OutBuffer

Risk Mitigation Parameters (certainly critical in a production environment)

What-If -Confirm

Wildcard support.

All names and parameter value can support wildcard. Pipeline

Much more about this later.

Page 10: Boulos Dib September 21, 2011files.meetup.com/1585210/IntroductionToPowerShell.pdf · Wes Montgomery, Larry Carlton and Ritchie Blackmore (Deep Purple, Rainbow)

Command vs. Expression mode parsing Echo 1+1

1+1 Everything returns a value “String”

Variable $ Prefix

i.e. $var = “Hello Sharepoint” Type System All .Net types as well as Custom Types

Page 11: Boulos Dib September 21, 2011files.meetup.com/1585210/IntroductionToPowerShell.pdf · Wes Montgomery, Larry Carlton and Ritchie Blackmore (Deep Purple, Rainbow)

Help Get-Help (or -? Following any command) Get-Help about_<<anyname>> Get-Help –Examples Get-Help –Full

Page 12: Boulos Dib September 21, 2011files.meetup.com/1585210/IntroductionToPowerShell.pdf · Wes Montgomery, Larry Carlton and Ritchie Blackmore (Deep Purple, Rainbow)

High level task oriented abstraction Verb-XXNoun Verbs: Get, Set, New, Write, Read

Nouns: Drive, Variable, Provider, Site, Collection Get-Verb Predefined Commands When Starting Remember these: Get-Help

Get-Member

Get-Command

Page 13: Boulos Dib September 21, 2011files.meetup.com/1585210/IntroductionToPowerShell.pdf · Wes Montgomery, Larry Carlton and Ritchie Blackmore (Deep Purple, Rainbow)

Get-Help As it says, it helps!!!

Get-Command Get information about what can be invoked

Get-Member Show what can be done with an object

Get-Module Show packages of commands

GetType Discover details about an object’s type information.

Page 14: Boulos Dib September 21, 2011files.meetup.com/1585210/IntroductionToPowerShell.pdf · Wes Montgomery, Larry Carlton and Ritchie Blackmore (Deep Purple, Rainbow)

Compare Foreach Group Measure Select Sort Tee Where

Page 15: Boulos Dib September 21, 2011files.meetup.com/1585210/IntroductionToPowerShell.pdf · Wes Montgomery, Larry Carlton and Ritchie Blackmore (Deep Purple, Rainbow)

PowerShell ISE

Simple Editor and Debugger

PowerGUI

Administrative Console

PowerGUI Editor

Powerpacks – a number to choose from.

Visual Studio Notepad

Page 16: Boulos Dib September 21, 2011files.meetup.com/1585210/IntroductionToPowerShell.pdf · Wes Montgomery, Larry Carlton and Ritchie Blackmore (Deep Purple, Rainbow)

Out-Host Out-Null Out-Printer Out-String Out-GridView

Page 17: Boulos Dib September 21, 2011files.meetup.com/1585210/IntroductionToPowerShell.pdf · Wes Montgomery, Larry Carlton and Ritchie Blackmore (Deep Purple, Rainbow)

The best part about PowerShell

Output of one CmdLet is Input into next CmdLet in pipeline.

Uses the Pipe operator |

Output and Input are objects, not text like traditional shells.

Example

▪ Get-Command | Get-Member

▪ Get-Process | Out-GridView

Page 18: Boulos Dib September 21, 2011files.meetup.com/1585210/IntroductionToPowerShell.pdf · Wes Montgomery, Larry Carlton and Ritchie Blackmore (Deep Purple, Rainbow)

Case-Insensitive Variables: begin with $ (i.e. $a = “test”) Script Blocks using {} Array $a = 1,2,3 $a[1]

Hashtable $h = @{a=1; b=2}

Scope – Functions and Script Blocks Security Context aware Remoting - WSMan

Page 19: Boulos Dib September 21, 2011files.meetup.com/1585210/IntroductionToPowerShell.pdf · Wes Montgomery, Larry Carlton and Ritchie Blackmore (Deep Purple, Rainbow)

If then else If ($a –eq “test”) { “It’s a test”} else {“Not”}

While loop and Do While loop $i = 1; While ($i –lt 10) {$i++}

$i =5; do {$i} while (--$i) For loop for ($i=0; $i –lt 10; $i++) { “5 * $i is $(5 * $i)” }

Foreach loop Foreach ($i in 1..10) {“`$i is $i”}

Page 20: Boulos Dib September 21, 2011files.meetup.com/1585210/IntroductionToPowerShell.pdf · Wes Montgomery, Larry Carlton and Ritchie Blackmore (Deep Purple, Rainbow)

Foreach CmdLet

1..10 | ForEach-Object {“begin”} {$_ * 2} {“end”}

Where Cmdlet

1..10 | Where-Object {$_ -gt 4 -and $_ -lt 10}

Page 21: Boulos Dib September 21, 2011files.meetup.com/1585210/IntroductionToPowerShell.pdf · Wes Montgomery, Larry Carlton and Ritchie Blackmore (Deep Purple, Rainbow)

Arithmetic Operators

+ * - / %

Assignment Operators

=, +=, -=, *=, /=, %=

Page 22: Boulos Dib September 21, 2011files.meetup.com/1585210/IntroductionToPowerShell.pdf · Wes Montgomery, Larry Carlton and Ritchie Blackmore (Deep Purple, Rainbow)

Get-Location and SetLocation Copy-Item Remove-Item Move-Item Rename-Item Set-Item New-Item Get-Content

Page 23: Boulos Dib September 21, 2011files.meetup.com/1585210/IntroductionToPowerShell.pdf · Wes Montgomery, Larry Carlton and Ritchie Blackmore (Deep Purple, Rainbow)

Pretty much the same as CMD

> replace file

>> Append to file

2> File is replaced with error messages

2>> Error text is appended to file

2>&1 Error messages are written to output pipe

Page 24: Boulos Dib September 21, 2011files.meetup.com/1585210/IntroductionToPowerShell.pdf · Wes Montgomery, Larry Carlton and Ritchie Blackmore (Deep Purple, Rainbow)

A module is a package that contains Windows PowerShell commands, such as cmdlets, providers, functions, variables, and aliases

Need to create module folder new-item -type directory -path

$home\Documents\WindowsPowerShell\

Modules

Copy the module to the Modules folder. Start using a module (import-module etc…)

Page 25: Boulos Dib September 21, 2011files.meetup.com/1585210/IntroductionToPowerShell.pdf · Wes Montgomery, Larry Carlton and Ritchie Blackmore (Deep Purple, Rainbow)

Standard Providers Windows PowerShell providers are Microsoft .NET Framework-based

programs that make the data in a specialized data store available in Windows PowerShell so that you can view and manage it

Get-PSProvider | Select -Property Name WSMan - Alias Environment FileSystem Function Registry Variable Certificate

Page 26: Boulos Dib September 21, 2011files.meetup.com/1585210/IntroductionToPowerShell.pdf · Wes Montgomery, Larry Carlton and Ritchie Blackmore (Deep Purple, Rainbow)

Get-PSDrive New-PSDrive

New-PSDrive -Name Y -PSProvider FileSystem -Root c:\temp

Remove-PSDrive Remove-PSDrive

Try a non-disk PSDrive like cert: Dir Cert:

Page 27: Boulos Dib September 21, 2011files.meetup.com/1585210/IntroductionToPowerShell.pdf · Wes Montgomery, Larry Carlton and Ritchie Blackmore (Deep Purple, Rainbow)

A Script file is a text file with .ps1 extension containing one or more PowerShell command

A Script is a simple mechanism to re-use functionality.

To run a script on a remote computer, use the Invoke-Command and provide remote computer name as a parameter.

Scripts can accept parameters. To run a script in the current session, we Dot-

Source the . .\Script1.ps1 We can Scope Local or Global.

Page 28: Boulos Dib September 21, 2011files.meetup.com/1585210/IntroductionToPowerShell.pdf · Wes Montgomery, Larry Carlton and Ritchie Blackmore (Deep Purple, Rainbow)

Single Line: # Multi Line:

<#

#>

Comments can be used to automatically generate help

Page 29: Boulos Dib September 21, 2011files.meetup.com/1585210/IntroductionToPowerShell.pdf · Wes Montgomery, Larry Carlton and Ritchie Blackmore (Deep Purple, Rainbow)

A function is a script block containing list of statements

function small_files ($size = 1kB) { Get-ChildItem c:\Temp | where { $_.length -lt $size -and

!$_.PSIsContainer} } small_files

To control how a function uses the pipeline, you use Begin, Process and End.

function pipelineFunc {

process {"The value is: $_"}

}

1,2,3 | pipelineFunc

Advanced functions allow you to write CmdLets using scripts instead of compiled code.

Page 30: Boulos Dib September 21, 2011files.meetup.com/1585210/IntroductionToPowerShell.pdf · Wes Montgomery, Larry Carlton and Ritchie Blackmore (Deep Purple, Rainbow)

try

{

$wc = new-object System.Net.WebClient

$wc.DownloadFile("http://www.contoso.com/MyDoc.doc","c:\MyDoc.doc")

}

catch [System.Net.WebException],[System.IO.IOException]

{

"unable to download MyDoc.doc from http://www.contoso.com."

}

catch

{

"An error occurred that could not be resolved."

}

Page 31: Boulos Dib September 21, 2011files.meetup.com/1585210/IntroductionToPowerShell.pdf · Wes Montgomery, Larry Carlton and Ritchie Blackmore (Deep Purple, Rainbow)

Sharepoint Management Shell Need to execute Add-SPShellAdmin in order

to acquire permissions to run PowerShell on Sharepoint

Farm component

Member of Administrators group on the local computer

Member of Farm Administrators SharePoint group

Full Control on backup folder

Farm Yes No Yes

Service application Yes No Yes

Content database Yes No Yes

Site collection No Yes Yes

Site, list, document library Yes No Yes

Source: MSDN

Page 32: Boulos Dib September 21, 2011files.meetup.com/1585210/IntroductionToPowerShell.pdf · Wes Montgomery, Larry Carlton and Ritchie Blackmore (Deep Purple, Rainbow)

Get-Command -Noun SP* (Get-Command –Name *-SP* -

CommandType cmdLet).Count

Page 33: Boulos Dib September 21, 2011files.meetup.com/1585210/IntroductionToPowerShell.pdf · Wes Montgomery, Larry Carlton and Ritchie Blackmore (Deep Purple, Rainbow)

$Host.Runspace.ThreadOptions = "ReuseThread"

Get-SPAssignment –Global

$spWeb = Get-SPWeb -Identity $url

$spWeb.TreeViewEnabled = $True

$spWeb.Update()

Stop-SPAssignment –Global

Page 34: Boulos Dib September 21, 2011files.meetup.com/1585210/IntroductionToPowerShell.pdf · Wes Montgomery, Larry Carlton and Ritchie Blackmore (Deep Purple, Rainbow)

Windows Powershell Blog http://blogs.msdn.com/b/powershell/

Doug Finke – MVP (Also ShowUI) http://dougfinke.com/blog/

PowerShell Magazine http://www.powershellmagazine.com/

Jim Christopher MVP (Check out StudioShell) http://www.beefycode.com/

Tome Tanasovski MVP/Author – NYC PowerShell User Group http://powertoe.wordpress.com/

Page 35: Boulos Dib September 21, 2011files.meetup.com/1585210/IntroductionToPowerShell.pdf · Wes Montgomery, Larry Carlton and Ritchie Blackmore (Deep Purple, Rainbow)

Productivity PowerGUI

http://PowerGUI.org

PowerTab http://powertab.codeplex.com/

Community Extensions http://pscx.codeplex.com

Quest ActiveRoles Management Shell http://www.quest.com/powershell/activeroles-server.aspx

UI

ShowUI (WPF) – http://showui.codeplex.com

Page 36: Boulos Dib September 21, 2011files.meetup.com/1585210/IntroductionToPowerShell.pdf · Wes Montgomery, Larry Carlton and Ritchie Blackmore (Deep Purple, Rainbow)

Tool Url

PowerGUI http://PowerGUI.org/

PowerTab http://powertab.codeplex.com/

Community Extensions http://pscx.codeplex.com/

Quest ActiveRoles http://www.quest.com/powershell/activeroles-server.aspx/

ShowUI http://showui.codeplex.com/

Windows Automation Snaping for Powershell

http://wasp.codeplex.com/

Page 37: Boulos Dib September 21, 2011files.meetup.com/1585210/IntroductionToPowerShell.pdf · Wes Montgomery, Larry Carlton and Ritchie Blackmore (Deep Purple, Rainbow)

NuGet

http://nuget.org/

http://nuget.codeplex.com/

StudioShell

http://studioshell.codeplex.com/

Page 38: Boulos Dib September 21, 2011files.meetup.com/1585210/IntroductionToPowerShell.pdf · Wes Montgomery, Larry Carlton and Ritchie Blackmore (Deep Purple, Rainbow)

PowerShell in Action Bruce Payette “The book from the authority on

PowerShell”

Windows PowerShell 2.0 Bible By Thomas Lee, Karl Mitschke, Mark

E. Schill, and Tome Tanasovski http://powertoe.wordpress.com/

Page 39: Boulos Dib September 21, 2011files.meetup.com/1585210/IntroductionToPowerShell.pdf · Wes Montgomery, Larry Carlton and Ritchie Blackmore (Deep Purple, Rainbow)

PowerShell for Microsoft Sharepoint 2010 Administrators.

Niklas Goude & Mattias Karlsson http://www.powershell.nu/

Automating Microsoft SharePoint 2010 Administration with Windows PowerShell 2.0

Gary Lapointe & Shannon Bray http://blog.falchionconsulting.com/

Page 40: Boulos Dib September 21, 2011files.meetup.com/1585210/IntroductionToPowerShell.pdf · Wes Montgomery, Larry Carlton and Ritchie Blackmore (Deep Purple, Rainbow)

Next session will be about PowerShell Scripts in the SharePoint Management Shell

Contact:

http://blog.boulosdib.com

@boulosdib