Showing posts with label Update Site Collection Administrator. Show all posts
Showing posts with label Update Site Collection Administrator. Show all posts

Saturday, September 27, 2014

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--"