<

SharePoint Administration and PowerShell - Getting started

Published on
8,274 Points
2,274 Views
Last Modified:
Approved
SharePoint Administration and the PoSH

Background – I started in SharePoint as a contractor Support Engineer for the company that created the product, which after years as a UNIX guy, left me confused and intrigued.

After I relocated to a new state, I tried getting back into the UNIX/Linux world but with such tight competition, it was difficult.

So, I ended up selling a client on a small SharePoint farm to help organize and share their massive collection of stock photographs, while giving them the ability to assign metadata and sort by company and job number.  

This basically was the first time I really got into PowerShell with SharePoint since it required a set of scripts for automating site provisioning…. So lets start from the beginning….

PoSH = PowerShell.  I will repeatedly use this interchangeably

Variables:

These are simply an object that contains something.  Variables can be identified by a preceding $, such as the “new” variable that follows:
$new

We populate variables with the = sign.  The following instantiates (creates an instance of) a SharePoint web object (called an SPWeb object in the Server Object Model vernacular):
$newSPweb = get-spweb http://url.to/my/subsite

Open in new window

You can name your variables pretty much whatever you like, though there are rules – google them if you are curious, since .Net object naming conventions are slightly out of the scope of this document.

The SharePoint Server Object Model:

This is what makes SharePoint Administration on the command line sooo much easier and fun vs the GUI.  It does have some requirements however – mainly, you must be granted SPShellAdmin privilege over every database you wish to perform operations on.
 
If you are in a regular PowerShell screen, you can add the SharePoint assembly by typing the following:
Add-pssnapin Microsoft.sharepoint.powershell

Open in new window

If you do not have SPShellAdmin, you will likely receive an error talking about the farm not being available ….

The following is a command that I run with one of our Farm Admin service accounts every time I find my account does not have SPShellAdmin (we have about 20 different farms at work).  

Just a note – this command must be run with an account that already has SPShellAdmin – it also must be run with the SharePoint Management Shell app with administrative / elevated privileges:
$spdb = get-spdatabase
$username = “domain\myuser”
Foreach ($_ in $spdb) {add-spshelladmin –database $_.id –username $username}

Open in new window

What this command does is it grabs ALL of the database names that the current SharePoint databases and stores them in that $spdb variable and also says that within the loop, they will individually be addressed using the $_ variable.  

The $username variable is the username you wish to get SPShellAdmin privilege to.  The ForEach loop says that, for every item located in the $spdb variable (which is every one of the databases, you can view that simply by typing get-spdatabase), the command between the { } should be ran.  Within the { }, you will see the $_.id – what this is saying is that the particular database item’s ID (GUID) should be used with the –database parameter, and your username used in the –username parameter.

Assuming all went well you will be presented with my favorite output of all absolutely nothing! This counts as one of those ‘no news is good news’ situations.

Moving on ….

Now that we have authorization to be in here, sign out of your service account and sign in with your regular account.

After you are back in, pull open a regular old PoSH window and add the SharePoint assembly:
Add-pssnapin Microsoft.sharepoint.powershell

Open in new window

Next, type
Get-SPWebApplication 

Open in new window

to see all the web applications in your farm.
Lets put one in a variable:
$SPWebApp = get-spwebapplication http://urlToMyWebApp
$SPWebApp | get-member

Open in new window

You should see a giant list of stuff on your screen now – these are all of the member objects that are available through the Object Model to you for play with.
$SPWebApp.sites | select rootweb, url

Open in new window

You should now see a list of all the site collections you previously created in your web application.

Play around with get-member and select using the |  …. That vertical line is called a Pipe – it is how you can pass objects from one command to another.

You may also want to try hitting the TAB button on a partially finished command to see what happens (this is called Tab-Completion and is by far my fav part of modern command line interfaces!)  

Stay tuned for more indepth PoSH articles….
0
Comment
Author:vaderj
0 Comments

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Join & Write a Comment

Key to your CPU's ability to stay cool is to use the right amount of thermal paste and apply it correctly. In other words you want as much thermal conductivity between CPU and the cooling block. Use a quality thermal paste and apply it in a manner…
Check How effective MS Exchange Expert thinks Exchange Mailbox Recovery by SysTools IS. Visit the Official site to get detailed information:- https://www.systoolsgroup.com/exchange-recovery.html (https://www.systoolsgroup.com/exchange-recovery.h…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month