Jump to content

Recommended Posts

Posted (edited)

I have been working on a PS Script that will look in to M365 for a list of users who hasn't logged in for over 90 days and then provide a list of their license so we can see where a potential licenses are being wasted. I have managed to get this far with it at the momenet based on googling and general messing about. If anyone can provide some help that would be great.

 

#Parameters$InactiveDays = 90 $CSVPath = "C:\InactiveUsers.csv"$InactiveUsers = @()$ThresholdDate = (Get-Date).AddDays(-$InactiveDays) #Connect to Microsoft GraphConnect-MgGraph -Scopes "AuditLog.Read.All", "User.Read.All" -NoWelcome #Set the Graph Profile#Select-MgProfile Beta #Properties to Retrieve$Properties = @(    'Id','DisplayName','Mail','UserPrincipalName','UserType', 'AccountEnabled', 'SignInActivity', 'Department',  'Created', 'AssignedLicenses'  ) #Get All users along with the properties$AllUsers = Get-MgUser -All -Property $Properties ForEach ($User in $AllUsers){    $UserLicenseDetails = Get-MgUserLicenseDetail -UserId $user.Id    $UserLicenses = $UserLicenseDetails | ForEach-Object {        ($_.ServicePlans | ForEach-Object { $_.SkuPartNumber }) -join ', '    }     $LastLoginDate = $User.SignInActivity.LastSignInDateTime    If($LastLoginDate -eq $null)    {        $LastSignInDate = "Never Signed-in!"    }    Else    {        $LastSignInDate = $LastLoginDatel    }     #Collect data    If(!$LastLoginDate -or ($LastLoginDate -and ((Get-Date $LastLoginDate) -lt $ThresholdDate)))    {        $InactiveUsers += [PSCustomObject][ordered]@{            LoginName       = $User.UserPrincipalName            Email           = $User.Mail            DisplayName     = $User.DisplayName            UserType        = $User.UserType            AccountEnabled  = $User.AccountEnabled            LastSignInDate  = $LastSignInDate            Department      = $User.Department            License         = $UserLicense        }    }} $InactiveUsers#Export Data to CSV$InactiveUsers | Export-Csv -Path $CSVPath -NoTypeInformation

Edited by Beasley66
Posted

So it currently it provides me the list of users who havent used their account for 90 day but the license part is just returning "[TABLE=width: 64]

[TR]

[TD=width: 64]Microsoft.Graph.PowerShell.Models.MicrosoftGraphLicenseDetails" which is next to useless.

 

[/TD]

[/TR]

[/TABLE]

Posted

I can't test in my tenancy - seems like a licensing issue. Are you getting "Microsoft.Graph.PowerShell.Models.MicrosoftGraphLicenseDetails" in the output of $InactiveUsers?

so $InactiveUsers[0].License returns "Microsoft.Graph.PowerShell.Models.MicrosoftGraphLicenseDetails"?

 

If so I'd be debugging this area as it builds the variable that's used in the object ($UserLicenses) - maybe throw in a write-host $UserLicense after the variable is set as some quick and dirty testing :

$UserLicenseDetails = Get-MgUserLicenseDetail -UserId $user.Id
$UserLicenses = $UserLicenseDetails | ForEach-Object { ($_.ServicePlans | ForEach-Object { $_.SkuPartNumber }) -join ', '} 

Posted
Also check your scopes are correct for what you want to access. I’m in early days of using graph but I hit this a few times.
Posted
I think you can export all your users out of m365 gui interface and it gives a csv with lots of information more than you really need. Includes livense and last logged in
  • Thanks 1
Posted
I've had a look at this and you are able to export a list of users who haven't been logged in for the last 90 days but it doesn't give you a their assigned license.
Posted
I have been working on a new script that i found while continually looking for a solution, it is looking promising but taking a while to run so ill get back to you on that when its finished.
Posted
I've had a look at this and you are able to export a list of users who haven't been logged in for the last 90 days but it doesn't give you a their assigned license.

 

I’m almost certain you can, I just used it last week. If you go in to portal.office.com. Log on with an admin user and navigate to admin console. Expand users. Then export active users ( which actually means all in m365)

 

It will give you a csv.

Bit of excel wizardry and hey presto

 

Hope this helps

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...