Thursday 19 April 2012

Set Site Collection Owner For All Sites...

# set site collection owner for all sites...
# 1-2012 Jack
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
# $AccountList is an array of Windows Identities in the format of $AccountList = @("DOMAIN\USERID" , "DOMAIN\USERID2")
$AccountList = @("LAB\Jack", "Lab\tom", "Lab\dick", "lab\harry")
# $iisSiteList is an array of top level IIS site URLs
#this gets an array of objects representing the sites at the IIS level:
## no longer needed.... $IISSites = Get-SPWebApplication
Foreach($oneIISSite in $IISSiteList)
{
  #using .Sites, we can get a list of the site collections
  #so really what were saying is for each SharepointSiteCollection
  #this code is altered a bit, since we're using an array of top level site names.
  # we need to use (Get-SPWebApplication $oneIISSite).Sites
  # which is the same as $sitelist = Get-SPWebApplication $oneIISSite
  #                      $sitelist.sites
  foreach ($SharepointSiteCollection in (Get-SPWebApplication $oneIISSite).Sites)
  {
       write-host $SharepointSiteCollection.url -ForegroundColor Cyan
       $spweb = Get-SPWeb $SharepointSiteCollection.url
       #now we have the website, so lets look at each account in our array
       foreach ($Account in $AccountList)
       {
           #lets see if the user already exists
           Write-host "Looking to see if User " $account " is a member on " $SharepointSiteCollection.url -foregroundcolor Blue
           $user = Get-SPUSER -identity $Account -web $SharepointSiteCollection.url -ErrorAction SilentlyContinue #This will throw an error if the user does not exist
           if ($user -eq $null)
           { #if the user did NOT exist, then we will add them here.
               $SPWeb.ALLUsers.ADD($Account, "", "", "Added by AdminScript")
               $user = Get-SPUSER -identity $Account -web $SharepointSiteCollection.url
                Write-host "Added user $Account to URL $SPWeb.URL" -Foregroundcolor Magenta
           }
            else
           {
                Write-host -ForegroundColor DarkGreen "user $Account was already in URL " $SPWeb.URL
           }
           if ($user.IsSiteAdmin -ne $true)
           {
             $user.IsSiteAdmin = $true
             $user.Update()
             Write-host "$account has been made an admin on $SPWeb.URL" -Foregroundcolor Magenta
           }
           else
           {
             Write-host -ForegroundColor DarkGreen "$account was already an admin on $SPWeb.URL"
           }
       }
     $SharepointSiteCollection.Dispose()
  }
 }

No comments:

Post a Comment