SharePoint自动化系列——经过PowerShell建立SharePoint Site Collection

经过PowerShell建立SharePoint Site Collection,代码以下:web

Add-PSSnapin microsoft.sharepoint.powershell
function CreateTeamSite()
{
    $webApps = Get-SPWebApplication
    $webAppsUrl = $webApps.Url
    if($webApps.count -eq 1)
    {
        Write-Host "You have only one web application:"
        Write-Host $webApps.Url
        $choice = Read-Host "Do you want to create a site collection under this web application? Type 'y' to create.'"
        if($choice -eq "y")
        {
            $siteTitle = Read-Host "Enter the site collection's title:"
            $siteUrl = $webAppsUrl+"sites/"+$siteTitle
            Write-Host "Choose the site collecion's template:"
            Write-Host "[1].Template1."
            Write-Host "[2].Template2."
            Write-Host "[3].Template3."
            $choice = Read-Host "Enter the number to choose"
            SwitchSiteTemplateAndCreateSite $choice $siteUrl 
     $choice = Read-Host "Type 'y' to continue"
            if($choice -eq 'y')
            {
                CreateTeamSite
            }
        }
    }
    else
    {
        Write-Host "Choose the web application:"
        for($i=0;$i -le $webApps.count-1;$i++)
        {
            $tip = "[" + $i + "]." + $webApps[$i].Url
            Write-Host $tip
        }
        $choice = Read-Host "Enter the number to choose"
        $siteTitle = Read-Host "Enter the site collection's title"
        $siteUrl = $webApps[$choice].Url + "sites/"+$siteTitle
        Write-Host "Choose the site collecion's template:"
        Write-Host "[1].Template1."
        Write-Host "[2].Template2."
        Write-Host "[3].Template3."
        $choice = Read-Host "Enter the number to choose"
        SwitchSiteTemplateAndCreateSite $choice $siteUrl
        $choice = Read-Host "Type 'y' to continue"
        if($choice -eq 'y')
        {
            CreateTeamSite
        }
    }
}
function SwitchSiteTemplateAndCreateSite($choice,$siteUrl)
{
    switch($choice)
    {
        1 {$template = "Template1ID"}
        2 {$template = "Template2ID"}
        3 {$template = "Template3ID"}
    }
    if(($template -ne "Template1ID") -and ($template -ne "Template2ID") -and ($template -ne "Template3ID"))
    {
        $choice = Read-Host "Please enter the correct number."
        SwitchSiteTemplateAndCreateSite $choice $siteUrl
    }else
    {
        Write-Host "Site collection creating..."
        New-SPSite -Url $siteUrl -OwnerAlias "Administrator" -Language 1033 -Template $template
        Write-Host "Site collection created successfully."
    }
}
CreateTeamSite

文中Template和TemplateID可替换(其余部分也能够进行替换,看我的需求。),switch语句中能够扩展模板ID(相应在提示填写模板名处也要作相应的添加)。平常工做中,选择几个经常使用的模板ID和模板名填进去便可,UserName填写你经常使用的那个就能够,最好是全部环境都通用的user(这样就不会由于换了域就找不到用户了),不用填写域,由于SharePoint支持模糊搜索,会自动将你输入的名字进行匹配,好比你输入Tylan,若是在SharePoint中存在该用户的话,SharePoint在check用户名时会自动在其前面加上Domain,像这样:“Domain\Tylan”。shell

其实写成脚本就是为了方便平常工做,节省时间,具体要把哪些地方设成变量哪些地方进行硬编码能够根据工做须要而变,仍然是以提升工做效率为目的。若是是长久的项目,为了推广使用,作个窗体工具也何尝不可,关键是看有没有这个必要。api