-
Notifications
You must be signed in to change notification settings - Fork 1
/
Set-CalendarSharingPolicy.ps1
93 lines (79 loc) · 3.36 KB
/
Set-CalendarSharingPolicy.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<#
.Synopsis
Set calendar sharing permission in mailboxes where default sharing permission is different than $Sharing_Policy
The script is provided “AS IS” with no guarantees, no warranties, and they confer no rights.
.DESCRIPTION
.NOTES
Author: Michal Ziemba
File Name: Set-CalendarSharingPolicy.ps1
Version: 1.0.0, DateUpdated: 2017-03-22
Version: 1.0.1, DateUpdated: 2017-04-11
add try/catch for finding a $calendar and $calendar_permission
.LINK
https://pl.linkedin.com/in/mziemba
#>
#Credentials to connect to Exchange Online
$Credentials = Get-AutomationPSCredential -Name 'Office 365 User Management'
#Prefix to search for
function Connect-ExchangeOnline {
param (
$Creds
)
#Clean up existing PowerShell Sessions
Get-PSSession | Remove-PSSession
#Connect to Exchange Online
$Session = New-PSSession –ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Credentials -Authentication Basic -AllowRedirection
Import-PSSession -Session $Session -DisableNameChecking:$true -AllowClobber:$true | Out-Null
}
Connect-ExchangeOnline -Creds $Credentials
$VerbosePreference='Continue'
<#
Check what "Default Sharing Policy" is set in the Office 365 and set the $Sharing_Policy variable based on this
The following sharing policy action values can be found:
- CalendarSharingFreeBusySimple Share free/busy hours only.
- CalendarSharingFreeBusyDetail Share free/busy hours, subject, and location.
- CalendarSharingFreeBusyReviewer Share free/busy hours, subject, location, and the body of the message or calendar item.
- ContactsSharing Share contacts only.
#>
switch (((Get-SharingPolicy).domains|? {$_ -match '\*'}) -replace "\*:","")
{
# The following roles apply specifically to calendar folders:
# AvailabilityOnly - View only availability data
# LimitedDetails - View availability data with subject and location
'CalendarSharingFreeBusyDetail'
{$Sharing_Policy = "LimitedDetails"}
Default
{$Sharing_Policy = "AvailabilityOnly"}
}
$ErrorActionPreference = "Stop"
$mailboxes = get-mailbox -ResultSize unlimited
foreach ($mailbox in $mailboxes){
Try
{
$calendar = (Get-MailboxFolderStatistics $mailbox.UserPrincipalName -FolderScope calendar |select -First 1).Identity.Replace("\",":\")
Try
{
$calendar_permission = $calendar|Get-MailboxFolderPermission -user Default
}
catch
{
Write-Output "Couldn't find a default user permision for the calendar :$($calendar)`n$($_.Exception.Message)"
}
if ($calendar_permission.AccessRights -ne $Sharing_Policy)
{
try
{
Set-MailboxFolderPermission -Identity $calendar -User Default -AccessRights $Sharing_Policy -WarningAction stop -WhatIf
Write-Output "Set the default calendar permission for $($mailbox.userprincipalname)"
}
catch
{
Write-Output "Failed to set the default calendar permission for $($mailbox.userprincipalname)`n$($_.Exception.Message)"
}
}
}
Catch
{
Write-Output "Couldn't finnd a calendar for mailbox:$($mailbox.userprincipalname)`n$($_.Exception.Message)"
}
}