Beasley66 Posted December 15, 2023 Posted December 15, 2023 (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 December 15, 2023 by Beasley66
network41 Posted December 15, 2023 Posted December 15, 2023 I use Chat GPT to help me with this sort of stuff, Due to the fact I am rubbish at PS
ThomL Posted December 15, 2023 Posted December 15, 2023 What is the script not doing at the moment that you want it to do / what part requires some help?
Beasley66 Posted December 15, 2023 Author Posted December 15, 2023 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]
ThomL Posted December 15, 2023 Posted December 15, 2023 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 ', '}
XiJ Posted December 17, 2023 Posted December 17, 2023 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.
dapaulio Posted December 17, 2023 Posted December 17, 2023 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 1
Beasley66 Posted December 18, 2023 Author Posted December 18, 2023 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.
Beasley66 Posted December 18, 2023 Author Posted December 18, 2023 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.
dapaulio Posted December 19, 2023 Posted December 19, 2023 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now