+ All Categories
Home > Documents > PowerShell Brian Caauwe – Senior Consultant April 14, 2012 The Administrator’s Best Friend.

PowerShell Brian Caauwe – Senior Consultant April 14, 2012 The Administrator’s Best Friend.

Date post: 28-Dec-2015
Category:
Upload: martin-sparks
View: 214 times
Download: 0 times
Share this document with a friend
41
PowerShell Brian Caauwe – Senior Consultant April 14, 2012 The Administrator’s Best Friend
Transcript
Page 1: PowerShell Brian Caauwe – Senior Consultant April 14, 2012 The Administrator’s Best Friend.

PowerShell

Brian Caauwe – Senior Consultant

April 14, 2012

The Administrator’s Best Friend

Page 2: PowerShell Brian Caauwe – Senior Consultant April 14, 2012 The Administrator’s Best Friend.

Session Agenda

• What you need to know• Top 10 Cmdlets• Top 10 Scripts

Page 3: PowerShell Brian Caauwe – Senior Consultant April 14, 2012 The Administrator’s Best Friend.

• Brian Caauwe– SharePoint Consultant & Speaker• Email: [email protected]• Twitter: @bcaauwe• Blog: http://blog.avtex.com/author/bcaauwe

– Certifications• MCITP: SP Administrator 2010• MCPD: SP Developer 2010

Who am I?

Page 4: PowerShell Brian Caauwe – Senior Consultant April 14, 2012 The Administrator’s Best Friend.

Minnesota SharePoint User Group

• 2nd Wednesday of the Month• 9:00 – 11:30 AM

• SharePoint resources and links• Meeting Schedule• Past User Group Presentations• This Presentation

• Next Meeting – 5/9• TBD

www.sharepointmn.com

Page 5: PowerShell Brian Caauwe – Senior Consultant April 14, 2012 The Administrator’s Best Friend.

Quick Poll• SharePoint Version– 2007 – WSS, MOSS– 2010 – SPF, Server, FAST

• Work Roles– SharePoint Administrator– SharePoint Developer– Business User– Other

Page 6: PowerShell Brian Caauwe – Senior Consultant April 14, 2012 The Administrator’s Best Friend.

What you need to know

Page 7: PowerShell Brian Caauwe – Senior Consultant April 14, 2012 The Administrator’s Best Friend.

What you need to know

• You won’t learn by books or sessions

• Find YOUR practical applications

Page 8: PowerShell Brian Caauwe – Senior Consultant April 14, 2012 The Administrator’s Best Friend.

What you need to know• Run “As Administrator”• Uses powershell.exe under Windows\

system32• Registers Microsoft.SharePoint.PowerShell

snap-in via script– C:\Program Files\Common Files\Microsoft Shared\Web

Server Extensions\14\CONFIG\POWERSHELL\ Registration\sharepoint.ps1

• Sets Execution Policy to RemoteSigned

Management Shell

Page 9: PowerShell Brian Caauwe – Senior Consultant April 14, 2012 The Administrator’s Best Friend.

What you need to know• Microsoft.SharePoint namespace• Server Architecture

– SPFarm– SPWebApplication– SPContentDatabase

• Site Architecture– SPSite– SPWeb– SPList– SPListItem

• MSDN Resource– http://msdn.microsoft.com/en-us/library/ms473633.aspx

Object Model

Page 10: PowerShell Brian Caauwe – Senior Consultant April 14, 2012 The Administrator’s Best Friend.

What you need to know• Call PowerShell from Windows\system32• Register Microsoft.SharePoint.Powershell

snap-in-psconsolefile “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\CONFIG\POWERSHELL \Registration\psconsole.psc1”

• Call Script-command “E:\PowerShell\Set-ScriptName.ps1”

• Logging

Scheduled Tasks

Page 11: PowerShell Brian Caauwe – Senior Consultant April 14, 2012 The Administrator’s Best Friend.

What you need to know• Disposable Objects

$web.Dispose()$site.Dispose()

• SPAssignment – Garbage Collector– GlobalStart-SPAssignment –Global

Get-SPSite -Limit All | Get-SPWeb | Select Url, WebTemplate, WebTemplateId | Format-Table -AutoSize

Stop-SPAssignment -Global

– Scoped$w = Get-SPWebApplication http://www.company.com$siteScope = Start-SPAssignmentforeach ($site in ($siteScope | Get-SPSite -Limit All –WebApplication $)){

$webScope = Start-SPAssignmentforeach ($web in ($webScope | Get-SPWeb -Limit All -Site $site)){

## Do Something}Stop-SPAssignment $webScope

}Stop-SPAssignment $siteScope

Memory Leakage

Page 12: PowerShell Brian Caauwe – Senior Consultant April 14, 2012 The Administrator’s Best Friend.

What you need to know• Crazy setup• Run Commands on SharePoint servers

– Enable-PSRemoting –force– Enable-WSManCredSSP –role Server –force– Set-Item WSMan:\localhost\Shell\MaxMemoryPerShellMB 1024

• Run Commands on local machine– Enable-PSRemoting -force– Enable-WSManCredSSP –role Client –DelegateComputer

“*.domain.com or COMPUTERNAME” –force

• Shared SPModule (\\servername\spmodule)– Zach Rosenfields’s Blog

http://sharepoint.microsoft.com/blogs/zach/Lists/Posts/Post.aspx?ID=54• Store Credentials in a variable

– $cred = Get-Credential

• Load Modules– $env:PSModulePath = \\servername\spmodule; + $env:PSModulePath– Import-Module SPModule.misc– Import-Module SPModule.setup

Remote Scripting

Page 13: PowerShell Brian Caauwe – Senior Consultant April 14, 2012 The Administrator’s Best Friend.

What you need to know• Other Assemblies– IIS (WebAdministration)– SQL– Exchange

• User Profile– Microsoft.Office.Server.UserProfiles

• Managed Metadata– Microsoft.SharePoint.Taxonomy

What you don’t get

Page 14: PowerShell Brian Caauwe – Senior Consultant April 14, 2012 The Administrator’s Best Friend.

Top 10 Cmdlets

Page 15: PowerShell Brian Caauwe – Senior Consultant April 14, 2012 The Administrator’s Best Friend.

Top 10 Cmdlets

• Tools for the toolbox• Some SharePoint, Some NOT– SharePoint requires 2010

Page 16: PowerShell Brian Caauwe – Senior Consultant April 14, 2012 The Administrator’s Best Friend.

Top 10 Cmdlets

• How to Use– Built in help EVERYWHERE– Start with what you know to find what you don’t

• Examples– Get-Command *-SPFeature*– Get-Help Backup-SPSite -Examples

Get-Command | Get-Help

Page 17: PowerShell Brian Caauwe – Senior Consultant April 14, 2012 The Administrator’s Best Friend.

Top 10 Cmdlets

• How to use– Main Cmdlets you will use– Limit parameter– SPSite and SPWeb are disposable

• Example– $w = Get-SPWebApplication http://my.company.com– $site = Get-SPSite http://my.company.com– $site | Get-SPWeb -Limit All

Get-SPWebApplication | Get-SPSite | Get-SPWeb

Page 18: PowerShell Brian Caauwe – Senior Consultant April 14, 2012 The Administrator’s Best Friend.

Top 10 Cmdlets

• How to use– Maintain services on server– Get current server with environment variable– Start-SPServiceInstance may need other configuration

• Example– $server = Get-SPServer $env:COMPUTERNAME– $svc = Get-SPServiceInstance -Server $server | ?

{$_.TypeName -eq “Excel Calculation Services”}– if ($svc.Status –eq “Disabled”){ Start-

SPServiceInstance }

Get-SPServer | Get-SPServiceInstance

Page 19: PowerShell Brian Caauwe – Senior Consultant April 14, 2012 The Administrator’s Best Friend.

Top 10 Cmdlets

• How to use– Maintain properties of service applications– Pipe to query on TypeName

• Example– $svcApp = Get-SPServiceApplication | ?{$_.TypeName -

eq “User Profile Service Application”}– $svcApp.NetBIOSDomainNamesEnabled = $true– $svcApp.Update()

Get-SPServiceApplication

Page 20: PowerShell Brian Caauwe – Senior Consultant April 14, 2012 The Administrator’s Best Friend.

Top 10 Cmdlets

• How to use– Great for troubleshooting– Creates new log file on that server

• Example– New-SPLogFile

New-SPLogFile

Page 21: PowerShell Brian Caauwe – Senior Consultant April 14, 2012 The Administrator’s Best Friend.

Top 10 Cmdlets

• How to use– Merges last hour of logs across farm– Can consume CorrelationId and other filters

• Example– Merge-SPLogFile –Path “C:\Debug\error.log” –

Correlation 470a4ec2-985d-43be-a14e-176eb1c39672

Merge-SPLogFile

Page 22: PowerShell Brian Caauwe – Senior Consultant April 14, 2012 The Administrator’s Best Friend.

Top 10 Cmdlets

• How to use– Gets ALL log events (no time filter)– Single server– Pipe to query and use -StartTime parameter

• Example– $guid = “470a4ec2-985d-43be-a14e-176eb1c39672”– Get-SPLogEvent –StartTime (Get-Date).AddHours(-1) | ?

{$_.Correlation –eq $guid} | Format-List Timestamp, Category, Message | Out-File “c:\Debug\debug.log”

Get-SPLogEvent

Page 23: PowerShell Brian Caauwe – Senior Consultant April 14, 2012 The Administrator’s Best Friend.

Top 10 Cmdlets

• How to use– Add farm solution to solution store– Automated deployments

• Example– Add-SPSolution –LiteralPath “C:\Solutions\spcmis.wsp”– $sol = Get-SPSolution | ?{$_.Name –eq “spcmis.wsp”}– $sol | Install-SPSolution –GACDeployment -

AllWebApplications

Add-SPSolution | Get-SPSolution | Install-SPSolution

Page 24: PowerShell Brian Caauwe – Senior Consultant April 14, 2012 The Administrator’s Best Friend.

Top 10 Cmdlets

• How to use– Write to the file– Nice for logging on scheduled tasks– Use -Append parameter

• Example– $msg = “This is what I want to send to the file”– $path = “logging.log”– $currentTime = Get-Date –UFormat “%H:%M:%S”– $msg = $currentTime + “ :: DEB :: “ + $msg– $msg | Out-File $path -append

Out-File

Page 25: PowerShell Brian Caauwe – Senior Consultant April 14, 2012 The Administrator’s Best Friend.

Top 10 Cmdlets

• How to use– Write-Host used to display status to the screen

• Use ForegroundColor for color coding

– Read-Host used to get user input• Example

– $url = Read-Host “What is the web application URL”– $w = Get-SPWebApplication $url– Write-Host -ForegroundColor Green $w.Url

Write-Host | Read-Host

Page 26: PowerShell Brian Caauwe – Senior Consultant April 14, 2012 The Administrator’s Best Friend.

Top 10 Scripts

Page 27: PowerShell Brian Caauwe – Senior Consultant April 14, 2012 The Administrator’s Best Friend.

Top 10 ScriptsMy favorites to find information in a crazy world

Page 28: PowerShell Brian Caauwe – Senior Consultant April 14, 2012 The Administrator’s Best Friend.

Top 10 Scripts• What it uses– Merge-SPLogFile– Get-SPLogEvent

• When to use it– When you get a correlation ID

• How to use it– Choose to query farm or current server

Get-ErrorLog.ps1

Page 29: PowerShell Brian Caauwe – Senior Consultant April 14, 2012 The Administrator’s Best Friend.

Top 10 Scripts• What it uses– Start-SPAssignment / Stop-SPAssignment– Get-SPWebApplication / Get-SPSite

• When to use it– Get storage size and quota

• How to use it– Run as scheduled task or on demand

Get-SiteStorageInfo.ps1

Page 30: PowerShell Brian Caauwe – Senior Consultant April 14, 2012 The Administrator’s Best Friend.

Top 10 Scripts• What it uses– Start-SPAssignment / Stop-SPAssignment– Get-SPWebApplication / Get-SPSite / Get-SPWeb

• When to use it– Get web properties (Url, Template, Author, Last Modified)

• How to use it– Run as scheduled task or on demand

Get-WebInfo.ps1

Page 31: PowerShell Brian Caauwe – Senior Consultant April 14, 2012 The Administrator’s Best Friend.

Top 10 Scripts• What it uses– Start-SPAssignment / Stop-SPAssignment– Get-SPWebApplication / Get-SPSite / Get-SPWeb– SPList / SPContentType / SPWorkflowAssociation

• When to use it– Get workflow properties (SPD, Author, Running Instances)

• How to use it– Run as scheduled task or on demand

Get-WorkflowInfo.ps1

Page 32: PowerShell Brian Caauwe – Senior Consultant April 14, 2012 The Administrator’s Best Friend.

Top 10 Scripts• What it uses– Start-SPAssignment / Stop-SPAssignment– Get-SPWebApplication / Get-SPSite / Get-SPWeb– SPList

• When to use it– Get version properties (Item Count, Versioning, Limits)

• How to use it– Run as scheduled task or on demand

Get-VersionInfo.ps1

Page 33: PowerShell Brian Caauwe – Senior Consultant April 14, 2012 The Administrator’s Best Friend.

Top 10 Scripts• What it uses

– System.Net.CredentialCache / WebClient– Get-SPWebApplication / Get-SPSite– Get-Content

• When to use it– Keep system connections “hot”

• How to use it– Run as scheduled task or on demand

• Reference– Kirk Hofer

• http://kirkhofer.wordpress.com/2008/10/18/sharepoint-warm-up-script/

– Martin Laukkanen• http://nearbaseline.com.au/blog/2010/02/powershell-warmup-script-2/

Warmup-Farm.ps1

Page 34: PowerShell Brian Caauwe – Senior Consultant April 14, 2012 The Administrator’s Best Friend.

Top 10 Scripts• What it uses

– Microsoft.Office.Server.UserProfiles– Start-Transcript– Update-SPProfilePhotoStore

• When to use it– Centrally managed photos to push into SharePoint

• How to use it– Run as scheduled task or on demand

Set-UserProfileImages.ps1

Page 35: PowerShell Brian Caauwe – Senior Consultant April 14, 2012 The Administrator’s Best Friend.

Top 10 Scripts• What it uses– Get-SPWeb / SPSite– SPAlert

• When to use it– Fix up alerts after changing web application url or site

collection url• How to use it

– Invoke-AlertFixup -site “http://teams/sites/newteam” -oldurl “http://teams/sites/oldteam”

• Reference– TechNet

• http://technet.microsoft.com/en-us/library/cc508847.aspx

Invoke-AlertFixup.ps1

Page 36: PowerShell Brian Caauwe – Senior Consultant April 14, 2012 The Administrator’s Best Friend.

Top 10 Scripts

• What it uses– Get-Item– New-ItemProperty

• When to use it– Need to check for registry value and create if

needed• How to use it– Use as part of build scripts other server admin

Check-Loopback.ps1

Page 37: PowerShell Brian Caauwe – Senior Consultant April 14, 2012 The Administrator’s Best Friend.

Top 10 Scripts

• What it uses– Microsoft.Web.Administration

• When to use it– Used to recover application pool credentials

• How to use it– Run on demand, requires application pool name

• Reference– Raymond Mitchell (IWKid)

• http://www.iwkid.com/blog/Lists/Posts/Post.aspx?ID=85

Get-AccountCreds.ps1

Page 38: PowerShell Brian Caauwe – Senior Consultant April 14, 2012 The Administrator’s Best Friend.

DEMO

Page 39: PowerShell Brian Caauwe – Senior Consultant April 14, 2012 The Administrator’s Best Friend.

References• Brian’s Blog

– http://blog.avtex.com/author/bcaauwe• Windows PowerShell for SharePoint 2010

– http://technet.microsoft.com/en-us/sharepoint/ff603532.aspx• Script References

– Invoke-AlertFixup• http://technet.microsoft.com/en-us/library/cc508847.aspx

– Raymond Mitchell’s Blog• http://www.iwkid.com/blog

– Kirk Hofer’s Blog• http://kirkhofer.wordpress.com

– Martin Laukkanen• http://nearbaseline.com.au/blog/2010/02/powershell-warmup-script

-2/

Page 40: PowerShell Brian Caauwe – Senior Consultant April 14, 2012 The Administrator’s Best Friend.

Q & A

Page 41: PowerShell Brian Caauwe – Senior Consultant April 14, 2012 The Administrator’s Best Friend.

Recommended