Saturday, September 27, 2014

Update the Site Logo using PowerShell script

The following script updates the site logo url to the new url.

Script:
$sites=(get-spsite http://sharepointsite).AllWebs
$sites| foreach {
$var=$_.SiteLogoUrl;
if($var.Contains("http://testsite/"))
{
$var1=$var.Replace("http://testsite/", "http://newsite/");
$_.SiteLogoUrl = $var1;
$_.Update()
}
}

Retrieve the List Item Count using PowerShell script


The following script will get the SharePoint List Item count for each list under each site collection. This script generated the output as CSV file.

Script:

cls
$ListsInfo = @()

$spsite= Get-SPSite -Limit All
foreach ($spsiteitem in $spsite)
{

$TotalItems = 0

ForEach ($Site in $spsiteitem.AllWebs)
{
    ForEach ($List in $Site.Lists)
    {
        $ListInfo=  New-Object System.Object
        $ListInfo | Add-Member -MemberType NoteProperty -Name "Site_Url" -Value $Site.Url -Force
        $ListInfo | Add-Member -MemberType NoteProperty -Name "List_Name" -Value $List.Title  -Force
        $ListInfo | Add-Member -MemberType NoteProperty -Name "Item_Count" -Value $List.ItemCount -Force
        $ListsInfo += $ListInfo
    }

}

$ListsInfo | Export-Csv -NoTypeInformation -Path "C:\ListData.csv"

}

Get the SharePoint Designer workflows using PowerShell script

The following script will get the SharePoint Designer workflows.

Script:

$site = Get-SPSite("http://sharepointsite/");
$site.AllWebs | foreach { $_.Lists | foreach { $_.WorkflowAssociations | foreach { write-host "Site:" $_.ParentWeb.Url ", List:" $_.ParentList.Title ", Workflow:" $_.Name} } }

Create and Set the quota template for site collections after moving to the SP2013 using PowerShell script


The following script will create and set the quota template for site collections after moving to the SP 2013. This script will generate the xml file.

Script:

cls
if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {
     Add-PSSnapin Microsoft.SharePoint.PowerShell;
}


function Get-SPQuotaTemplates
{
    # Prepare XML structure
    $xmlText =
@"
<?xml version="1.0" encoding="UTF-8" ?>
<QuotaTemplates AsOn="">
    <QuotaTemplate SiteUrl="Sample" Name="Sample" Value="0" QuotaID="0" StorageMaximumLevel="0" InvitedUserMaximumLevel="0" StorageWarningLevel="0" UserCodeWarningLevel="0" UserCodeMaximumLevel="0" UpgradedPersistedProperties="0" />
</QuotaTemplates>
"@

 
    # Read the XML string into an object
    $xmlDoc = [xml]$xmlText

    # Read the sample element
    $sampleQuotaTemplate = @($xmlDoc.QuotaTemplates.QuotaTemplate)[0]
    $service = [Microsoft.SharePoint.Administration.SPWebService]::ContentService
    #Get all the SiteCollections
    $spsite= Get-SPSite -Limit All
    #Iterate through all the sitecollections
 
    foreach ($spsiteitem in $spsite)
    {
        $spsiteitem.Quota
        if($spsiteitem -ne $null)
        {   $flag=0
            #$service.QuotaTemplates
            foreach($serviceitem in $service.QuotaTemplates)
            {
             
                if($serviceitem.QuotaID -eq $spsiteitem.Quota.QuotaID)
                {
                    $quotaName=[string]($serviceitem.Name)
                    $tempQuotaTemplate=$serviceitem
                    $flag=1
                 
                }
             

            }
            if($flag -eq 0)
            {            
                    $quotaName="Individual Quota"
                    $tempQuotaTemplate=$spsiteitem.Quota
            }

       
                  $newQuotaTemplate = $sampleQuotaTemplate.Clone()
     
       #$newQuotaTemplate.Name = [string]($SPSite.Quota.QuotaID)
                  $newQuotaTemplate.SiteUrl=[string]$spsiteitem
         $newQuotaTemplate.Name =$quotaName.ToString()
       $newQuotaTemplate.Value = [string]$tempQuotaTemplate.Value
                  $newQuotaTemplate.QuotaID = [string]$tempQuotaTemplate.QuotaID
       $newQuotaTemplate.StorageMaximumLevel = [string](($tempQuotaTemplate.StorageMaximumLevel) / 1024 / 1024)
       $newQuotaTemplate.StorageWarningLevel = [string](($tempQuotaTemplate.StorageWarningLevel) /1024 / 1024)
       $newQuotaTemplate.UserCodeMaximumLevel = [string]$tempQuotaTemplate.UserCodeMaximumLevel
       $newQuotaTemplate.UserCodeWarningLevel = [string]$tempQuotaTemplate.UserCodeWarningLevel
                  $newQuotaTemplate.UpgradedPersistedProperties =[string]$tempQuotaTemplate.UpgradedPersistedProperties
                  $newQuotaTemplate.InvitedUserMaximumLevel=[string]$tempQuotaTemplate.InvitedUserMaximumLevel


        # Add the element to the QuotaTemplates node as a new child
        $xmlDoc.QuotaTemplates.AppendChild($newQuotaTemplate)
        }
    }

    # Remove Sample node
     $sample = $xmlDoc.QuotaTemplates.QuotaTemplate | Where-Object { $_.Name -eq "Sample" }
     $xmlDoc.QuotaTemplates.RemoveChild($sample)

    # Set date and time when generated
    $now = Get-Date
    $xmlDoc.QuotaTemplates.AsOn = [string]$now

    $xmlDoc.Save("C:\QTforSiteCol.xml")
    # Report completion
    Write-Host -f Green "The quota template information has been saved to C drive - QTforSiteCol.xml."
}

Get-SPQuotaTemplates

Update Managed Paths using PowerShell script

The following script will create the Managed Paths for the web application. This script needs the Input as CSV file. And CSV file should contains the Type,Name,WebApp as columns.
Type should contain for example - WildcardInclusion, ExplicitInclusion

Script:

$Name = @()
$Type = @()
$WebApp = @()

Import-Csv "D:\SPManagedPath.csv" |
    ForEach-Object {
        $Name += $_.Name
        $Type += $_.Type
                $WebApp += $_.WebApp
$inputNumber = Read-Host -Prompt "Please Enter to continue"  
    $Where = [array]::IndexOf($Type, $inputNumber)
    Write-Host "Name1: " $Name[$Where]
    Write-Host "Type2: " $Type[$Where]
    Write-Host "WebApp3: " $WebApp[$Where]
    New-SPManagedPath -RelativeURL $Name[$Where] -Explicit -WebApplication $WebApp[$Where]
    }

Update Primary and Secondary Site Collection Administrators using PowerShell script

The following script will update the Primary and Secondary Site collection administrators using powershell script. This script needs the Input as CSV file. And CSV file should contains the Sitecollectionurl,PrimaryAdmin,SecAdmin as columns.

Script:

#Get Site collection URL as a parameter  #param(  #[string] $filePath = $(Throw "Missing Input - CSV file path required with filename. Ex: C:\Solutions\SiteColAdmins.csv"), #required parameter  #[string] $LogFilePath = $(Throw "Missing Input - Path to create log file. Path al Ex: Ex: C:\Solutions\") #required parameter  #)  #Load SharePoint dll file  [void] [System.Reflection.Assembly]::Load(”Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”)
#Load SharePoint Snap In
cls
if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {
     Add-PSSnapin Microsoft.SharePoint.PowerShell;
}
$filePath = "C:\Solutions\SiteColAdmins.csv"
$csv_info = Import-Csv $filePath
#Logfile Name and Path
$LogFilePath = "C:\Solutions\"
$date = (get-date).ToString('yyyyMMdd')
$FileName = "UpdatePrimarySecondaryAdmin"
$FileName = $LogFilePath += $FileName += $date
#Creating Log file
if(!(Test-Path -Path "$FileName.txt"))
{
$file = New-Item -type file "$FileName.txt"
}
Write-Host "--Begin Process--"
Add-Content -Path "$FileName.txt" -Value "Begin Process"
#iterate Csv File
foreach ($line in $csv_info)
{
Write-Host "--starting update for site: " $line.Sitecollectionurl " PrimaryOwner: " $line.PrimaryAdmin " & SecondaryOwner: " $line.SecAdmin
Add-Content -Path "$FileName.txt" -Value $( "--starting update for site: " + $line.Sitecollectionurl + " PrimaryOwner: " + $line.PrimaryAdmin + " and SecondaryOwner: " + $line.SecAdmin)
#Get site collection
$urltest=$line.Sitecollectionurl
$sadmin=$line.SecAdmin
$padmin=$line.PrimaryAdmin
if($urltest -ne $null)
{
if($sadmin -ne $null -or $sadmin -ne " ")
{
    if($padmin -ne $null -or $padmin -ne "")
    {
Set-SPSite -Identity $urltest -SecondaryOwnerAlias $sadmin -OwnerAlias $padmin
Write-Host "Primary owner :" $padmin "is successfully added to " $urltest
Add-Content -Path "$FileName.txt" -Value $("Primary owner: " + $padmin + "is successfully added to " + $urltest)
Write-Host "Secondary owner :" $sadmin "is successfully added to " $urltest
Add-Content -Path "$FileName.txt" -Value $("Secondary owner: " + $sadmin + "is successfully added to " + $urltest)
    }
else
{
Write-Host "Secondary owner :" $sadmin "is Empty and not added to " $urltest
Add-Content -Path "$FileName.txt" -Value $("Secondary owner: " + $sadmin + "is not added to " + $urltest)
}


}
else
{
Write-Host "Primary owner :" $padmin "is Empty and not added to " $urltest
Add-Content -Path "$FileName.txt" -Value $("Primary owner: " + $padmin + "is not added to" + $urltest)
}
}
}
Add-Content -Path "$FileName.txt" -Value "End Process"
Write-Host "--End Process--"

Get the Primary and Secondary Site collection Administrator using PowerShell script

The following script will generate the text file which will give the list of Primary & Secondary Site collection Administrators for each web application.

Script:

Add-pssnapin microsoft.sharepoint.powershell -EA 0
$outputFile = "C:\\SCAdminsReport.txt"
Write-Host "This script will loop through all site collections in a web application and list the collection's URL, Primary Owner, and Secondary Owner"
$webapp = "http://sharepointsite/"
$Web = Get-SPWebApplication $webapp
$text = ""
$text | out-file $outputFile
foreach ($sites in $Web.sites) {
    $text = "-------------------------------------------"
    $text | out-file $outputFile -append
        $text = "Site: " + $sites.url
        $text | out-file $outputFile -append
    $text = "Primary Owner: " + $sites.owner
        $text | out-file "$outputfile" -append
    $text = "Secondary Owner: " + $sites.secondarycontact
        $text | out-file "$outputfile" -append
    $text = "-------------------------------------------"
        $text | out-file "$outputfile" -append
    }
Write-Host "-----END------"