|
| 1 | +$TenantId = "TENANT_ID" |
| 2 | +$ClientId = "CLIENT_ID" |
| 3 | +$CertThumbprint = "CERT_THUMBPRINT" |
| 4 | +$AdminMail = "ADMIN_MAIL" |
| 5 | + |
| 6 | +# Consent to permissions using app created in the last demo. |
| 7 | +$AdminConsentUrl = "https://login.microsoftonline.com/$TenantId/adminconsent?client_id=$ClientId" |
| 8 | +Write-Host -ForeGroundColor Yellow "Please go to the following URL in your browser to provide admin consent" |
| 9 | +Write-Host $AdminConsentUrl |
| 10 | +Write-Host "Press any key to continue when done ....." |
| 11 | +$Key = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") |
| 12 | + |
| 13 | +# Authenticate as a confidential client for app only calls. |
| 14 | +Connect-Graph -ClientId $ClientId -TenantId $TenantId -CertificateThumbprint $CertThumbprint |
| 15 | + |
| 16 | +# Get group named 'Weekday Employees' |
| 17 | +$WeekdayEmployeesGroup = Get-MgGroup -Filter "DisplayName eq 'Weekday Employees'" |
| 18 | + |
| 19 | +# Get Members of the group. |
| 20 | +$GroupMembers = Get-MgGroupMember -GroupId $WeekdayEmployeesGroup.Id |
| 21 | +$ToRecipients = @() |
| 22 | +foreach ($member in $GroupMembers) { |
| 23 | + $User = Get-MgUser -UserId $member.Id -Select "displayName", "mail", "mailboxSettings" | ` |
| 24 | + Select DisplayName, Mail, MailboxSettings |
| 25 | + |
| 26 | + # Get users with invalid workdays in their mailbox setting. |
| 27 | + if ( $User.MailboxSettings.WorkingHours.DaysOfWeek -contains "saturday" || ` |
| 28 | + $User.MailboxSettings.WorkingHours.DaysOfWeek -contains "sunday" ) { |
| 29 | + Write-Host -ForegroundColor Yellow "User "$User.DisplayName" has an invalid workday." |
| 30 | + |
| 31 | + $ToRecipients += @{ |
| 32 | + emailAddress = @{ |
| 33 | + name = $User.DisplayName; |
| 34 | + address = $User.Mail |
| 35 | + }} |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +if ($ToRecipients.Length) { |
| 40 | + # Compose message. |
| 41 | + $Message = @{ |
| 42 | + subject = "Update Your Mailbox Settings!"; |
| 43 | + toRecipients = $ToRecipients; |
| 44 | + body = @{ |
| 45 | + contentType = "Text"; |
| 46 | + content = "Please update to your mailbox settings to reflect your working hours." + ` |
| 47 | + " You currently have Saturday and/or Sunday set as workday." |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + $Admin = Get-MgUser -UserId $AdminMail |
| 52 | + |
| 53 | + # Send mail to users with invalid workdays. |
| 54 | + Send-MgUserMail -UserId $Admin.Id -BodyParameter @{message = $Message} |
| 55 | + Write-Host -ForegroundColor Green "Mail sent to affected users." |
| 56 | +} |
| 57 | + |
| 58 | +Disconnect-Graph |
0 commit comments