前段时间太忙,好久没更新博客了,项目总算完成了,就把项目中用到PowerShell脚本整理一下,分享给需要的朋友。
首先通过Powershell创建WebApplication,创建时要判断是否存在,如果存在就删除,在这个操作中有异常,如果WebApplication不存在,会出现错误,不知如何避免,用Try 也不起作用,如果谁有好的解决办法可以回复。
一、创建 WebApplication、Sites、Web、及激活Features
# check to ensure Microsoft.SharePoint.PowerShell is loaded$snapin = Get-PSSnapin | Where-Object { $_.Name -eq 'Microsoft.SharePoint.Powershell'}if ($snapin -eq $null) { Write-Host "Loading SharePoint Powershell Snapin" Add-PSSnapin "Microsoft.SharePoint.Powershell"}$WebAppPoolAccount = "T\Administrator"$WebAppName = "SharePoint - XXX.com80"$WebAppPort = 80$WebAppPool = "SharePoint - XXX.com80"$WebAppDBName = "WSS_Content_XXX"$WebAppUrl = "http://XXX.com"$WebAppUrl_XXX = "http://XXX.com/XXX"$WebAppHostHeader = "XXX.com"$SiteTemplate_WorkGroup = "STS#0" #STS#0 是工作组网站$SiteTemplate_DocCenter = "BDR#0" #STS#0 是文档中心################################################ WebApplication ##################################################Delete any existing Webapplication by name$targetUrl = Get-SPWebApplication $WebAppUrlif($targetUrl -ne $null){ Write-host "Deleting existing webapplication:" $WebAppName Remove-SPWebApplication $WebAppName -confirm -DeleteIISSite -removeContentDatabase Write-host "Deleted Site and ContentDatabase successful." -foregroundcolor red}Write-Host "Starting create WebApplication"Write-Host "Please waiting......" -foregroundcolor green$WebApp = New-SPWebApplication -Name $WebAppName -Port $WebAppPort -HostHeader $WebAppHostHeader -URL $WebAppUrl -ApplicationPool $WebAppPool -DatabaseName $WebAppDBName -ApplicationPoolAccount (get-SPManagedAccount $WebAppPoolAccount)Write-Host "WebApplication Created successfully"#display WebApplication InfoWrite-HostWrite-Host "------------------------------" -foregroundcolor GreenWrite-Host "Name:" $WebApp.Name -foregroundcolor greenWrite-Host "URL:" $WebApp.Url -foregroundcolor greenWrite-Host "------------------------------" -foregroundcolor Green########### 自定义方法 Add Web #############Function Add_Web($webUrl,$webName,$template){ Write-Host "Creating new web " $webUrl " please waiting......" New-SPWeb -Url $webUrl -Template $template -Name $webName Write-Host "Created " $webUrl " sucessful!" -foregroundcolor green}########### Site #####################if($WebApp -ne $null){ Write-host $WebApp.Url -foregroundcolor green $SiteUrl = $WebApp.Url $SiteTitle = "XXX" # 创建网站集 Write-Host "Creating new site " $SiteTitle " ,please waiting......" $NewSite = New-SPSite -Url $SiteUrl -OwnerAlias $WebAppPoolAccount -Template $SiteTemplate_WorkGroup -Name $SiteTitle Write-host "Finish Createed successful" -foregroundcolor green # 激活网站集功能 Write-host "Starting active PublishingSite feature" Enable-SPFeature –Identity PublishingSite –url $WebAppUrl Write-host "PublishingSite Actived" -foregroundcolor green #创建子站点 Add_Web $WebAppUrl_XXX "XXX" $SiteTemplate_WorkGroup $NewSite.Dispose() }else{ Write-host "WebApplication " $WebApp.Name " is not existing"}
二、Site创建后,要创建用户组、分配权限、初始化人员信息
View Code
################################################### SPGROUP# 获取网站集$Web = Get-SPWeb $WebAppUrl$GroupCount = $Web.SiteGroups.Count # 获取站点用户组的个数#删除目前已有的用户组While($GroupCount -ne 0){ $Web.SiteGroups.Remove($Web.SiteGroups[$GroupCount-1]) $GroupCount--}$Web.Update();################################ 自定义方法 # 给用户组分配相应的权限function AddRoleAssignment($group,$role){ $RoleAssignment = New-Object Microsoft.SharePoint.SPRoleAssignment($group); $RoleAssignment.RoleDefinitionBindings.Add($role) $Web.RoleAssignments.Add($RoleAssignment)}$Domain = "T\"#添加用户function AddUser($group,$userName){ $User = $web.Site.RootWeb.EnsureUser($Domain + $userName) $group.AddUser($User)}# 给相应的组添加成员function AddUser_DeptLeader($group){ foreach($user in $group.Users){ $group.RemoveUser($user); } AddUser $group "zhangjiangfeng"}# 获取相应的角色$RoleName_Discuss = "参与讨论"$RoleDiscuss = $Web.RoleDefinitions[$RoleName_Discuss]$RoleName_Contribution = "完全控制"$RoleContribution = $Web.RoleDefinitions[$RoleName_Contribution]#用户组名$DeptLeader = "XXX"################################################################# # 添加组及人员$Group = $Web.SiteGroups[$DeptLeader]if($Group -eq $null){ # 判断组是否存在 $Web.SiteGroups.Add($DeptLeader,$Web.CurrentUser, $Web.CurrentUser,""); #添加组 $Group = $Web.SiteGroups[$DeptLeader] #获取组 AddUser_DeptLeader $Group #添加人员 AddRoleAssignment $Group $RoleDiscuss #分配权限}else{ AddUser_DeptLeader $Group}$Web.Update();foreach($Group in $Web.SiteGroups){ write-host $Group.Name}$Web.Dispose()Write-Host "Finished" -foregroundcolor green
三、添加 QuickLaunch 及分配权限
##################### 添加导航$Web = Get-SPWeb $WebAppUrl_XXX$NodeList_XXX = $Web.Navigation.QuickLaunch # 获取快速启动栏$NodeList_XXXCount = $NodeList_XXX.Count #获取菜单个数Foreach($node in $NodeList_XXX){ # 遍历输出所有菜单 write-host $node.Title}While($NodeList_XXXCount -ne 0){ # 删除已有菜单 $NodeList_XXX[$NodeList_XXXCount - 1].Delete() $NodeList_XXXCount --}##########我的菜单$NewNode = New-Object Microsoft.SharePoint.Navigation.SPNavigationNode("我的菜单", "",$true)$NodeList_XXX.AddAsLast($NewNode)$NewChild = New-Object Microsoft.SharePoint.Navigation.SPNavigationNode("我的子菜单", "/SitePages/XXX.aspx",$true)$NewNode.Children.AddAsLast($NewChild)$NewChild.Properties["Audience"] = ";;;;用户组名" #给菜单分配相应的访问权限$NewChild.Update()
四、创建文档库及文件夹
View Code
#################################### 添加文档库及文件夹# SPListTemplateType$DocTemp = [Microsoft.SharePoint.SPListTemplateType]::DocumentLibrary$Folder = [Microsoft.SharePoint.SPFileSystemObjectType]::Folder# Delete ListFunction DeleteList($web,$listName){ $list = $web.Lists.TryGetList($listName) if($list -ne $null){ Write-Host "............." $listName "already exists,deleting................" -foregroundcolor red $list.Delete() Write-host "............." $listName "already is deleted....................." -foregroundcolor Green Write-host "" }}# Add ListFunction AddList($web,$listName,$listDescription,$listTemplate){ DeleteList $web $listName Write-host ".............Starting create " $listName ",please waiting............." $web.Lists.Add($listName,$listDescription,$listTemplate) Write-host ".............. " $listName " already is created......................." -foregroundcolor green}# List OnQuickLaunchFunction OnQuickLaunchList($list,$bool){ $list.OnQuickLaunch = $bool $list.Update()}# Add ItemFunction AddItemFolder($web,$url,$listName,$name){ Write-host ".............Starting create " $name " Folder,please waiting............." -foregroundcolor blue $list = $web.Lists.TryGetList($listName) if($list -ne $null){ $folder = $list.AddItem($url,$Folder,$name) $folder.Update() Write-host ".............. " $name " Folder already is created......................." -foregroundcolor green }}##$Web = Get-SPWeb $WebAppUrl_XXX################# 添加文档库AddList $Web "我是文档库" "我是文档库" $DocTempAddItemFolder $Web "我是文件夹" "我是文件夹"$Web.Dispose()
五、文档库断开继承并分配权限
View Code
############################################################ 分配权限$RoleName_Discuss = "参与讨论"$RoleName_Contribution = "完全控制"$RoleName_Read = "读取"# 断开继承Function BreakRoleInheritance($web,$listName){ $list = $web.Lists.TryGetList($listName) if($list -ne $null){ Write-Host "............." $listName "BreakRoleInheritance ................" $list.BreakRoleInheritance($false,$false) Write-host "............." $listName "BreakRoleInheritance sucessed....................." -foregroundcolor Green return $list }}# 分配权限Function AssignPermission($web,$list,$groupName,$roleDefinition){ $group = $web.SiteGroups[$groupName]; if($group -ne $null){ write-host "............." $list.Title "Add RoleAssignment" $groupName $roleAssignment = New-Object Microsoft.SharePoint.SPRoleAssignment($group); $permLevel = $web.RoleDefinitions[$roleDefinition]; $roleAssignment.RoleDefinitionBindings.Add($permLevel); $list.RoleAssignments.Add($roleAssignment); }}$Web = Get-SPWeb $WebAppUrl_XXX$List = BreakRoleInheritance $Web "我是文档库"if($List -ne $null){ AssignPermission $Web $List "XXX" $RoleName_Read}$Web.Update();$Web.Dispose();
六、只要单击Cnblogs.bat 一个基本的网站就创建完成了。