Archive for the ‘Powershell’ Category

Okay, before anyone comments on the simplicity of this posting. It merely t allow me to get the script easily in the future.  What’s the purpose of it? It basically retracts a solution and waits until its fully retracted, then deletes it, adds it in again and redeploys it. Not rocket science, but handy. 

#Set up Web Application variable
#Only needed if solution contains Web Application scoped resources

$webApp = "

#Set up solution name variable
$solutionName =  "solution.wsp"

#Set up solution file path variable
$filePath = "C:\Deploy51011\" + $solutionName

#Uninstall solution
#Add -WebApplication $webApp if solution contains Web Application scoped resources

Uninstall-SPSolution –Identity $solutionName –WebApplication $webApp  -Confirm:$false

#set up a variable for the solution to allow access to properties
$solution = Get-SPSolution $solutionName

#Wait for solution to be uninstalled
do {Start-Sleep -s 1} while ($solution.Deployed -eq $true)

# add another couple of seconds
Start-Sleep -s 2

#Remove solution from the farm without prompting
Remove-SPSolution $solutionName -Confirm:$false

# add it back in and deploy it
Add-SPSolution –LiteralPath $filePath
Install-SPSolution –Identity $solutionName –WebApplication $webApp –GACDeployment

Hope this saves someone some time Winking smile

After deploying several event receivers to a variety of lists I found that one of the event receivers did not seem to be working, but instead was firing a old version of the DLL.

So, to check that it was indeed attached, I ran the following PowerShell command.

$spWeb = Get-SPWeb –Identity <SharePointWebUrl> 
$spList = $spWeb.Lists["List Display Name"]  
$spList.EventReceivers | Select Name, Assembly, Type

After running this I discovered that one of the event receivers had been attached to the list twice! I’m not entirely sure why or how this happened, but my problem was resolved by deleting the duplicate receivers and reattaching it once.

The script is used to delete was:

$spWeb = Get-SPWeb –Identity <SharePointWebUrl> 
$spList = $spWeb.Lists["List Display Name"] 
$eventsCount = $spList.EventReceivers.Count
$assembly = "Company.SharePoint.Client.EventReceivers, Version=, Culture=neutral, PublicKeyToken=570c484d87a3aa61" 
$class = "Company.SharePoint.Client.EventReceivers.ProvisionServiceAreaSecurityGroups.ProvisionServiceAreaSecurityGroups" $name = "ProvisionServiceAreaSecurityGroupsItemUpdating" 
$type = 2 
for ($i = 0; $i -lt $eventsCount; $i+=1)
     if ($spList.EventReceivers[$i].Assembly -eq $assembly –and
         $spList.EventReceivers[$i].Class -eq $class –and
         $spList.EventReceivers[$i].Type -eq $type –and
         $spList.EventReceivers[$i].Name -eq $Name)


And to add it was:

$spWeb = Get-SPWeb –Identity <SharePointWebUrl> 
$spList = $spWeb.Lists["List Display Name"] 
$spEventReceiver = $spList.EventReceivers.Add() 
$spEventReceiver.Assembly = "Company.SharePoint.Client.EventReceivers, Version=, Culture=neutral, PublicKeyToken=570c484d87a3aa61" 
$spEventReceiver.Class = "Company.SharePoint.Client.EventReceivers.ProvisionServiceAreaSecurityGroups.ProvisionServiceAreaSecurityGroups" $spEventReceiver.Name = "ProvisionServiceAreaSecurityGroupsItemUpdating" 
$spEventReceiver.Type = 2 
$spEventReceiver.SequenceNumber = 1000 
$spEventReceiver.Synchronization = 0 


That’s it Winking smile

Today, while trying generate a model, using SPMetal, I found out that SPMetal does  not like spaces in the web URL. After trying the URL in quotes with the space and again with %20 in the string for the URL encoding seen below. Neither of them were able to find the application. 

SPMetal /web: ”http://sharepoint/services/Rota Management” /namespace: etc
SPMetal /web: ”http://sharepoint/services/Rota%20Management” /namespace: etc

Resulting in the following errors respectively

Error: Web at http://sharepoint/services/Rota Management could not be found.
Error: Web at http://sharepoint/services/Rota0Management could not be found.

So in order to get the model definition I used the following PowerShell command.

Start-SPAssignment –Global
$web = Get-SPWeb "http://sharepoint/services/Rota Management"
$web.ServerRelativeUrl = "/services/RotaManagement"
Stop-SPAssignment –Global

Then, I ran the SPMetal command again and it worked no problem.

After generating the class just reversed the above to restore the URL

Okay, so you try to search and your getting dreaded “Internal Server error exception” message seen below. This is not very helpful at all. However, the chances are, your search databases have somehow became corrupt


To resolve this, I carried out the following steps.

Note: All of the below assumes that you have called your Search Service Application the default name of Search Service Application

Firstly, you need to delete the SharePoint Search Service Application. You can try doing this from Central Admin, but I found that this just hangs and does nothing. So, in remedy, fire up a SharePoint PowerShell – remembering to run as Administrator (seen below)


Once you have PowerShell open you need to find out the ID of Application and delete it.

You can do this in two seperate commands:

Get-SPServiceApplication |?{$ -eq "Search Service Application"}

Remove-SPServiceApplication –Identity <ID of Above> –RemoveData

or you can assign it to a variable and do it as one command

$search = Get-SPServiceApplication |?{$ -eq "Search Service Application"}
Remove-SPServiceApplication -Identity $search.Id –RemoveData

You should now see the following confirmation.


Choose Y to delete it.

Now, just to check that all search references are gone, run the following command

Get-SPDatabase | Where-Object {$ -like "Search_Service_Application*"} | select id, Name

You should not have anything returned here, if you have run the command below.

ForEach($db in Get-SPDatabase |  Where-Object  {$ -like "Search_Service_Application*"})

Before going any further you may want to go into SQL Server Management Studio and delete the three orphaned databases created for the search:


However, I’ll leave that decision up to you.

After all of the above has been done and you are satisfied that all the references have gone you can now re-configure the Search Service Application as described in the MSDN documentation

Another resource that i have found very helpful is An A-Z Index of Windows PowerShell 2.0 commands