西西軟件下載最安全的下載網(wǎng)站、值得信賴的軟件下載站!

首頁編程開發(fā)其它知識 → SharePoint 2010中 PowerShell 腳本應用總結(jié)

SharePoint 2010中 PowerShell 腳本應用總結(jié)

相關(guān)軟件相關(guān)文章發(fā)表評論 來源:西西整理時間:2013/1/17 10:33:37字體大。A-A+

作者:西西點擊:3次評論:0次標簽: PowerShell

  • 類型:文件處理大小:11.3M語言:英文 評分:5.0
  • 標簽:
立即下載

把項目中用到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)建完成了。

單擊下載

    相關(guān)評論

    閱讀本文后您有什么感想? 已有人給出評價!

    • 8 喜歡喜歡
    • 3 頂
    • 1 難過難過
    • 5 囧
    • 3 圍觀圍觀
    • 2 無聊無聊

    熱門評論

    最新評論

    發(fā)表評論 查看所有評論(0)

    昵稱:
    表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
    字數(shù): 0/500 (您的評論需要經(jīng)過審核才能顯示)
    推薦文章

    沒有數(shù)據(jù)