2017-06-02 142 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) 
関連する問題