Install wsp files placed in the same directory with PowerShell script

If you are required to deploy a big number of wsp files, the best way is to put them in the same folder with PowerShell script and make the script install them one by one. This way you won’t need to spend time to execute Add-SPSolution and Install-SPSolution for each one.

Add-PSSnapin Microsoft.SharePoint.Powershell;

# Input the web application name
$webApplication = Read-Host "Please enter the web application name";
# Calculate the script directory
$scriptDirectory = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition;
# Get the wsp files placed in script directory
Write-Host "Retreiving *.wsp files";
$wspFiles= Get-ChildItem $scriptDirectory *.wsp|where-object {!($_.psiscontainer)};

# Iterate through all wsp files and add and install them
foreach($wspFile in $wspFiles) {
   
   $solutionPath = $wspFile.FullName;
   $solutionName = $wspFile.name;

   # Add solution
   Write-Host "Adding solution " + $solutionName;
   Add-SPSolution $solutionPath;
   Write-Host  $solutionName + " was added to succesfully"; 
   
   # Install solution
   Write-Host "Installing solution " + $solutionName;
   Install-SPSolution -Identity $solutionName -WebApplication $webApplication -GacDeployment;
   Write-Host $solutionName + " was installed succesfully";     
    
}
Advertisements

Create search scope for searching inside a site

Creating search scope for a site in PowerShell is basically a two steps operation. First step consists in creating a search scope inside a search application, while second step consists in creating a search rule and making this rule to search inside a site. There is nothing complicated in this, as you will see in description below.

First thing you need to do is to make PowerShell prompt for the folder (URL) where site is located.

Write-Host "Enter Site URL:"
$url = Read-Host

Once I created a variable $url with the value assigned dynamically by the user, I can proceed to create the search scope.

$searchapp = Get-SPEnterpriseSearchServiceApplication
$src = Get-SPEnterpriseSearchQueryScope  -Identity "Search scope name" -SearchApplication $searchapp
if($src -ne $null) {
	Remove-SPEnterpriseSearchQueryScope -Identity  "Search scope name"  -SearchApplication $searchapp
}
$src = New-SPEnterpriseSearchQueryScope -Name "Search scope name" -SearchApplication $searchapp -Description "Search scope name" -DisplayInAdminUI $true

Scope name of course is going to be changed to fit your needs. Or you can assign the scope name dynamically by applying the same technique as I applied for site URL.

The last step is to add search rule for making new search scope pointing to site URL.

$rule = New-SPEnterpriseSearchQueryScopeRule -Scope $src -RuleType "Url" -Url $url -FilterBehavior "Require" -MatchingString $url -UrlScopeRuleType "Folder"

I hope everything is clear. For a complete view, see full code below.

Write-Host "Enter Site URL:"
$url = Read-Host

$searchapp = Get-SPEnterpriseSearchServiceApplication
$src = Get-SPEnterpriseSearchQueryScope  -Identity "Search scope name" -SearchApplication $searchapp
if($src -ne $null) {
	Remove-SPEnterpriseSearchQueryScope -Identity  "Search scope name"  -SearchApplication $searchapp
}
$src = New-SPEnterpriseSearchQueryScope -Name "Search scope name" -SearchApplication $searchapp -Description "Search scope name" -DisplayInAdminUI $true

$rule = New-SPEnterpriseSearchQueryScopeRule -Scope $src -RuleType "Url" -Url $url -FilterBehavior "Require" -MatchingString $url -UrlScopeRuleType "Folder"