Last active
January 2, 2025 12:01
-
-
Save AlexanderHolmeset/9f50badd912f557550122ba7a0400f01 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Threshold for the last activity | |
| $threshold = 50 | |
| $CopilotLicenseGroupID = "xxxxx-xxx-xxxx-xxx" | |
| # Connect to Microsoft Graph using the Tenant ID and Client Secret Credential | |
| Connect-MgGraph -identity | |
| $users = Invoke-MgGraphrequest -Method GET -Uri "https://graph.microsoft.com/beta/reports/getMicrosoft365CopilotUsageUserDetail(period='D90')" | |
| $count2 = 0 | |
| $today = Get-Date -Format "yyyy-MM-dd" | |
| $UsersNoActivity = @() | |
| foreach ($user in $users.value) { | |
| $count = 0 | |
| $tempuserdata = @() | |
| $tempuserdata = $user | Select-Object "displayName","userPrincipalName","lastactivitydate","microsoftTeamsCopilotLastActivityDate","wordCopilotLastActivityDate","powerPointCopilotLastActivityDate","oneNoteCopilotLastActivityDate","loopCopilotLastActivityDate","outlookCopilotLastActivityDate","excelCopilotLastActivityDate","copilotChatLastActivityDate","reportrefreshdate" | |
| $LicenseDate = @() | |
| $LicenseDate = (Get-MgBetaUser -UserId $tempuserdata.userPrincipalName).assignedplans | where-object{$_.serviceplanid -like "b95945de-b3bd-46db-8437-f2beb6ea2347"} | |
| $LicenseDate = $LicenseDate.AssignedDateTime | Get-Date -Format yyyy-MM-dd | |
| if ((New-TimeSpan -start $LicenseDate -End $today).days -ge $threshold) { | |
| If($tempuserdata.lastactivitydate) { | |
| if ((New-TimeSpan -start $tempuserdata.lastactivitydate -End $today).days -le $threshold) { | |
| $count++ | |
| } | |
| } | |
| else{$count2++} | |
| if($count -eq 0) { | |
| $tempuser= @() | |
| $tempuser = [PSCustomObject]@{ | |
| DisplayName = $tempuserdata.displayName | |
| UserPrincipalName = $tempuserdata.userPrincipalName | |
| LicenseDate = $LicenseDate | |
| Reportrefreshdate = $tempuserdata.reportrefreshdate | |
| LastActivityDate = $tempuserdata.lastactivitydate | |
| MicrosoftTeamsCopilotLastActivityDate = $tempuserdata.microsoftTeamsCopilotLastActivityDate | |
| WordCopilotLastActivityDate = $tempuserdata.wordCopilotLastActivityDate | |
| PowerPointCopilotLastActivityDate = $tempuserdata.powerPointCopilotLastActivityDate | |
| OneNoteCopilotLastActivityDate = $tempuserdata.oneNoteCopilotLastActivityDate | |
| LoopCopilotLastActivityDate = $tempuserdata.loopCopilotLastActivityDate | |
| OutlookCopilotLastActivityDate = $tempuserdata.outlookCopilotLastActivityDate | |
| ExcelCopilotLastActivityDate = $tempuserdata.excelCopilotLastActivityDate | |
| CopilotChatLastActivityDate = $tempuserdata.copilotChatLastActivityDate | |
| } | |
| $UsersNoActivity += $tempuser | |
| } | |
| } | |
| } | |
| $UsersNoActivity = $UsersNoActivity | |
| write-output $UsersNoActivity | |
| write-output $UsersNoActivity.count | |
| write-output "Number of licensedusers wihtout activity: $count2" | |
| <# | |
| $params = @{ | |
| message = @{ | |
| subject = "Copilot Inaktive brukere" | |
| body = @{ | |
| contentType = "TEXT" | |
| content = " | |
| $($UsersNoActivity | Out-String) | |
| " | |
| } | |
| toRecipients = @( | |
| @{ | |
| emailAddress = @{ | |
| address = "brumunddal@alexholmeset.onmicrosoft.com" | |
| } | |
| } | |
| ) | |
| } | |
| saveToSentItems = "false" | |
| } | |
| #> | |
| # A UPN can also be used as -UserId. | |
| #Send-MgUserMail -UserId "brumunddal@alexholmeset.onmicrosoft.com" -BodyParameter $params | |
| foreach($UserNoActivity in $UsersNoActivity){ | |
| $UserObjectID = @() | |
| $UserObjectID = (Get-MgUser -Filter "UserPrincipalName eq '$($UserNoActivity.UserPrincipalName)'").id | |
| Remove-MgGroupMemberByRef -GroupId $CopilotLicenseGroupID -MemberId $UserObjectID | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment