2017-06-02 81 views
0

我需要能夠將文件夾從csv創建到SharePoint Online文檔庫,每個文件夾的權限繼承被禁用,並且不同的用戶可以添加到每個文件夾。SharePoint Online Powershell文件夾創建

以下代碼可以創建文件夾並禁用繼承,但似乎嘗試添加一個組而不是用戶。如何讓它添加一個用戶呢?

謝謝。

### Get the user credentials 
$credential = Get-Credential 
$username = $credential.UserName 
$password = $credential.GetNetworkCredential().Password 
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force 

### Input Parameters 
$url = 'URL HERE' 
$csvfilepath='C:\Scripts\data.csv' 
$libname ='BUS61' 

### References 
# Specified the paths where the dll's are located. 
Add-Type -Path 'C:\Scripts\SPOCmdlets\Microsoft.SharePoint.Client.dll' 
Add-Type -Path 'C:\Scripts\SPOCmdlets\Microsoft.SharePoint.Client.Runtime.dll' 


### CreateFolder with Permissions Function 
function CreateFolderWithPermissions() 
{ 

    # Connect to SharePoint Online and get ClientContext object. 
    $clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url) 
    $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword) 
    $clientContext.Credentials = $credentials 

    Function GetRole 
    { 
     [CmdletBinding()] 
     param 
     (
      [Parameter(Mandatory = $true, Position = 1)] 
      [Microsoft.SharePoint.Client.RoleType]$rType 
     ) 

     $web = $clientContext.Web 
     if ($web -ne $null) 
     { 
      $roleDefs = $web.RoleDefinitions 
      $clientContext.Load($roleDefs) 
      $clientContext.ExecuteQuery() 
      $roleDef = $roleDefs | Where-Object { $_.RoleTypeKind -eq $rType } 
      return $roleDef 
     } 
     return $null 
    } 

    # Get the SharePoint web 
    $web = $clientContext.Web; 
    $clientContext.Load($web) 

    #Get the groups 
    $groups = $web.SiteGroups 
    $clientContext.Load($groups) 
    $clientContext.ExecuteQuery() 


    #Read CSV File and iterate 
    $csv = Import-CSV $csvfilepath 
    foreach ($row in $csv) 
    { 
     #Create Folder 
     $folder = $web.Folders.Add($libname + "/" + $row.Folder) 
     $clientContext.Load($folder) 
     $clientContext.ExecuteQuery() 

     #Assign Role 
     $group = $groups.GetByName($row.Group) 
     $clientContext.Load($group) 
     $clientContext.ExecuteQuery() 

     $roleType= $row.Role 
     $roleTypeObject = [Microsoft.SharePoint.Client.RoleType]$roleType 
     $roleObj = GetRole $roleTypeObject 
     $usrRDBC = $null 
     $usrRDBC = New-Object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($clientContext) 
     $usrRDBC.Add($roleObj) 

     # Remove inherited permissions 
     $folder.ListItemAllFields.BreakRoleInheritance($false, $true) 
     $clientContext.Load($folder.ListItemAllFields.RoleAssignments.Add($group, $usrRDBC)) 
     $folder.Update() 
     $clientContext.ExecuteQuery()   

     # Display the folder name and permission 
     Write-Host -ForegroundColor Blue 'Folder Name: ' $folder.Name ' Group: '$row.Group ' Role: ' $roleType; 

    } 
} 
#Execute the function 
CreateFolderWithPermissions 
+0

你是否敲我的答案? – tinamou

回答

0

我們假設您將在CSv文件中定義用戶登錄。比你要更改的行:

$group = $groups.GetByName($row.Group) 

$user = $web.EnsureUser($row.User) 

$user

更通用的方法替換所有引用$group變量用於搜索用戶(通過例如顯示器名稱)將使用Utility.ResolvePrincipal方法:

[Microsoft.SharePoint.Client.Utilities.Utility]::ResolvePrincipal($clientContext, $web, "DisplayName", ([Microsoft.SharePoint.Client.Utilities.PrincipalType]::User), ([Microsoft.SharePoint.Client.Utilities.PrincipalSource]::All), $null, $false) 
相關問題