I have a script as follows but I need to run it against domain A and then against domain B and not a UserPrincipal name it works with $Exception = @(“[email protected]<\/a>”, “[email protected]<\/a>”) The script runs against domain A and B simultaneously with no issues.<\/p>\n
But not using $Exception = @(“@A-DOMAIN.org.uk<\/span>”) or $Exception = @(“@B-DOMAIN.org.uk<\/span>”) how do I correct this the full script is below as I am new to this can anyone point out my mistakes<\/p>\n
<# .DESCRIPTION The script works for: .LINK<\/p>\n .NOTES<\/p>\n .CHANGELOG<\/p>\n #><\/p>\n Start-Transcript -Path “C:\\temp\\Set-DefCalPermissions01.log” -Append<\/p>\n #Set-ADServerSettings<\/span> -ViewEntireForest $true<\/p>\n $Users = Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails UserMailbox<\/p>\n $Permission = “LimitedDetails”<\/p>\n $FolderCalendars = @(“Agenda”, “Calendar”, “Calendrier”, “Kalender”, “日历”)<\/p>\n foreach ($User in $Users) {<\/p>\n $Calendars = (Get-MailboxFolderStatistics $User.UserPrincipalName -FolderScope Calendar)<\/p>\n foreach ($Calendar in $Calendars) { if ($FolderCalendars -Contains $CalendarName) { Set-MailboxFolderPermission -Identity $Cal -User Default -AccessRights $Permission -WarningAction:SilentlyContinue -WhatIf<\/p>\n if ($CurrentMailFolderPermission.AccessRights -eq “$Permission”) { Stop-Transcript<\/p>","upvoteCount":6,"answerCount":5,"datePublished":"2023-11-14T19:31:06.000Z","author":{"@type":"Person","name":"simoneastwood","url":"https://community.spiceworks.com/u/simoneastwood"},"acceptedAnswer":{"@type":"Answer","text":" One way to fix it for a domain name match rather than an actual userprincipalname match, is to use -match and flip it. Also, the variable doesn’t have to be declared with @().<\/p>\n Switch to:<\/p>\n I have a script as follows but I need to run it against domain A and then against domain B and not a UserPrincipal name it works with $Exception = @(“[email protected]<\/a>”, “[email protected]<\/a>”) The script runs against domain A and B simultaneously with no issues.<\/p>\n But not using $Exception = @(“@A-DOMAIN.org.uk<\/span>”) or $Exception = @(“@B-DOMAIN.org.uk<\/span>”) how do I correct this the full script is below as I am new to this can anyone point out my mistakes<\/p>\n <# .DESCRIPTION The script works for: .LINK<\/p>\n .NOTES<\/p>\n .CHANGELOG<\/p>\n #><\/p>\n Start-Transcript -Path “C:\\temp\\Set-DefCalPermissions01.log” -Append<\/p>\n #Set-ADServerSettings<\/span> -ViewEntireForest $true<\/p>\n $Users = Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails UserMailbox<\/p>\n $Permission = “LimitedDetails”<\/p>\n $FolderCalendars = @(“Agenda”, “Calendar”, “Calendrier”, “Kalender”, “日历”)<\/p>\n foreach ($User in $Users) {<\/p>\n $Calendars = (Get-MailboxFolderStatistics $User.UserPrincipalName -FolderScope Calendar)<\/p>\n foreach ($Calendar in $Calendars) { if ($FolderCalendars -Contains $CalendarName) { Set-MailboxFolderPermission -Identity $Cal -User Default -AccessRights $Permission -WarningAction:SilentlyContinue -WhatIf<\/p>\n if ($CurrentMailFolderPermission.AccessRights -eq “$Permission”) { Stop-Transcript<\/p>","upvoteCount":6,"datePublished":"2023-11-14T19:31:07.000Z","url":"https://community.spiceworks.com/t/powershell-script-exception-not-working/962504/1","author":{"@type":"Person","name":"simoneastwood","url":"https://community.spiceworks.com/u/simoneastwood"}},{"@type":"Answer","text":" Thank you for a beautifully eloquent and simple code change that works perfectly<\/p>","upvoteCount":0,"datePublished":"2023-11-15T10:04:37.000Z","url":"https://community.spiceworks.com/t/powershell-script-exception-not-working/962504/3","author":{"@type":"Person","name":"simoneastwood","url":"https://community.spiceworks.com/u/simoneastwood"}},{"@type":"Answer","text":" Happy to help One last thing, if you feel your question has been sufficiently answered, I’ll ask that you mark a post as Best Answer so this question is marked as [SOLVED] here, in Spiceworks search results, and in internet search results. Thanks!<\/p>\n
\n.SYNOPSIS
\nPrepareAndSetDefaultCalendarPermissionsForAllUsers.ps1<\/p>\n
\nSet default calendar permissions for all user mailboxes including exception for users.<\/p>\n
\n-Exchange On-Premises (Run Exchange Management Shell)
\n-Exchange Online (Connect to Exchange Online PowerShell)<\/p>\n<\/a>Script Exclusions<\/h1>\n
<\/a>Exclude users that you don’t want the script to run against. Add them in line 36, 37, 38. If you don’t need this feature, comment out lines 36, 37, 38, 53, 54, 55, 56 and 80.<\/h1>\n
<\/a>Calendars are not always set in the English language. For example, in The Netherlands, it’s named Agenda. The script will check for the calendar names defined in line 44.<\/h1>\n
<\/a>Change permission that you want to set for all the users in line 39.<\/h1>\n
<\/a>Note: The -WhatIf parameter is added in the script on line 66. If you run the script, nothing will happen in the environment. Instead, you get an output showing what will happen.<\/h1>\n
<\/a>Line 36, 37, 38 Option enabled<\/h1>\n
<\/a>-WhatIf parameter Active<\/h1>\n
<\/a>Start transcript<\/h1>\n
<\/a>Set scope to entire forest. Cmdlet only available for Exchange on-premises.<\/h1>\n
<\/a>Get all user mailboxes<\/h1>\n
<\/a>Users exception (add the UserPrincipalName)<\/h1>\n
<\/a>$Exception = @(“*@A-DOMAIN.org.uk<\/span>”)<\/h1>\n
<\/a>$Exception = @(“*@B-DOMAIN.org.uk<\/span>”)<\/h1>\n
<\/a>$Exception = @(“[email protected]<\/a>”, “[email protected]<\/a>”)<\/h1>\n
<\/a>Permissions<\/h1>\n
<\/a>Calendar name languages<\/h1>\n
<\/a>Loop through each user<\/h1>\n
<\/a>Get calendar in every user mailbox<\/h1>\n
<\/a>Leave permissions if user is exception<\/h1>\n
<\/a>if ($Exception -Contains ($User.UserPrincipalName)) {<\/h1>\n
<\/a>Write-Host “$User is an exception, don’t touch permissions” -ForegroundColor Red<\/h1>\n
<\/a>}<\/h1>\n
<\/a>else {<\/h1>\n
<\/a>Loop through each user calendar<\/h1>\n
\n$CalendarName = $Calendar.Name<\/p>\n<\/a>Check if calendar exist<\/h1>\n
\n$Cal = “$($User.UserPrincipalName):$CalendarName”
\n$CurrentMailFolderPermission = Get-MailboxFolderPermission -Identity $Cal -User Default<\/p>\n<\/a>Set calendar permission / Remove -WhatIf parameter after testing<\/h1>\n
<\/a>Write output<\/h1>\n
\nWrite-Host $User.DisplayName already has the permission $CurrentMailFolderPermission.AccessRights -ForegroundColor Yellow
\n}
\nelse {
\nWrite-Host $User.DisplayName added permissions $Permission -ForegroundColor Green
\n}
\n}
\n}
\n}<\/p>\n<\/a>}<\/h1>\n
$Exception = @(\"@A-DOMAIN.org.uk\")\nif ($Exception -Contains ($User.UserPrincipalName))\n<\/code><\/pre>\n
$Exception = \"@A-DOMAIN.org.uk\"\nif ($User.UserPrincipalName -match $Exception)\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2023-11-15T09:51:01.000Z","url":"https://community.spiceworks.com/t/powershell-script-exception-not-working/962504/2","author":{"@type":"Person","name":"jrp78","url":"https://community.spiceworks.com/u/jrp78"}},"suggestedAnswer":[{"@type":"Answer","text":"
\n.SYNOPSIS
\nPrepareAndSetDefaultCalendarPermissionsForAllUsers.ps1<\/p>\n
\nSet default calendar permissions for all user mailboxes including exception for users.<\/p>\n
\n-Exchange On-Premises (Run Exchange Management Shell)
\n-Exchange Online (Connect to Exchange Online PowerShell)<\/p>\n<\/a>Script Exclusions<\/h1>\n
<\/a>Exclude users that you don’t want the script to run against. Add them in line 36, 37, 38. If you don’t need this feature, comment out lines 36, 37, 38, 53, 54, 55, 56 and 80.<\/h1>\n
<\/a>Calendars are not always set in the English language. For example, in The Netherlands, it’s named Agenda. The script will check for the calendar names defined in line 44.<\/h1>\n
<\/a>Change permission that you want to set for all the users in line 39.<\/h1>\n
<\/a>Note: The -WhatIf parameter is added in the script on line 66. If you run the script, nothing will happen in the environment. Instead, you get an output showing what will happen.<\/h1>\n
<\/a>Line 36, 37, 38 Option enabled<\/h1>\n
<\/a>-WhatIf parameter Active<\/h1>\n
<\/a>Start transcript<\/h1>\n
<\/a>Set scope to entire forest. Cmdlet only available for Exchange on-premises.<\/h1>\n
<\/a>Get all user mailboxes<\/h1>\n
<\/a>Users exception (add the UserPrincipalName)<\/h1>\n
<\/a>$Exception = @(“*@A-DOMAIN.org.uk<\/span>”)<\/h1>\n
<\/a>$Exception = @(“*@B-DOMAIN.org.uk<\/span>”)<\/h1>\n
<\/a>$Exception = @(“[email protected]<\/a>”, “[email protected]<\/a>”)<\/h1>\n
<\/a>Permissions<\/h1>\n
<\/a>Calendar name languages<\/h1>\n
<\/a>Loop through each user<\/h1>\n
<\/a>Get calendar in every user mailbox<\/h1>\n
<\/a>Leave permissions if user is exception<\/h1>\n
<\/a>if ($Exception -Contains ($User.UserPrincipalName)) {<\/h1>\n
<\/a>Write-Host “$User is an exception, don’t touch permissions” -ForegroundColor Red<\/h1>\n
<\/a>}<\/h1>\n
<\/a>else {<\/h1>\n
<\/a>Loop through each user calendar<\/h1>\n
\n$CalendarName = $Calendar.Name<\/p>\n<\/a>Check if calendar exist<\/h1>\n
\n$Cal = “$($User.UserPrincipalName):$CalendarName”
\n$CurrentMailFolderPermission = Get-MailboxFolderPermission -Identity $Cal -User Default<\/p>\n<\/a>Set calendar permission / Remove -WhatIf parameter after testing<\/h1>\n
<\/a>Write output<\/h1>\n
\nWrite-Host $User.DisplayName already has the permission $CurrentMailFolderPermission.AccessRights -ForegroundColor Yellow
\n}
\nelse {
\nWrite-Host $User.DisplayName added permissions $Permission -ForegroundColor Green
\n}
\n}
\n}
\n}<\/p>\n<\/a>}<\/h1>\n
<\/p>\n