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

相關文章
相關標籤/搜索