您的位置:首页 > 运维架构 > Shell

SharePoint自动化系列——通过PowerShell创建SharePoint Site Collection

2015-09-17 18:06 621 查看
通过PowerShell创建SharePoint Site Collection,代码如下:

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”。

其实写成脚本就是为了方便日常工作,节省时间,具体要把哪些地方设成变量哪些地方进行硬编码可以根据工作需要而变,仍然是以提高工作效率为目的。如果是长久的项目,为了推广使用,做个窗体工具也未尝不可,关键是看有没有这个必要。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: