Powershell 设置日历上的文件夹权限,除非该用户已存在权限

Powershell 设置日历上的文件夹权限,除非该用户已存在权限,powershell,exchange-server,Powershell,Exchange Server,设置日历上的文件夹权限,除非该用户已存在权限。 我正在使用此计划脚本在用户日历上设置正确的文件夹权限。 工作很好,但是由于脚本通过遍历所有用户占用了很多时间,我想知道,如果“calendaruser”已经具有“editor”权限,那么如何检查脚本是否跳过添加和设置部分 $cred = Get-AutomationPSCredential -Name 'AzureAutomationAccount' $accessrights = "editor" $S

设置日历上的文件夹权限,除非该用户已存在权限。 我正在使用此计划脚本在用户日历上设置正确的文件夹权限。 工作很好,但是由于脚本通过遍历所有用户占用了很多时间,我想知道,如果“calendaruser”已经具有“editor”权限,那么如何检查脚本是否跳过添加和设置部分

    $cred = Get-AutomationPSCredential -Name 'AzureAutomationAccount'
    $accessrights = "editor"
    $Scope = "calendaruser"
    $FolderCalendars = @("Agenda", "Calendar","Calendrier", "Kalender")


    Write-Output "Connecting to Exchange Online."

        $session = New-PSSession –ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $cred -Authentication Basic -AllowRedirection -ThrottleLimit 1
        $commands = @("Get-MailboxFolderPermission","Set-MailboxFolderPermission","Add-MailboxFolderPermission","Get-Mailbox","Get-MailboxFolderStatistics")
        $null = Import-PSSession -Session $session -DisableNameChecking:$true -CommandName $commands -AllowClobber:$true
        write-output "[INFO] - Connected to Exchange Online"
        write-output "[INFO] - Starting  Get-Users"
        $users = Get-Mailbox -RecipientTypeDetails UserMailbox
        Write-Output "Found $(@($users.count)) mailboxes."

 

    write-output "[INFO] - Starting  Set-Sharing"
    write-output "[INFO] - Set sharing for $($users.count) users, with accessrights $accessrights"
    foreach($user in Get-Mailbox -RecipientTypeDetails UserMailbox) {
        $calendarName = $null
        write-output "[INFO] - Checking $($user.Identity)"
        $Calendars = (Get-MailboxFolderStatistics $user.Identity -FolderScope Calendar)
        if($calendars.count -gt 1){
            write-output "[INFO] - User got multiple calendars, checking with known calendars"
            foreach($calendar in $calendars){
                #Only continue if correct calendar names hasn't been found
                if($calendarName -eq $null){
                    write-output "[INFO] - Checking calendar: $($calendar.Name)"
                    foreach($folder in $FolderCalendars){
                        write-output "[INFO] - Checking known calendar: $folder"
                        if($folder -eq $calendar.Name){
                            write-output "[INFO] - Match found, saving $folder as CalendarName"
                            $calendarName = $folder
                            break;
                        }
                    }
                }
            }
        }
        else{
            write-output "[INFO] - User only has one calendar $($Calendars.Name)"
            $calendarName = $Calendars.Name
        }
        if($calendarName -eq $null){
            Write-Host "[ERROR] - No calendar found, skipping"
        }
        else{
            $cal = $user.Identity+":\$CalendarName"
            write-output "[INFO] - Setting permissions for $($cal) with scope $scope to $accessrights"
            
            try{
                Add-MailboxFolderPermission -Identity $cal -User $scope -AccessRights $accessrights
                Set-MailboxFolderPermission -Identity $cal -User $scope -AccessRights $accessrights
                write-output "[INFO] - Set calendar properties"
            }
            catch{
                write-output "[ERROR] - Error setting properties"
                write-output "$($_.Exception.Message)"
            }
        }
        Write-Host "-------------"
    }

# Close all open Exchange sessions

Get-PSSession | Remove-PSSession

Write-Output "`r`nExchange Online session closed."

注意:如果你能提供一个答案,你会得到更多的答案(重点是最小值)