Tag Archive for PowerShell

Restart Services Script

Looking for a PowerShell script to restart important services across a SharePoint farm? Look no further because all you need is here 🙂

The script below, initiated by calling the Restart-Services function, restarts the following services on all SharePoint servers in the farm:

  • SharePoint Timer
  • World Wide Web Publishing Service
  • IIS Admin Service
  • SharePoint Administration

Read more

Shrink CrawlStore Database

A useful script for running on your dev\uat environments to free valuable disk space. The crawl store database can grow quite significantly over time.

First of all, the PowerShell script:

# Load the SharePoint assembly - change the version to for SharePoint 2013
Add-Type -AssemblyName "Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c"
# Reset the content index
# Get the database server for the default content service
$dbServer = [Microsoft.SharePoint.Administration.SPWebService]::ContentService.DefaultDatabaseInstance.NormalizedDataSource
# Get the SQL Management Object server for the content service retrieved above
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | out-null
$SMOserver = New-Object ('Microsoft.SqlServer.Management.Smo.Server') -argumentlist $dbServer
# Get the search service application instance
$searchServiceApp = (Get-SPEnterpriseSearchServiceApplication)
# Iterate the crawl stores and for each database, shrink it in size by 20%
foreach($crawlStore in $searchServiceApp.CrawlStores)
	$dbName = $crawlStore.Name	
	$db = $SMOserver.Databases[$dbName]
	if ($db -ne $null)  
		$db.Shrink(20, [Microsoft.SqlServer.Management.Smo.ShrinkMethod]'TruncateOnly')
		Write-Host "$dbName has been shrunk"
		Write-Host "$dbName does not exist"

Comments in the script should make this one clear to understand, happy coding!

Import Tag MSBuild Errors

Creating a SharePoint package is easy enough with Visual Studio but each time you restart it, the package location used previously is replaced with the default location of the current user’s Documents folder.

To save a few seconds from my life I decided to write a PowerShell script to build and package the solution, meaning I no longer have to specify the publish location whenever Visual Studio is restarted.

I’m using Visual Studio 2013 and SharePoint 2013

& "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\msbuild.exe" "C:\Projects\TestProject\TestSolution.csproj" /t:rebuild;package
Write-Host "Copying TestSolution.wsp to Packages folder" -NoNewLine
Copy-Item -Confirm:$false "C:\Projects\TestProject\bin\Debug\TestSolution.wsp" C:\Packages\
Write-Host " - done."

The script shown has been simplified for this post and is easy enough to parametrise to allow you to specify different SharePoint projects to package.

Anyway, back to the main reason for this post.

After doing the above and running the script the following error was generated:

C:\Projects\TestProject\TestSolution.csproj(88,3): error MSB4019: The imported project “C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\SharePointTools\Microsoft.VisualStudio.SharePoint.targets” was not found. Confirm that the path in the declaration is correct, and that the file exists on disk.
Read more

Create Site Collection with Content Database

A PowerShell related post today.

If you need to create a new site collection and ensure it uses a specific content database, this little script will get that done for you. No messing about in Central Administration disabling content databases and all that good stuff, just a simple script that gets the job done, hassle free.

$hostIP = $ENV:ComputerName
$hostURL = "http://$hostIP/sites/Site Collection Name"
$user = [Security.Principal.WindowsIdentity]::GetCurrent().Name
$dbServer = "$hostIP"
$contentDbName = "Your_Content_Db_Name"
$webAppName = "SharePoint - 80"
$lcid = 1033
$templateName = "STS#0"
$siteName = "Site Collection Name"
function Add-Site {
	Write-Host ("Creating new ComplianceSP site '", $siteName, "' at ", $hostURL -join "") -NoNewline
	$newSite = New-SPSite $hostURL -OwnerAlias $user -name $siteName -Template $templateName -Language $lcid -ContentDatabase $contentDbName
	$newSite.RootWeb.CreateDefaultAssociatedGroups($newSite.Owner, $null, $null)
	Write-Host " - Done." -ForegroundColor Green
function Add-ContentDatabase {
	$contentDatabase = Get-SPContentDatabase -WebApplication $webAppName | Where { $_.Name -eq $contentDbName }
	if ($contentDatabase -eq $null) {
		Write-Host ("Creating new content database '", $contentDbName, "' in '", $dbServer, "' for web application '", $webAppName, "'" -join "") -NoNewline
		New-SPContentDatabase -Name $contentDbName -DatabaseServer $dbServer -WebApplication $webAppName -MaxSiteCount 1 -WarningSiteCount 0 -Confirm:$false
		Write-Host " - Done." -ForegroundColor Green
	else {
		Write-Host ("Content database '", $contentDbName, "' in '", $dbServer, "' associated with web application '", $webAppName, "' already exists." -join "") -NoNewline
try {
catch {
	Write-Host ""
	Write-Host "Error : " $Error[0] -ForegroundColor Red
	Exit 1

Lets walk through the code.
Read more

Get SearchServiceApplication in PowerShell

In C# when retrieving the default SearchServiceApplication object you might do something like the following, if you were trying to initiate a new Schema object, for example:

SPServiceContext serviceContext = SPServiceContext.GetContext(SPContext.Current.Site);
SearchServiceApplicationProxy searchApplicationProxy = serviceContext.GetDefaultProxy(typeof(SearchServiceApplicationProxy)) as SearchServiceApplicationProxy;
Guid applicationId = searchApplicationProxy.GetSearchServiceApplicationInfo().SearchServiceApplicationId;
SearchServiceApplication searchApplication = SearchService.Service.SearchApplications.GetValue<SearchServiceApplication>(applicationId);
Schema schema = new Schema(searchApplication);

If you’re looking to implement something similar in PowerShell, you don’t have to write quite as much code. Take a look at the following script, which achieves the same goal:

$searchApplication = Get-SPEnterpriseSearchServiceApplication "Search Service Application"
$schema = New-Object Microsoft.Office.Server.Search.Administration.Schema($searchApplication);

The call to Get-SPEnterpriseSearchServiceApplication is all that’s required to retrieve the default SearchServiceApplication object.



Get every new post delivered to your Inbox

Join other followers: