set primary and secondary Site Collection Administrator through PowerShell


This script reads the CSV in the below format
Url,Owner,SecondaryOwner

and update the primary and secondary admins


#Get Site collection URL as a parameter
 #param(
 #[string] $filePath = $(Throw "Missing Input - CSV file path required with filename. Ex: E:\input\Archivingsites.csv"), #required parameter
 #[string] $LogFilePath = $(Throw "Missing Input - Path to create log file. Path al Ex: E:\input\") #required parameter
 #)

#Load SharePoint dll file
 [void] [System.Reflection.Assembly]::Load(”Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”)

$filePath = "E:\Scripts\UpdateOwnership\UpdateOwnership.csv"
 $csv_info = Import-Csv $filePath

#Logfile Name and Path
 $LogFilePath = "E:\Scripts\UpdateOwnership\"
 $date = ( get-date ).ToString('yyyyMMdd')
 $FileName = "UpdateOwnershipSitesLog_"
 $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.Url " PrimaryOwner: " $line.Owner " & SecondaryOwner: " $line.SecondaryOwner
 Add-Content -Path "$FileName.txt" -Value $( "--starting update for site: " + $line.Url + " PrimaryOwner: " + $line.Owner + " and SecondaryOwner: " + $line.SecondaryOwner)
 #Get site collection
 if($line.Url)
 {
 $site = new-object Microsoft.SharePoint.SPSite($line.Url.Trim())
 $web = $site.RootWeb
 $users = $web.SiteUsers
 if($line.Owner)
 {
 $PrimaryOwner = $web.SiteUsers[$line.Owner.Trim()]

if(!$PrimaryOwner)
 {
 Write-Host "Please add primary owner to " $site
 Add-Content -Path "$FileName.txt" -Value $("Please add primary owner to " + $site)
 }
 else
 {
 $site.set_AllowUnsafeUpdates(1)
 $site.Owner = $PrimaryOwner
 $site.set_AllowUnsafeUpdates(0)
 Write-Host "Primary owner :" $PrimaryOwner "is successfully added to " $site
 Add-Content -Path "$FileName.txt" -Value $("Primary owner: " + $PrimaryOwner + "is successfully added to " + $site)
 }

}
 if($line.SecondaryOwner)
 {
 $SecondaryOwner = $web.SiteUsers[$line.SecondaryOwner.Trim()]
 if(!$SecondaryOwner)
 {
 Write-Host "Please add secondary owner to " $site
 Add-Content -Path "$FileName.txt" -Value $("Please add secondary owner to " + $site)
 }
 else
 {
 $site.set_AllowUnsafeUpdates(1)
 $site.SecondaryContact = $SecondaryOwner
 $site.set_AllowUnsafeUpdates(0)
 Write-Host "Secondary owner :" $SecondaryOwner "is successfully added to " $site
 Add-Content -Path "$FileName.txt" -Value $("Secondary owner: " + $SecondaryOwner + "is successfully added to " + $site )

}
 }
 $site.Dispose()
 }
 }

Add-Content -Path "$FileName.txt" -Value "End Process"
 Write-Host "--End Process--"

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s