Intro to PowerShell

24
Intro to PowerShell Adam Preston Senior Consultant December 4 th , 2010

description

Introduction to PowerShell as well as basic commands used with SharePoint 2010

Transcript of Intro to PowerShell

Page 1: Intro to PowerShell

Intro to PowerShell

Adam PrestonSenior Consultant

December 4th, 2010

Page 2: Intro to PowerShell

Adam Preston – Senior Consultant (TCSC)Born in Richmond, VACertified:– MCITP: SharePoint 2010, Administrator– MCTS: SharePoint 2010, Configuration– MCTS: Microsoft Office SharePoint Server 2007, Configuration– MCTS: Microsoft Windows SharePoint Services 3.0, Configuration

Contact Information:– Email: [email protected]– Twitter: @_apreston– Blog: http://www.tcscblog.com

Who is this guy?

Page 3: Intro to PowerShell

Founded in 1981Based in Richmond, VAConsulting Services and Software Product DevelopmentConsulting Services has been specializing in SharePoint services for over 6 years (since v1)Consistently recognized for quality of technical services– Microsoft Worldwide Partner Conference Technology Innovation Award

Finalist– Microsoft Mid-Atlantic Teamwork Award– Microsoft Mid-Atlantic Loyalty Award– Infineon Technologies’ Technology Innovation Award

Providing Microsoft-centric consulting, application development and work management solutions

The Computer Solution Company (TCSC)

Page 4: Intro to PowerShell

Agenda

What is PowerShell?Where do I find it?What are the benefits of PowerShell?Wait, what happened to STSADM?What commands will get me started?Who has permission to SharePoint 2010 w/PowerShell?Demos, Demos, and Demos!ReferencesQuestions

Page 5: Intro to PowerShell

Windows PowerShell is Microsoft's task automation framework, consisting of a command-line shell and associated scripting language built on top of, and integrated with the .NET Framework. PowerShell provides full access to COM and WMI, enabling administrators to perform administrative tasks on both local and remote Windows systems.DOS on Steroids!A modern replacement for the DOS command-line shell

PowerShell

Page 6: Intro to PowerShell

PowerShell 2.0 is built into Windows 7 and Windows Server 2008 R2For older OS’s, need to download Windows Management Framework to get PS 2.0Start > Accessories > Windows PowerShellSharePoint 2010 Management Shell– Simply a shortcut to a profile that loads the

SP2010 Provider Snapin

PowerShell

Page 7: Intro to PowerShell

Still there and still works– Remains for backwards compatibility– Allows for portability of existing STSADM batch files and scripts– Some commands have been removed (ex. all SSP related commands)

Don’t use this as a crutch! You should start to do everything in PowerShell.Remember that it’s the “14 Hive” now– C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN

PowerShell > STSADM– Newly created cmdlets were developed for enhanced performance– PowerShell has full access to the SharePoint API from the command line

STSADM to Windows PowerShell Mapping:– http://technet.microsoft.com/en-us/library/ff621081.aspx

STSADM?

Page 8: Intro to PowerShell

Windows PowerShell Integrated Scripting Environment (ISE)Host application for PSCan run commands, write, test, and debug scriptsUses color-coding Only available with PS 2.0Server Manager > Add Features > Windows PowerShell ISENeeds to be installed in order to use Out-GridView

PowerShell ISE

Page 9: Intro to PowerShell

Interact with .NET objects directlyRemoting interface– SharePoint PowerShell Remoting Requirements– SharePoint Remoting with PowerShell 2.0

Reduction in the need of some 3rd party solutionsPerform administrative functions in bulk, reducing time and effort.– Will show an example of this in a later demo

Some SharePoint 2010 functions require the use of PowerShell:– Resetting the Farm Passphrase

• How to Reset the Farm Passphrase in SharePoint 2010

– Configuring the State Service Application– Configuring the Publishing Feature’s Super User Accounts

Performance benefits for batch operations

Benefits of PowerShell

Page 10: Intro to PowerShell

Used to find all the commands available to PowerShellCmdlets generally have a verb-noun naming convention– Get-ChildItem, Get-Member, Add-

SPSolution, Backup-SPFarm, Export-SPWeb

Get-Command -noun *infopath*Get-Command -noun *solution*List all available SP2010 cmdlets:– Get-Command -PSSnapin

“Microsoft.SharePoint.PowerShell”

Get-Command

Page 11: Intro to PowerShell

Used to show Synopsis, Syntax, Description, Parameters, and ExamplesSeveral ways to get help information on cmdlets:– Get-Help <cmdlet>

• Get-Help Get-SPSite -full• Get-Help Get-SPSite -examples• Get-Help Get-SPSite –detailed• Get-Help *-SPSite

– <cmdlet> -?– Help <name>

• Ex. Help database, Help Get-SPSite, etc.

Paging– Some results will only show a subset ending

with “-- More --”– <ENTER> = show next line or row– <SPACE> = show next page or subset– Can create paging by piping results to “more”

• Get-SPSite | more

Get-Help

Page 12: Intro to PowerShell

Aliases– A way of calling a command using a

keyword or abbreviation– Most cmdlets have an associated alias to

allow for shortcutting• Get-Command = gcm• Format-List = fl• Set-Location = cd• Get-ChildItem = dir

– Use Get-Alias to get a list of aliases available for your current session

– Use Set-Alias to create your own!

Get-Member– Gets the properties and methods of objects

• Get-SPSite | Get-Member

– Useful for when trying to determine what property you want to output

Aliases and Get-Member

Page 13: Intro to PowerShell

Demo

Navigating PowerShell

Page 14: Intro to PowerShell

Pipeline– Allows output of one cmdlet to be the input of another cmdlet– Examples

• Get-Process | Format-Table Name, CPU• Get-SPWebApplication | Get-SPSite | Get-SPWeb

Select-Object– Allows you to pick available properties instead of grabbing all properties– Get-SPSite | Get-SPWeb | Select URL, WebTemplate, LastItemModifiedDate

Where-Object– Only display results which meet a certain condition– Get-SPLogEvent -StartTime (Get-Date).AddMinutes(-5) | Where-Object {$_.Level -eq “High”}

Sort-Object– Sort the result set by one or more property

• Get-SPSite | Sort Owner, URL

– Sort ascending (default) or descending• Get-SPSite | Select URL, Owner, LastContentModifiedDate | Sort LastContentModifiedDate -Descending

Pipeline, Select, Where, and Sorting

Page 15: Intro to PowerShell

Write-Host– Outputs to PowerShell session window

Write-WarningWrite-ErrorWrite-EventLogOut-File– Outputs to provided path

location– Can use “>” as alias

Out-GridView– Requires PS ISE

Out-Printer– Sends to default printer

Export-CSVExport-XML

Ways to Output

Page 16: Intro to PowerShell

Get-Command Format-*Format-List– Results are:

• Property1: Result• Property2: Result• Property3: Result

Format-Table– Results are:

• Property1 Property2 Property3• Result Result Result

Format-Wide– Only displays one property and puts it into multiple columns

Formatting

Page 17: Intro to PowerShell

Demo

Result Filtering, Output, and Formatting

Page 18: Intro to PowerShell

Most Microsoft products expose their management interface through providers– Windows, SharePoint, Exchange, IIS, SQL, Forefront, etc. – IIS Example:

• Import-Module WebAdministration• Set-ItemProperty ‘IIS:\Sites\SharePoint – 80’ -Name LogFile.directory -Value ‘C:\Logs\IIS’

– Windows Example:• New-ItemProperty HKLM:\System\CurrentControlSet\Control\Lsa\MSV1_0 -Name

“BackConnectionHostNames” -Value “intranet.contoso.com”,”mysites.contoso.com” -PropertyType multistring

This means that you could have your script or command interact with multiple applications at once. Scripting – PowerShell allows for complex scripting, which include functions, loops, parameters, branching, and

much more!– Scripts are saved using the .PS1 file extension– Restricted by default, can change using Set-ExecutionPolicy– Best Practice is to have your scripts signed, don’t leave your environment in an unrestricted state

PowerShell Providers and Scripting

Page 19: Intro to PowerShell

Out-of-the-box only the Farm Account has permission to interact with SharePoint 2010 via PowerShell. This does not include additional Farm Administrators.Get-SPShellAdmin– Lists who has rights to run PowerShell commands against the databases

Add-SPShellAdmin– Must be local admin, db_owner, and securityadmin to run this command properly– User is added to WSS_Admin_WPG group on all WFEs and is added to the SharePoint_Shell_Access

role on the database you specified– Add-SPShellAdmin -username tcsc\dap -database Portal_Content_DB

• If the database switch is not used, then the user is added access to the configuration database

Remove-SPShellAdmin– Remove-SPShellAdmin -username tcsc\dap

Be Careful!

SP2010 PowerShell Permissions

Page 20: Intro to PowerShell

Demo

Permissions and SP2010 Commands

Page 21: Intro to PowerShell

Bing Visual Search

Search Bing for “PowerShell”, click top tab “Visual Search”

Page 22: Intro to PowerShell

Windows PowerShell for SP2010 Resource CenterWindows PowerShell for SP2010 ReferencesTechNet Script Center RepositoryGetting Started with Windows PowerShell for SharePoint AdministratorsPowerShell Videos on Channel9Gary Lapointe’s Custom SharePoint 2010 PowerShell Cmdlets and STSADM CommandsMastering PowerShell (Free eBook - Over 560 Pages!)PowerShell Book(s)– PowerShell for Microsoft SharePoint 2010 Administrators– Windows PowerShell 2.0 Administrator’s Pocket Consultant

References

Page 23: Intro to PowerShell

TCSC Office 2010 Giveaway

TCSC is giving away copies of Microsoft Office 2010 Professional Plus3 Ways To Win (One copy max per person overall)

1. Visit the new TCSC Blog www.tcscblog.com Make a comment on the SharePoint Saturday post with the words

“SharePoint Saturday Rocks” in it

2. Follow @TCSC on Twitter Tweet “Rocking out with @TCSC at #SharePoint Saturday Richmond

#spsric”

3. Visit our booth and provide us your business card

Page 24: Intro to PowerShell

Questions?