简体   繁体   English

SharePoint Online Powershell文件夹创建

[英]SharePoint Online Powershell Folder creation

I need to be able to script folder creation from a csv into a SharePoint Online document library with each folder with permission inheritance disabled and for different user to each folder to be added. 我需要能够将从csv创建的文件夹脚本化为SharePoint Online文档库,其中每个文件夹都禁用了权限继承,并且要为每个文件夹添加不同的用户。

The following code can create the folders and disable the inheritance but it seems to try add a group but not a user. 以下代码可以创建文件夹并禁用继承,但似乎尝试添加组而不是用户。 How to make it add a user instead? 如何使其添加用户呢?

Thanks. 谢谢。

### 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

Let's assume that you will define user login in your CSv file. 假设您将在CSv文件中定义用户登录。 Than you have to change the line: 比您必须更改的行:

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

to

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

and replace all references to $group variable with $user 并将所有对$group变量的引用替换为$user

More generic approach for searching for a user (with for example display name) would be using Utility.ResolvePrincipal method: 搜索用户(例如显示名称)的更通用方法是使用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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM