Run PowerShell Script on SharePoint Server

If you have a PowerShell script which should run on a SharePoint Server (Snap In: Microsoft.SharePoint.PowerShell), you have to run it with the credentials of a SP-Shell Admin.

The error message looks like this if the account is not a SP-Shell Admin:

To add the account to the SP-Shell Admin you have to login with an existing SP-Shell Admin. You can get all admins by following command:

  1: Get-SPShellAdmin

After that, start a new PowerShell console with the “Run as administrator” command. If you don’t do this, your command will end with following error message:

  1: Add-SPShellAdmin : You need to have Machine administrator priviliges to run this cmdlet.

Now we are ready to add a new shell admin but there is at least one more obstacle. You have to provide the database as an object. Otherwise, it won’t take effect. I’ve prepared a short script for this:

  1: #Add SP Shell Admin Script

  2:

  3: $spUrl = "http://infsaa1205:200" #Server Name (frontend)

  4: $accountName = "demomyadmin" #Account Name

  5:

  6: #load pshell snapin for SP

  7: if ((Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null)

  8: {

  9:   Add-PSSnapin Microsoft.SharePoint.PowerShell

 10: }

 11:

 12: #lookup content db

 13: $db = Get-SPContentDatabase -site $spUrl

 14:

 15: #add acount to spshell admins

 16: Add-SPShellAdmin -UserName $accountName -database $db

Comments are now closed for this article