I am trying to automate disabling Microsoft Teams licenses for users not connected for more than 180 days.
In order to achieve this, I currently try to extract a report of users by signin activity, and then based on this select those with an activity greater than 6 months and remove their license.
My current (not working) code is the following:
get-mguser -All `
-property signinactivity | Select-Object DisplayName -expandproperty signinactivity | Export-CSV "C:\temp\LastActivity.csv" -NoTypeInformation
$InputFile = "c:\temp\LastActivity.csv"
[array]$Users = Import-CSV $InputFile
If (LastSignInDateTime -gt 6 months)
ForEach ($User in $Users)
{
try
{
$License = Set-MgUserLicense -UserId $User.UPN -AddLicenses @() -RemoveLicenses @('18181a46-0d4e-45cd-891e-60aabd171b4e') -ErrorAction:Continue;
}
catch
{
Write-Warning -Message "Script failed while attempting to set the licence for $($User.UPN). Aborting.";
throw;
}
}
I see two main issues: first is the signinactivity I obtain which is not the Teams last signinactivity. The second is the command If (LastSignInDateTime -gt 6 months) that obviously does not work as is, I do not find how to make it work based on the report generated previsouly.
Does anyone see how I can tweak my code to achieve the desired result?