Share via

Powershell Script - User License Report

Ben Davies 41 Reputation points
2022-01-07T06:45:05.523+00:00

Hey, I have a script which identifies if a user has a specific license and exports the data in a csv. The script works and I get the data I want but when I manipulate the data using a pivot table it doesn't add up. The reason for this is the data is a mix of TRUE, FALSE, BLANK and text (AccountSkuId) so the pivot table doesn't work. Below is the script:

Get-MsolUser -All | where {$.isLicensed -contains $true} | Select-Object DisplayName, {$.Licenses.AccountSkuId -eq "Blah:SPE_F1"}, {$.Licenses.AccountSkuId -eq "Blah:MCOEV"}, {$.Licenses.AccountSkuId -eq "Blah:ENTERPRISEPACK"}, {$_.Licenses.AccountSkuId -eq "Blah:MEETING_ROOM"}, {$_.Licenses.AccountSkuId -eq "Blah:POWER_BI_PRO"}, {$_.Licenses.AccountSkuId -eq "Blah:POWERAUTOMATE_ATTENDED_RPA"}, {$_.Licenses.AccountSkuId -eq "Blah:STANDARDPACK"} | export-csv C:\PS-CSV\LicenseUser1.csv

Is there a way to get round this so its consistent using PowerShell?

Thanks,

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments

5 answers

Sort by: Most helpful
  1. Ben Davies 41 Reputation points
    2022-01-11T04:50:40.39+00:00

    Hi Rich,

    thanks for that.

    I managed to get it running by moving lines 1,2,3 as part of one line. This gives me the data in True or False statements which is great. However I've ran into the same problem as before where the pivot table doesn't work, must be something I need to do in Excel.

    Thanks for your help.

    Ben

    Was this answer helpful?

    0 comments No comments

  2. Ben Davies 41 Reputation points
    2022-01-10T04:10:20.477+00:00

    Hi Rich,
    I've changed it to contains but it still get this error:

    The hash literal was incomplete.

    • CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
    • FullyQualifiedErrorId : IncompleteHashLiteral

    Get-MsolUser -All |
    Where-Object { $.isLicensed -contains $true } |
    Select-Object DisplayName,
    @{n = 'SPE_F1'; e = { $_.Licenses.AccountSkuId -contains "Blah:SPE_F1" } },
    @{n = 'MCOEV'; e = { $.Licenses.AccountSkuId -contains "blah:MCOEV" } },
    @{n = 'ENTERPRISEPACK'; e = { $
    .Licenses.AccountSkuId -contains "Blah:ENTERPRISEPACK" } },
    @{n = 'MEETING_ROOM'; e = { $_.Licenses.AccountSkuId -contains "Blah:MEETING_ROOM" } },
    @{n = 'POWER_BI_PRO'; e = { $_.Licenses.AccountSkuId -contains "Blah:POWER_BI_PRO" } },
    @{n = 'POWERAUTOMATE_ATTENDED_RPA'; e = { $_.Licenses.AccountSkuId -contains "Blah:POWERAUTOMATE_ATTENDED_RPA" },
    @{n = 'STANDARDPACK'; e = { $_.Licenses.AccountSkuId -contains "Blah:STANDARDPACK" } } |
    Export-Csv C:\PS-CSV\LicenseUser14.csv -NoTypeInformation

    thanks,

    Was this answer helpful?


  3. Ben Davies 41 Reputation points
    2022-01-10T03:27:37.157+00:00

    Sorry Rich I'm getting the same error.

    I've just noticed there is an extra } at the end of the script, when I remove this I get a different error:

    The hash literal was incomplete.

    • CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
    • FullyQualifiedErrorId : IncompleteHashLiteral

    thanks,

    Was this answer helpful?


  4. Ben Davies 41 Reputation points
    2022-01-08T01:55:15.787+00:00

    Thanks Rich for your reply much appreciated.

    When I run the above script I get this error:

    Select-Object : The "e" key has a type, System.Object[], that is not valid; expected types are {System.String, System.Management.Automation.ScriptBlock}.
    At line:4 char:10

    • Select-Object DisplayName, @{n='SPE_F1';e={$_.Licenses.Acco ...
    • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    • CategoryInfo : InvalidArgument: (:) [Select-Object], NotSupportedException
    • FullyQualifiedErrorId : DictionaryKeyIllegalValue1,Microsoft.PowerShell.Commands.SelectObjectCommand

    Have I done something wrong?

    Thanks,

    Was this answer helpful?


  5. Rich Matheisen 48,116 Reputation points
    2022-01-07T15:51:03.327+00:00

    I think this is what you want:

    Get-MsolUser -All | 
        Where-Object { $.isLicensed -contains $true } | 
            Select-Object   DisplayName, 
                            @{n = 'SPE_F1'; e = { $_.Licenses.AccountSkuId -eq "Blah:SPE_F1" } }, 
                            @{n = 'MCOEV'; e = { $_.Licenses.AccountSkuId -eq "Blah:MCOEV" } }, 
                            @{n = 'ENTERPRISEPACK'; e = { $_.Licenses.AccountSkuId -eq "Blah:ENTERPRISEPACK" } }, 
                            @{n = 'MEETING_ROOM'; e = { $_.Licenses.AccountSkuId -eq "Blah:MEETING_ROOM" } }, 
                            @{n = 'POWER_BI_PRO'; e = { $_.Licenses.AccountSkuId -eq "Blah:POWER_BI_PRO" } }, 
                            @{n = 'POWERAUTOMATE_ATTENDED_RPA'; e = { $_.Licenses.AccountSkuId -eq "Blah:POWERAUTOMATE_ATTENDED_RPA" } }, 
                            @{n = 'STANDARDPACK'; e = { $_.Licenses.AccountSkuId -eq "Blah:STANDARDPACK" } } | 
                    Export-Csv C:\PS-CSV\LicenseUser1.csv -NoTypeInformation
    

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.