Adding Site Collection admins should be a part of every Site Collection creation Powershell script. As you might know, you can't add Active Directory groups as Site Collection admins in Central Administration. It only takes two names, and they have to be people.
This, among other reasons, is why creating Site Collections via Powershell is so much easier. The script below will allow you to add as many alternate Site Collection admins as you want. I typically just run this script after the Site Collection has been created. However, you could incorporate it into an overall Site Collection creation script. Basically I'm lazy, and this works. Maybe one day I'll put it in the main creation script.
This script, like most of the ones I use, is a combination of a few scripts I've found on the internet. I think it's important to note on most of the scripts I use that I usually change them, sometimes improve them, but rarely write the entire thing from scratch.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$logfile = "Powershelloutput.log"
# $AccountList is an array of Windows Identities in the format of $AccountList = @("DOMAIN\USERID" , "DOMAIN\USERID2")
$AccountList = @("AD\someuser", "AD\someotheruser", "AD\sharepointadministrators")
# $iisSiteList is an array of top level IIS site URLs
$iisSiteList = @("http://some.sharepointsite.com")
# this is from an earlier version of the script
# That scrip looks at all SP sites on the farm, I've left the old code here for reference
# this gets an array of objects representing the sites at the IIS level:
## $IISSites = Get-SPWebApplication
Foreach($oneIISSite in $IISSiteList)
{
foreach ($SharepointSiteCollection in (Get-SPWebApplication $oneIISSite).Sites)
{
$msg = $SharepointSiteCollection.url
write-host -ForegroundColor Cyan $msg
Add-Content $logfile $msg
$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
$msg = "Looking to see if User " + $account + " is a member on " + $SharepointSiteCollection.url
Write-host -foregroundColor Blue $msg
Add-Content $logfile $msg
$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
$msg = "Added user $Account to URL $SPWeb.URL"
Write-host -Foregroundcolor Magenta $msg
Add-Content $logfile $msg
}
else
{
$msg = "user $Account was already in URL " + $SPWeb.URL
Write-host -ForegroundColor DarkGreen $msg
Add-Content $logfile $msg
}
if ($user.IsSiteAdmin -ne $true)
{
$user.IsSiteAdmin = $true
$user.Update()
$msg = "$account has been made an admin on $SPWeb.URL"
Write-host -Foregroundcolor Magenta $msg
Add-Content $logfile $msg
}
else
{
$msg = "$account was already an admin on $SPWeb.URL"
Write-host -ForegroundColor DarkGreen $msg
Add-Content $logfile $msg
}
}
$SharepointSiteCollection.Dispose()
}
}
$msg = "=============== ALL DONE ================"
Write-host -ForegroundColor DarkGreen $msg
Add-Content $logfile $
No comments:
Post a Comment