So it’s pretty straight forward to create new site collections with the PowerShell command.
We noticed strange behaviour though between sites created with script and those in the UI. Basically the default security groups had not been provisioned when the site collection was created using script.
Basically what’s happening is that if you create the site collection via the browser an additional method call gets made on the root web site of the site collection. For whatever reason this method call is not made when using PowerShell.
So to get it created the you need to invoke the following:
$siteCollection = $gc | New-SPSite -Url ($webAppConfig.Url + $siteCollectionConfig.Url) -ContentDatabase $db -Description $siteCollectionConfig.Description -Language $siteCollectionConfig.LCID-Name $siteCollectionConfig.Name -Template $siteCollectionConfig.Template -OwnerAlias $siteCollectionConfig.OwnerLogin -OwnerEmail $siteCollectionConfig.OwnerEmail
This will create the default groups.
if($siteCollection -ne $null)
{
Write-Host "Site collection $($siteCollectionConfig.Url) created" -foregroundcolor green
$primaryOwner = $siteCollectionConfig.OwnerLogin
$secondaryOwner = ""
Write-Host "Removing any existing visitors group for Site collection $($siteCollectionConfig.Url)" -foregroundcolor blue
#This is here to fix the situation where a visitors group has already been assigned
$siteCollection.RootWeb.AssociatedVisitorGroup = $null;
$siteCollection.RootWeb.Update();Write-Host "Creating Owners group for Site collection $($siteCollectionConfig.Url)" -foregroundcolor blue
$siteCollection.RootWeb.CreateDefaultAssociatedGroups($primaryOwner, $secondaryOwner, $siteCollection.Title)
$siteCollection.RootWeb.Update();
}
else
{
Write-Host "Site collection $($siteCollectionConfig.Url) failed" -foregroundcolor red
}
I received the error: Exception calling “CreateDefaultAssociatedGroups” with “3” argument(s): “”
Hi Ian,
Apologies with the long delay in replying.
So I’ve seen errors like the one you describe when either of the $primaryowner has been returned as null or the title is zero length. I would suggest trying to output the values to ensure they have values.
Cheers,
Wes
Check out:
$web=get-spweb “http://yoursitecollection”
$web.CreateDefaultAssociatedGroups($PrimaryLogin,$SecondaryLogin,””)
$web.Dispose()
credit to http://www.sharepointpromag.com/article/sharepoint/create-a-sharepoint-site-collection-with-windows-powershell-ui-style
Wes,
Glad I found this. I was wondering why my host header site collections didn’t have the Owners Groups and then I found this. It’s not the host headers, it’s the script!
I found that I got a similar error to Ian even when I was using two valid domain accounts. I switched to using the same account twice and that worked for some reason.
Also, I don’t think $siteCollection.Title is populated, at least not in my case. I used $siteCollection.RootWeb.Title instead.
Tom
Is there a way to enable this setting after site collection creation. I have created groups at site collection level but when I am creating new sites the groups are getting created. Can we enable group creation by default.