Saturday, October 20, 2012

Create Site Collection Using Powershell

Add-PsSnapin Microsoft.SharePoint.Powershell –ErrorAction SilentlyContinue


#Get-SPWebApplication


     # Remove-SPWebApplication http://globas:1050 -Confirm -DeleteIISSite -RemoveContentDatabases

#Write-Host "Finished! Press enter key to exit." -ForegroundColor Green
#Read-Host


function Start-SiteCollectionCreate(
    [string]$settingsFile = "sites.xml") {
    [xml]$config = Get-Content $settingsFile
   
    $config.SiteCollections.SiteCollection | ForEach-Object {

 

#Create WebApplication
Write-Host  
      Write-Host  
      Write-Host "WebApplication Creating........"  
      Write-Host "******************************************"
New-SPWebApplication -Name $_.Name -Port $_.Port -AllowAnonymousAccess  -URL $_.SiteCreationUrl -ApplicationPool $_.Pool -ApplicationPoolAccount (Get-SPManagedAccount $_.OwnerLogin)

Write-Host  
      Write-Host  
      Write-Host "WebApplication Sucussfully Created........" 

 

 


Write-Host  
      Write-Host  
      Write-Host "Site collection Creating........"  
      Write-Host "******************************************"


        #Creating site collection
        Write-Host "Creating site collection $($_.Url)..."
        $gc = Start-SPAssignment
        $site = $gc | New-SPSite `
            -Url $_.Url `
            -Description $_.Description `
            -Language $_.LCID `
            -Name $_.Name `
            -Template $_.Template `
            -OwnerAlias $_.OwnerLogin `
            -OwnerEmail $_.OwnerEmail `
            -SecondaryOwnerAlias $_.SecondaryLogin `
            -SecondaryEmail $_.SecondaryEmail
            Stop-SPAssignment -SemiGlobal $gc
        # Associate Default Groups (Dan Holme: http://www.sharepointpromag.com/article/sharepoint/Create-a-SharePoint-Site-Collection-with-Windows-PowerShell-UI-Style)
        $MembersGroup = "$_.Name Members"
        $ViewersGroup = "Viewers"
        $web = Get-SPWeb $_.url
        $web.CreateDefaultAssociatedGroups($_.OwnerLogin,$_.SecondaryLogin,"")
        $PrimaryAdmin = Get-SPUser $_.OwnerLogin -Web $_.url
        $PrimaryAdmin.Name = $_.OwnerDisplay
        $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()
        }
    }

# Execute the script           
Start-SiteCollectionCreate


Write-Host  
      Write-Host  
      Write-Host "Site Collection Created........"  


Write-Host "Finished! Press enter key to exit." -ForegroundColor Green
Read-Host

 


 

Xml File

 

<SiteCollections>                   
    <SiteCollection Name="globas-1020"
                    Description=""
                    SiteCreationUrl="http://globas:1020/"
                    Port="1020"
                    Pool="globas1020"
                    Url="http://globas:1020"
                    LCID="1033"
                    Template="STS#0"
                    OwnerLogin="glob\administrator"
                    OwnerEmail="administrator@sgs.com"
                    OwnerDisplay="Administrator"
                    SecondaryLogin="glob\administrator"
                    SecondaryEmail="admin@cdfdontoso.com"
                    SecondaryDisplay="SharePoint Farm">
    </SiteCollection>

 

</SiteCollections>

No comments:

Post a Comment