SP2010 Management Shell + Stsadm

A small collection of SharePoint2010 Management Shell and Stsadm snippets…

Shell Cannot be loaded

You are on remote desktop to Sharepoint Server and do “Run as Admin” on Sharepoint 2010 Management Shell. But that is not enough – you get:

File C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\CO
NFIG\POWERSHELL\Registration\SharePoint.ps1 cannot be loaded because the execut
ion of scripts is disabled on this system. Please see "get-help about_signing"
for more details.
At line:1 char:3
+  & <<<<  ' C:\Program Files\Common Files\Microsoft Shared\Web Server Extensio
ns\14\CONFIG\POWERSHELL\Registration\\sharepoint.ps1 '
+ CategoryInfo          : NotSpecified: (:) [], PSSecurityException
+ FullyQualifiedErrorId : RuntimeException

PS C:\Windows\system32>

Fix: Create a .bat file and do “Run as Admin” on that

SPMgmtShell.bat :

C:\Windows\System32\WindowsPowerShell\v1.0\powershell Set-ExecutionPolicy Unrestricted
C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe  -NoExit  " & ' C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\CONFIG\POWERSHELL\Registration\\sharepoint.ps1 ' "
C:\Windows\System32\WindowsPowerShell\v1.0\powershell Set-ExecutionPolicy restricted

I need a bat file to start my .ps1 file

Create a .bat file and do “Run as Admin” on that

YourWonderScript.bat :

echo You must run this script as admin!
c:
cd C:\YourScriptFolder

powershell Set-ExecutionPolicy Unrestricted
powershell Get-ExecutionPolicy
rem One argument is included in the command below - just as a showcase - remove it if you don't need it
powershell .\'YourWonderScript.ps1' $true
powershell Set-ExecutionPolicy Restricted
pause

Please give me a name for that feature…

Got this error:

Feature with Id ‘8581a8a7-cf16-4770-ac54-260265ddb0b2’ is not installed in this farm, and cannot be added to this scope.

Ok easy to get a name with PS:

PS C:\> Get-SPFeature 8581a8a7-cf16-4770-ac54-260265ddb0b2

DisplayName                    Id                                       Scope
-----------                    --                                       -----
PremiumSite                    8581a8a7-cf16-4770-ac54-260265ddb0b2     Site
PS C:\>

Deactivate and activate sitecoll and web features in the whole webapp

Loop through sitecols and webs in a webapp with PS:

$wa = Get-SPWebapplication 'http://mywebapp.com'
# $wa.Sites | ForEach-Object { $_.url }
# Enable each SiteColl feature in the WA with a specific id
$wa.Sites | ForEach-Object { Enable-SPFeature -Id:MyWebapp.Layout.Installv4Master -Url:$_.Url -Confirm:$false }
# Disable each Web feature in the WA with a specific id
$wa.Sites | Get-SPWeb -limit:all | ForEach-Object { Disable-SPFeature -Id:MyWebapp.Layout.ChangeToMaster -Url:$_.Url -Confirm:$false }

You could also have used Get-SPContentDatabase instead of Get-SPWebapplication.

$db = Get-SPContentDatabase -Id:WSS_Content_Somebase
$wa = $db.Sites[0].WebApplication

Print those active features

Loop through the whole farm for active features

PrintFeatures.bat

rem you might need to run this script as admin

#C:\Windows\System32\WindowsPowerShell\v1.0\powershell Set-ExecutionPolicy Unrestricted

# Enter DisPlayName of feature as parm
C:\Windows\System32\WindowsPowerShell\v1.0\powershell .\'PrintFeatures.ps1' 'BaseWeb'

#C:\Windows\System32\WindowsPowerShell\v1.0\powershell Set-ExecutionPolicy restricted

pause

PrintFeatures.ps1

param
(
 #Optional featurename
 [string]$featureDisplayname
)
#Get-PSSnapin -Registered
#Add SP snapin
Add-PSSnapIn Microsoft.Sharepoint.Powershell

#Load file
#http://stackoverflow.com/questions/6016436/in-powershell-how-do-i-define-a-function-in-a-file-and-call-it-from-the-powershe
. .\SP_Display-FeaturesActivated2.ps1

#Print active features in Farm
if(-not [System.String]::IsNullOrEmpty($featureDisplayname)){
 Get-SPFeatureActivated -Identity $featureDisplayname
}else{
 Get-SPFeatureActivated
}
Write-Host "Debug End"

Read about SP_Display-FeaturesActivated2.ps1 and download it from here.

Read Uls log

Get-SPLogEvent | ?{$_.Correlation -eq "9e91d740-2130-4a00-a6cb-b7db1affcd46"} | ft Category, Message -Autosize 

From http://www.wictorwilen.se/Post/Working-with-SharePoint-2010-Correlation-ID-in-PowerShell-and-code.aspx

Developer Dashboard

Verify and enable developer dashboard:

PS C:\> stsadm -o getproperty -pn developer-dashboard
<Property Exist="Yes" Value="Off" />
PS C:\> stsadm -o setproperty -pn developer-dashboard -pv OnDemand
Operation completed successfully.
PS C:\> stsadm -o getproperty -pn developer-dashboard
<Property Exist="Yes" Value="OnDemand" />

There is also the option “On” for always on.

Upgrade features

On existing sites you often want to upgrade features by

  1. Update code and features including specific <Feature><UpgradeActions>
  2. Update-SPSolution with you upgraded wsp package containing the upgraded feature(s).
  3. Upgrade features on existing sites. There are several ways to do that:
    • Upgrade-SPFeatures. This is a powershell command installed with a “Feature Upgrade Kit”:
      • #Install FeatureUpgradeKit
        add-spsolution –LiteralPath:"\\myserver\dropfolder\COB.SharePoint.Utilities.FeatureUpgradeKit.wsp"
        install-SPSolution -Identity:COB.SharePoint.Utilities.FeatureUpgradeKit.wsp -GacDeployment
        
      • Restart SharePoint Mgmt Shell to be able to use the new cmdlet e.g. Upgrade-SPFeatures.
      • You can also go to CA – System Settings – Manage Feature Upgrades (http://CA/_admin/COB/CentralAdminFeatureUpgrade.aspx) //this page is part of Feature Upgrade Kit, too.
    • If you do not use above project then alternatives can be a script.

SiteColl Restore and SP BuildVersion

When trying to restore a SiteColl to another farm there were some mismatch between product, version or patch:

#Restore a SiteColl
Restore-SPSite http://somesite.xx/sites/abcSite -Path C:\backup\somesite.xx.abcSite.bak -ContentDatabase:WSS_Content_somesite_80
#Response: Restore-SPSite : Your backup is from a different version of Microsoft SharePoint Foundation and cannot be restored to a server running the current version.
#The backup file should be restored to a server with version '14.0.0.6120' or later.

What product/version/patch of SP is installed where? That can be something of a mistery. What you can to is to

#Check farm version and products
Get-SPFarm | Select BuildVersion, Products
#BuildVersion Products
#------------ --------
#14.0.6106.5000 {beed1f75-c398-4447-aef1-e66e1f0df91...

You can translate the product GUIDs to text using the ps1 script from the link.

This can also be found on page http://CA/_admin/FarmServers.aspx, but that page won’t tell you that you also have a Foundation product as the GUIDs does.

To get info of the patch status go to
http://CA/_admin/PatchStatus.aspx

MySPServer Microsoft SharePoint 2010 Service Pack 1 (SP1) 14.0.6029.1000 Installed

The solution for the restore could be to install latest CU.

Here is a list of SP2010 and 2007 patches.

SPCfgWiz failed with SQL connection error

After adding a SP CU the SPCfgWiz failed with some SQL connection error and at the last screen. The DB was enclosed in quotes and there was a space included at the end. The Fix was

  1. Make a note via CA which services are currently running on the server
  2. Disconnect server from farm via cmd
  3. Refresh product versions on the server
  4. Reconnect server to farm via Wizard
  5. Start the services from step 1

1: Goto http://CA/_admin/FarmServers.aspx and make a note of which services are currently running on the failing server.

2: Disconnect server from farm.
Ref: http://technet.microsoft.com/en-us/library/cc263093(v=office.14).aspx#section2

psconfig.exe -cmd configdb -disconnect

3: Refresh product versions on the server according to
http://blogs.msdn.com/b/sowmyancs/archive/2012/08/08/the-license-state-for-the-current-server-doesn-t-match-the-farm-s-license-state-sharepoint-psconfig-error.aspx

Set-SPFarmConfig –InstalledProductsRefresh

4: Reconnect server to farm

  • Exec SPCfgWiz
  • Enter DBserver etc
    => Now OK

5: Start the services from step 1

  • Goto http://CA/_admin/FarmServers.aspx
  • Click on the server that you just connected
  • Start the services from step 1

Footnote

And while we are at SharePoint: Here is a list of cmdlets: http://technet.microsoft.com/en-us/library/ff678226.aspx#NounOrder

The end

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: