I'm working on transitioning some reporting tasks from ExchangeOnlineManagement to MSGraph due to issues with the former. Specifically, I'm trying to pull a report for users with the job title 'Sales' over the last three days. I've figured out how to connect to MSGraph and fetch email activities, but I need help filtering those activities based on the users in the 'Sales' group. I can retrieve users using get-mguser, but I'm uncertain how to combine that with get-mgreportemailactivityuserdetail to get activities specific to this group. Any advice on how to make this work in PowerShell would be greatly appreciated!
2 Answers
It looks like you've got both the sales users in `$sales` and the email activities in `$activity`. To create a report just for the sales team, you could do something like this:
```powershell
$salesActivity = @()
ForEach ($user in $sales) {
$salesActivity += $activity | Where-Object { $_.'User Principal Name' -eq $user.UserPrincipalName }
}
```
This will collect all activity data for the users you've filtered for. Just remember that `@()` initializes an empty array that will hold the results from your sales users' activity!
You might want to check if you have different variations of the term 'Sales'. Using regex can be tricky, and it may be easier just to use `-eq` for an exact match. Are you looking to get all activities for sales employees across those three days?
Using `+=` and `@()` can be a bit slow, though! You might want to optimize it like this:
```powershell
$salesActivity = ForEach ($user in $sales) {
$activity | Where-Object { $_.'User Principal Name' -eq $user.UserPrincipalName }
}
```
Also, does `'User Principal Name'` really have spaces? Just make sure that's correct!