
FullName -AsByteStream $attachementBytes = :: ToBase64String ( $byteStream ) # request body which contains our message $requestBody = "message" = " } } ) "attachments" = "#" "name" = "" "contentType" = "text/plain" "contentBytes" = $attachementBytes }) } "saveToSentItems" = "true" } # make the graph request $request = "Headers" = Authorization = $msalToken. Path $byteStream = Get-Content -Path $attachementPath. # acquire an access token to interact with the app # we use a certificate from the users personal store $appRegistration = TenantId = "" ClientId = "1f5ffbea-f13f-4f1a-af63-258ce4344daf" ClientCertificate = Get - Item "Cert:\CurrentUser\My\164446192FE04237C206211D59730445CA892911" } $msalToken = Get-msaltoken appRegistration -ForceRefresh # get byte contents of attachement and encode it as base64 $attachementPath = Get-Item $MyInvocation.

We need to take additional steps to limit the permissions of the app registration. Start the script with a variable to hold the groups and then add a foreach loop to go through each of those groups and return the group membership.Simply create a new app registration with the Mail.Send permissions and use a certificate for the authentication.

For this article, we will work with just two groups - administrators and remote desktop users - but you can expand your coverage to other groups by adjusting the script accordingly. On a local server, the list of groups is likely quite short. How to gather local group memberships on Windows Server

First, we need to maintain a list of the groups to monitor, then get the members of each group and finally check for any changes since the last time the script ran. For both Windows servers and Active Directory, the approach to manage these groups will be similar.
