Monday, August 18, 2014

List all SharePoint site collections and subsites exists in particular web application using powershell script

List all SharePoint site collections and subsites exists in particular web application using powershell script

## # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
# creates two texts file(allSites.txt, allWebs.txt) having list of all SPSites and SPWebs
#
# This file is simple txt file, line separated, of the URL's for each object.
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #  

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
Start-SPAssignment -Global
$siteLimit = 500
function GetAllSites([string]$webAppUrl)
{
    
    $allSites = Get-SPWebApplication $webAppUrl `
        | Get-SPSite -Limit $siteLimit | `
            ForEach-Object { $_.URL }
    $allSites
}
function GetAllWebs([string]$webAppUrl)
{
    $allWebs = Get-SPWebApplication $webAppUrl `
        | Get-SPSite -Limit $siteLimit | `
            foreach { $_.AllWebs | foreach {$_.URL } }
    $allWebs
}

$rootUrl = "Web Application URL"
"Sites"
$sites = GetAllSites $rootUrl
"Webs"
$webs = GetAllWebs $rootUrl

$sites | Out-File -FilePath "allSites.txt" -Force
$webs | Out-File -FilePath "allWebs.txt" -Force

Stop-SPAssignment -Global
====================================================================

Save above code as .ps1 and run on powershell console and check two text file generated on same location.

Create SharePoint site in bulk using powershell script

Create SharePoint site, subsites in bulk using powershell script and also enable required feature on it

Create below function and save it as Sharedfunction.ps1

function CreateWeb
(
$url,
$name
)
{

$web= New-SPWeb -url $url -name $name -template Templatecode -UseParentTopNav
Write-Host "$web created succesfully for site $url"

Enable-SPFeature -identity "GUID" -Url $url
Enable-SPFeature -identity "GUID" -Url $url
Enable-SPFeature -identity "GUID" -Url $url
Enable-SPFeature -identity "GUID" -Url $url

 Write-Host "Feature activated on $web succesfully"


}

Copy below code and save it as ps1

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

. ".\SharedFunctions.ps1"

Start-SPAssignment -Global

StartTranscript "Create Web"

try {

createWeb http://sitename/test/site1 "Sitename";
createWeb http://sitename/test/site2 "Sitename";

}
catch {
    write-host "Caught an exception:" -ForegroundColor Red
    write-host "Exception Type: $($_.Exception.GetType().FullName)" -ForegroundColor Red
    write-host "Exception Message: $($_.Exception.Message)" -ForegroundColor Red
    exit 1
}

finally {
    Stop-SPAssignment -Global
    StopTranscript

}

Enable Site collection feature using Powershell Script

Enable Site collection feature using Powershell Script

$site = Get-SPSite http://sitecollectionurl
Enable-SPFeature -Identity “Feature GUID” -Url $site.Url
$site.Dispose()

Enable feature on large no. of site collection, first create a text file in the below format, you only need to define the URL for each Site collection or site.

SiteURL
http://sharepoint/sites/site1
http://sharepoint/sites/site2
http://sharepoint/sites/site3
http://sharepoint/sites/site4

Then use the below code and save it ps1 file and excute as PowerShell comand:

$SiteList = Import-Csv Siteslist.txt

ForEach ($item in $SiteList)

{

            If ($item -ne $null)

            {

                Write-Host "Activated Feature for: $($item.SiteURL)" -foregroundcolor cyan;
             
                Enable-SPFeature -id "FeatureGUID"-URL "$($item.SiteURL)"

                Disable-SPFeature -id "FeatureGUID" -URL "$($item.SiteURL)"
 
                Write-Host "Deactivated Feature for: $($item.SiteURL)" -foregroundcolor green;                     
 
            }

}

$web.update() 
$web.Dispose()


Check sites to make sure features are activated properly.

Stop SharePoint Search crawls using Powershell command

Stop SharePoint Search crawls using Powershell command

$searchapp = Get-SPEnterpriseSearchServiceApplication "NameofSearchService application"

$contentsource = Get-SPEnterpriseSearchCrawlContentSource "NameofContentsource" -SearchApplication $searchapp

$contentsource.StopCrawl()

Check Central admin and make sure content source crawl status is in stopping state or in idle state.

Enable continuous crawls in SharePoint 2013 using powershell script

Enable continuous crawls in SharePoint 2013 using powershell script

Execute below command in powershell console for every 10 minutes

$searchServiceContentSource = "NameofContentsource"

$crawlInterval = 10

$ssa = Get-SPEnterpriseSearchServiceApplication

$cs1 = Get-SPEnterpriseSearchCrawlContentSource -SearchApplication $ssa -Identity $searchServiceContentSource

Set-SPEnterpriseSearchCrawlContentSource -Identity $cs1 -EnableContinuousCrawls $True

$ssa.SetProperty("ContinuousCrawlInterval", $crawlInterval)

$ssa.Update()

Note- If you have multiple search service app, mention name of search service app infront of $ssa.

Check Central administration and confirm if continuous crawls are enabled and set to run every 10 mins.

Friday, August 15, 2014

Remove list of Users from all site collections in farm using powershell script

Remove list of Users from all site collections in farm using powershell script

Copy below code and save it as removeusers.ps1 

=========================================================
param
(
[Parameter(Mandatory=$true)][ValidateScript({Test-Path $_ -Include "*.csv"})]
[String]$CSVPath
)
#This script will remove users specified in the CSV.
$CSVFile = Import-CSV $CSVPath
Add-PSSnapin Microsoft.SharePoint.PowerShell -EA SilentlyContinue
#Get all site collections
$Sites = Get-SPSite -Limit All
$AllSites = @()
foreach($Line in $CSVFile)
{
foreach($Site in $Sites)
{
#Get the rootweb for the site collection
        $RootWeb = $Site.RootWeb
If([bool](Get-SPUser $Line.Username -Web $RootWeb -EA SilentlyContinue) -eq $True)
{
#Remove the user from the User Information List
        Remove-SPUser -Identity $Line.username -Web $RootWeb -Confirm:$False
$AllSites += $RootWeb.Url
}
}
if(!($AllSites).count -eq 0)
{
#Give feedback on deleted users
Write-Host “Removed user $($Line.username) from:” -Fore “Magenta”
foreach($S in $AllSites){Write-Host “- $S”}
Write-Host “”
$AllSites = @()
}
}

================================================

Open notepad and add below list of users and save it as .csv extension file

=============
username
domain\user1
domain\user2
================

Run the script with below command:
C:>.\removeusers.ps1 users.csv



Approve items in Sharepoint list using powershell script

Approve items in SharePoint list using powershell script in one go

Copy below code in notepad and save it as ps1 and run it

$spSite = new-object Microsoft.SharePoint.SPSite("WebapplicationURL")

Write-Host "Updating SharePoint list"
$spWeb = $spSite.OpenWeb("Subsitename")
$spList = $spWeb.Lists["Listname"]
$spitems = $splist.items
foreach($item in $spitems) {
$item["_ModerationStatus"] = 0;
 $item.Update();
}

Write-Host "Completed...."

Open SharePoint Powershell command and run file as approve.ps1

c:>.\approve.ps1

Check your SharePoint list or library an

SharePoint 2013 Search index partition and Query processing component is in bad status and search is not working

SharePoint 2013 Search index partition and Query processing component is in bad status and search is not working





Cause
- This happens when search controller host service is manually started
- noderunner is taking more memory on server its crashing

Workaround:
- Reset index and do full crawl.
- wait until full crawl completes to see change in status of index partition and query processing component to Green.


Slow performance of SharePoint 2013 site and gets timeout with error on webparts: 

correlation ID: 00000000-0000-0000-0000-0000000000

Cause:
webparts settings are using xslt to manipulate search results and its view. So its taking time to load and shows timeout with above correlation id.

Steps to fix:
1. Login to Web front end servers
2. Check current timeout settings using PowerShell:
$myfarm = Get-SPFarm
$myfarm.XsltTransformTimeOut

3. Change its value to 5 seconds(reasonable as per page customization)
$myfarm.XsltTransformTimeOut = 5
$myfarm.Update()


Refresh site page and check if issue still exists.