SharePoint自动化系列——通过PowerShell创建SharePoint Site Collection
2015-09-17 18:06
621 查看
通过PowerShell创建SharePoint Site Collection,代码如下:
文中Template和TemplateID可替换(其他部分也可以进行替换,看个人需求。),switch语句中可以扩展模板ID(相应在提示填写模板名处也要做相应的添加)。日常工作中,选择几个常用的模板ID和模板名填进去即可,UserName填写你常用的那个就可以,最好是所有环境都通用的user(这样就不会因为换了域就找不到用户了),不用填写域,因为SharePoint支持模糊搜索,会自动将你输入的名字进行匹配,比如你输入Tylan,如果在SharePoint中存在该用户的话,SharePoint在check用户名时会自动在其前面加上Domain,像这样:“Domain\Tylan”。
其实写成脚本就是为了方便日常工作,节省时间,具体要把哪些地方设成变量哪些地方进行硬编码可以根据工作需要而变,仍然是以提高工作效率为目的。如果是长久的项目,为了推广使用,做个窗体工具也未尝不可,关键是看有没有这个必要。
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”。
其实写成脚本就是为了方便日常工作,节省时间,具体要把哪些地方设成变量哪些地方进行硬编码可以根据工作需要而变,仍然是以提高工作效率为目的。如果是长久的项目,为了推广使用,做个窗体工具也未尝不可,关键是看有没有这个必要。
相关文章推荐
- Bash编程七大展开之参数展开(Parameter Expansion)
- shell笔记
- xshell连接不了opensuse linux解决办法
- ping命令检测脚本
- bash与vi编辑器常用快捷键
- bash与vi编辑器常用快捷键
- 关于xshell无法连接虚拟机中linux
- Linux shell脚本执行提示错误[[: not found
- Linux shell(1)
- linux insufficient permissions for device(解决adb shell问题)
- 你可能不知道的Shell
- linux shell软链接的问题
- shell脚本基础进阶(一)----shell介绍
- shell计算工具源码
- L7 linux shell编程练习
- shell 为变量赋值 命令的输出结果赋值
- L7.3 linux shell 循环语句总结
- linux shell必知必会sed、awk
- Bash Shell 重定向(二):中文解释
- Bash Shell 重定向(一):原文