2

I want to create PS script to create sitecollection and add the primary and secondary sitecollection administrator. However i need to find out if the user account user that used to assign in SiteCollection primary and secondary administrator not exist, how i can handle this by using the default username. Such as contoso\sp_farm. Thank you

The first section contains the variables you need to specify based on your needs

$url = "http://teams.contoso.com/depts/IT"
 $ContentDatabase = "SharePoint_Content_Teams"

     # OK… the content database is not something you are able to specify

     # when creating a site collection in the user interface, but it’s so

     # important and so many people wish it were there, that I’m adding it

    # to this script
 $WebsiteName = "Information Technology"
 $WebsiteDesc = "Information Technology department team site"
 $Template = "STS#0"

     # If you don’t know the names of templates,

     # enter the command GET-SPWebTemplate

 # Information about the primary site collection administrator (Owner)

 # Note that the information should match what is in Active Directory for

 # the username, display name, and email address
 $PrimaryLogin = "CONTOSO\LolaJ"
 $PrimaryDisplay = "Jacobsen, Lola"
 $PrimaryEmail = "Lola.Jacobsen@contoso.com"

 # Information about the secondary site collection administrator (Secondary Owner)
 $SecondaryLogin = "CONTOSO\AprilM"
 $SecondaryDisplay = "Meyer, April"
 $SecondaryEmail = "April.Meyer@contoso.com"



 # Names of the default Members and Viewers groups

 # You shouldn’t have to change these unless you’re using a

 # default language other than English
 $MembersGroup = "$WebsiteName Members"
 $ViewersGroup = "Viewers"



 # Create a site collection and top level website

 # The primary and secondary site collection administrators are specified

 # In Central Administration, their e-mail addresses are looked up, but

 # in PowerShell, you have to specify them or else they end up blank
 New-SPSite -Url $url –ContentDatabase $ContentDatabase -Name $WebsiteName –Description $WebsiteDesc  -Template $Template -OwnerAlias $PrimaryLogin –OwnerEmail $PrimaryEmail -SecondaryOwnerAlias $SecondaryLogin -SecondaryEmail $SecondaryEmail



 # In the user interface, after creating a site collection, the default groups are configured

 # automatically. This is not true of the New-SPSite cmdlet, so we have to create the

 # default groups (Visitor, Members, and Owners)

 $web = Get-SPWeb $url
 $web.CreateDefaultAssociatedGroups($PrimaryLogin,$SecondaryLogin,"")



 # In the user interface, the primary and secondary site collection administrators

 # are displayed with their friendly display names as looked up in Active Directory,

 # but with PowerShell the users are added to the site collection with their

 # display name set to their user name. These lines of code update the display names.

 $PrimaryAdmin = Get-SPUser $PrimaryLogin -Web $url
 $PrimaryAdmin.Name = $PrimaryDisplay
 $PrimaryAdmin.Update()
 $SecondaryAdmin = Get-SPUser $SecondaryLogin -Web $url
 $SecondaryAdmin.Name = $SecondaryDisplay
 $SecondaryAdmin.Update()

 # Finish by disposing of the SPWeb object to be a good PowerShell citizen
 $web.Dispose()

1 Answer 1

1

How about calling EnsureUser to see if the user exists before you call New-SPSite?

$user = try{ $web.EnsureUser($PrimaryLogin) }catch{}
if ($user) {
    ...
}

Although I don't know what you would do if the user does not exists.

3
  • This will add the user to a web that he / she might not have permissions to. But if it is done on the current web, on the current user I guess it is valid Mar 31, 2014 at 6:04
  • Well, the $web variable in OPs gist does not yet exist, so what SPWeb to use from my snippet is up to him/her :)
    – eirikb
    Mar 31, 2014 at 6:09
  • Off course :) Just wanted to get OP a heads up about possibly unwanted implications Mar 31, 2014 at 6:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.