Wednesday, October 19, 2011

Script to change Site Collection Administrators in SharePoint 2007 with PowerShell

Where I work, we receive several requests for site collection admin change per week, so that's something that always bothered me, since it's not a quick task to be done when you take care of a huge number of sites. That's why I managed to create this script today, it will help me (and my team) to save some time to do other more important (and more interesting) things. Here we go:

First of all, you need to create a ps file named Set-SiteCollectionAdmin.ps1 and paste the code below. You must have another file to put the sites which will have their SCA changed, let's name this one as Site-CollectionAdmin-List.txt.

Here's how the file should be formatted:

 http://mysite1,user1@company.com,user2@company.com  
 http://mysite2,,user3@company.com  
 http://mysite1,user4@company.com,  

Note that on the first line, we are changing both the primary and the secondary SCAs, on the second one we are changing only the secondary SCA and on the third one only the primary.

Below is the source code, commented.

 # Declaring SharePoint assembly so PowerShell can use the SP object model  
 # Read more about Developing a PowerShell Script for SharePoint 2007  
 [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")  
   
 # Getting the info from the file Set-SiteCollectionAdmin-List.txt  
 # Read also about Batch process in PowerShell - Get-Content and Import-CSV cmdlets  
 get-content Set-SiteCollectionAdmin-List.txt | foreach {  
      # Splitting the row by the commas and assigning each part to one variable  
      $row = $_.split(",")  
      # Site URL  
      $url = $row[0].Trim()  
      # Primary SCA email/username  
      $prim = $row[1].Trim()  
      # Secondary SCA email/username  
      $sec = $row[2].Trim()  
        
      Write-Host ""  
   
      # Creating an URI variable to be used on the next method  
      $uri = New-Object Uri($url)  
        
      # Checking whether the mentioned site exists  
      if([Microsoft.SharePoint.SPSite]::Exists($uri))  
      {  
           # Instantiating the site collection  
           $site = New-Object Microsoft.SharePoint.SPSite($url)  
           # Opening the top level web  
           $web = $site.OpenWeb()  
        
           Write-Host $site.URL -foregroundcolor Green  
             
           # Checking whether the primary SCA will be changed  
           if($prim -ne "")  
           {  
                # Checking whether the user already exists in the site.  
                # EnsureUser() will add the user to the site collection if he doesn't exist yet.  
                $primAdm = $web.EnsureUser($prim)  
                # Changing the site owner (primary SCA) to the user.  
                $site.Owner = $primAdm  
                Write-Host "New SCA: $prim"  
           }  
             
           # Same process of the primary SCA  
           if($sec -ne "")  
           {  
                # Checking whether the user already exists in the site.  
                # EnsureUser() will add the user to the site collection if he doesn't exist yet.  
                $secAdm = $web.EnsureUser($sec)  
                # Changing the site secondary contact (secondary SCA) to the user.  
                $site.SecondaryContact = $secAdm  
                Write-Host "New Sec SCA: $sec"  
           }  
      }  
      else # if the site doesn't exist, just jump to the next row  
      {  
           Write-Host "Site $url not found. Jumping to next row." -ForegroundColor Red  
      }  
        
      # Disposing web and site objects from memory  
      $web.Dispose()  
      $site.Dispose()  
 }  

Important: I could only make this work by running powershell as the farm account. Any suggestions of how to do it without needing the would be great.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...