Skip to content

Instantly share code, notes, and snippets.

@kevinblumenfeld
Last active June 7, 2023 13:45
Show Gist options
  • Save kevinblumenfeld/59a1906578c4cc606319335a11c490dd to your computer and use it in GitHub Desktop.
Save kevinblumenfeld/59a1906578c4cc606319335a11c490dd to your computer and use it in GitHub Desktop.
function Hide-PSGCalendar {
<#
.SYNOPSIS
if Owner of Calendar...
1. Remove as Owner from Calendar, if not chosen by client in PrimaryOwnerFilePath
2. Add as Editor to Calendar
3. Hide Calendar if #1 and #2 does not work
.DESCRIPTION
Long description
.PARAMETER PrimaryOwnerFilePath
List of the client decisions of Calendar Owners
.PARAMETER CalendarPermissionFilePath
All Calendar Permissions
.EXAMPLE
Hide-PSGCalendar -PrimaryOwnerFilePath ./ClientFile.csv -CalendarPermissionFilePath ./AllCalendarPerms.csv
.NOTES
General notes
#>
[CmdletBinding()]
Param (
[Parameter()]
[ValidateScript( { Test-Path $_ })]
$PrimaryOwnerFilePath,
[Parameter(Mandatory)]
[ValidateScript( { Test-Path $_ })]
$CalendarPermissionFilePath
)
$PrimaryHash = @{ }
$PrimaryList = Import-Csv $PrimaryOwnerFilePath
foreach ($Primary in $PrimaryList) {
if ($Primary.PrimaryOwnerEmail -like "*@*") {
$PrimaryHash[$Primary.CalendarEmail] = $Primary.PrimaryOwnerEmail
}
}
$PermissionList = Import-Csv $CalendarPermissionFilePath
foreach ($Permission in $PermissionList) {
Write-Host "$($Permission.CalendarEmail) ROLE: $($Permission.AccessRole)" -foregroundcolor cyan
if ($Permission.CalendarEmail -like "*@*.calendar.google.com" -and $Permission.AccessRole -eq 'Owner') {
Write-Host "OneOwner: $($PrimaryHash[$Permission.CalendarEmail])" -foregroundcolor cyan
Write-Host "AssignedUser: $($Permission.AssignedUser)" -foregroundcolor cyan
if ($PrimaryHash[$Permission.CalendarEmail] -and ($PrimaryHash[$Permission.CalendarEmail] -ne $Permission.AssignedUser)) {
try {
$ACL = $null
$ACL = Get-GSCalendarAcl -CalendarId $Permission.CalendarEmail | Where-Object {
($_.ID).split(':')[1] -eq $Permission.AssignedUser
}
$null = $ACL | Remove-GSCalendarAcl -ErrorAction 'Stop'
Write-Host "Removed $($Permission.AssignedUser) as owner from Calendar $($Permission.CalendarEmail)" -ForegroundColor Green
[PSCustomObject]@{
CalendarEmail = $Permission.CalendarEmail
CalendarName = $Permission.CalendarName
RemovedUser = $Permission.AssignedUser
AccessRole = $Permission.AccessRole
Step = 'REMOVEASOWNER'
Result = 'SUCCESS'
Log = 'SUCCESS'
}
try {
$NewSplat = @{
CalendarId = $Permission.CalendarEmail
Role = 'writer'
Value = $Permission.AssignedUser
Type = 'user'
ErrorAction = 'Stop'
}
$null = New-GSCalendarAcl @NewSplat
Write-Host "Added $($Permission.AssignedUser) as Editor to Calendar $($Permission.CalendarEmail)" -ForegroundColor Green
[PSCustomObject]@{
CalendarEmail = $Permission.CalendarEmail
CalendarName = $Permission.CalendarName
RemovedUser = $Permission.AssignedUser
AccessRole = $Permission.AccessRole
Step = 'ADDASEDITOR'
Result = 'SUCCESS'
Log = 'SUCCESS'
}
try {
$UpSplat = @{
User = $Permission.AssignedUser
CalendarId = $Permission.CalendarEmail
Hidden = $true
ErrorAction = 'Stop'
}
$null = Update-GSCalendarSubscription @UpSplat
Write-Host "Hidden from user $($Permission.AssignedUser) the Calendar $($Permission.CalendarEmail)" -ForegroundColor Green
[PSCustomObject]@{
CalendarEmail = $Permission.CalendarEmail
CalendarName = $Permission.CalendarName
RemovedUser = $Permission.AssignedUser
AccessRole = $Permission.AccessRole
Step = 'HIDEFROMUSER'
Result = 'SUCCESS'
Log = 'SUCCESS'
}
}
catch {
Write-Host "Failed to hide from user $($Permission.AssignedUser) the Calendar $($Permission.CalendarEmail)" -ForegroundColor Red
[PSCustomObject]@{
CalendarEmail = $Permission.CalendarEmail
CalendarName = $Permission.CalendarName
RemovedUser = $Permission.AssignedUser
AccessRole = $Permission.AccessRole
Step = 'HIDEFROMUSER'
Result = 'FAILED'
Log = $_.Exception.Message
}
}
}
catch {
Write-Host "Failed to add $($Permission.AssignedUser) as Editor to Calendar $($Permission.CalendarEmail)" -ForegroundColor Red
[PSCustomObject]@{
CalendarEmail = $Permission.CalendarEmail
CalendarName = $Permission.CalendarName
RemovedUser = $Permission.AssignedUser
AccessRole = $Permission.AccessRole
Step = 'ADDASEDITOR'
Result = 'FAILED'
Log = $_.Exception.Message
}
}
}
catch {
Write-Host "Failed to remove $($Permission.AssignedUser) as owner from Calendar $($Permission.CalendarEmail)" -ForegroundColor Red
[PSCustomObject]@{
CalendarEmail = $Permission.CalendarEmail
CalendarName = $Permission.CalendarName
RemovedUser = $Permission.AssignedUser
AccessRole = $Permission.AccessRole
Step = 'REMOVEASOWNER'
Result = 'FAILED'
Log = $_.Exception.Message
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment