把項目中用到PowerShell腳本整理一下,分享給需要的朋友。首先通過Powershell創(chuàng)建WebApplication,創(chuàng)建時要判斷是否存在,如果存在就刪除,在這個操作中有異常,如果WebApplication不存在,會出現(xiàn)錯誤,不知如何避免,用Try 也不起作用,如果誰有好的解決辦法可以回復。
一、創(chuàng)建 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 是工作組網(wǎng)站 $SiteTemplate_DocCenter = "BDR#0" #STS#0 是文檔中心 ################################################ WebApplication ################################################# #Delete any existing Webapplication by name $targetUrl = Get-SPWebApplication $WebAppUrl if($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 Info Write-Host Write-Host "------------------------------" -foregroundcolor Green Write-Host "Name:" $WebApp.Name -foregroundcolor green Write-Host "URL:" $WebApp.Url -foregroundcolor green Write-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" # 創(chuàng)建網(wǎng)站集 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 # 激活網(wǎng)站集功能 Write-host "Starting active PublishingSite feature" Enable-SPFeature –Identity PublishingSite –url $WebAppUrl Write-host "PublishingSite Actived" -foregroundcolor green #創(chuàng)建子站點 Add_Web $WebAppUrl_XXX "XXX" $SiteTemplate_WorkGroup $NewSite.Dispose() } else{ Write-host "WebApplication " $WebApp.Name " is not existing" }
二、Site創(chuàng)建后,要創(chuàng)建用戶組、分配權(quán)限、初始化人員信息
################################################### SPGROUP # 獲取網(wǎng)站集 $Web = Get-SPWeb $WebAppUrl $GroupCount = $Web.SiteGroups.Count # 獲取站點用戶組的個數(shù) #刪除目前已有的用戶組 While($GroupCount -ne 0){ $Web.SiteGroups.Remove($Web.SiteGroups[$GroupCount-1]) $GroupCount-- } $Web.Update(); ################################ 自定義方法 # 給用戶組分配相應的權(quán)限 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 #分配權(quán)限 } else{ AddUser_DeptLeader $Group } $Web.Update(); foreach($Group in $Web.SiteGroups){ write-host $Group.Name } $Web.Dispose() Write-Host "Finished" -foregroundcolor green
三、添加 QuickLaunch 及分配權(quán)限
##################### 添加導航 $Web = Get-SPWeb $WebAppUrl_XXX $NodeList_XXX = $Web.Navigation.QuickLaunch # 獲取快速啟動欄 $NodeList_XXXCount = $NodeList_XXX.Count #獲取菜單個數(shù) 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"] = ";;;;用戶組名" #給菜單分配相應的訪問權(quán)限 $NewChild.Update()
四、創(chuàng)建文檔庫及文件夾
#################################### 添加文檔庫及文件夾
# SPListTemplateType
$DocTemp = [Microsoft.SharePoint.SPListTemplateType]::DocumentLibrary
$Folder = [Microsoft.SharePoint.SPFileSystemObjectType]::Folder
# Delete List
Function 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 List
Function 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 OnQuickLaunch
Function OnQuickLaunchList($list,$bool){
$list.OnQuickLaunch = $bool
$list.Update()
}
# Add Item
Function 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 "我是文檔庫" "我是文檔庫" $DocTemp
AddItemFolder $Web "我是文件夾" "我是文件夾"
$Web.Dispose()
五、文檔庫斷開繼承并分配權(quán)限
############################################################ 分配權(quán)限 $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 } } # 分配權(quán)限 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 一個基本的網(wǎng)站就創(chuàng)建完成了。