Reputation: 487
I'm trying to get output from O365 via PowerShell for users with specific product licenses assigned.
There's plenty of articles out there of how to get this information but not found information explaining how to grab users with multiple specified products.
So far I have the following (taken from here):
Get-MsolUser -All |
Where-Object {
($_.licenses).AccountSkuId -match "EXCHANGEENTERPRISE" `
-and ($_.licenses).AccountSkuId -match "EMS" `
-and ($_.licenses).AccountSkuId -notmatch "ENTERPRISEPACK"
} |
Select-Object displayname,userprincipalname,{$_.Licenses.AccountSkuId} |
Export-Csv "C:\Temp\EOP2+EMSLicensedUsers.csv" -NoTypeInformation
However, this still shows users who have ENTERPRISEPACK assigned.
Upvotes: 0
Views: 3277
Reputation: 338376
PowerShell operators do not work like you think they do.
.AccountSkuId
is an array of values. Doing AccountSkuId -notmatch "ENTERPRISEPACK"
does not tell you whether "ENTERPRISEPACK"
is contained in that array or not. It gives you all the values from that array that do not match "ENTERPRISEPACK"
.
It's a filter. Try executing "1","2","3" -notmatch "3"
to see what I mean.
Therefore, if even one value in AccountSkuId
does not match "ENTERPRISEPACK"
, you still get some values back, and "some values" (i.e. a non-empty list) evaluates to $true
in a Boolean expression.
You wanted to write this:
Get-MsolUser -All |
Where-Object {
($_.licenses).AccountSkuId -match "EXCHANGEENTERPRISE"
-and ($_.licenses).AccountSkuId -match "EMS"
-and -not (($_.licenses).AccountSkuId -match "ENTERPRISEPACK")
} |
Select-Object displayname,userprincipalname,{$_.Licenses.AccountSkuId} |
Export-Csv "C:\Temp\EOP2+EMSLicensedUsers.csv" -NoTypeInformation
Note the change. ($_.licenses).AccountSkuId -match "ENTERPRISEPACK"
gives you all values that match "ENTERPRISEPACK"
(normally 1 or 0) and the -not
simply negates that result.
Other things to try with PowerShell operators:
1,2,2,3 -eq 2
1,2,2,3 -ne 2
1,2,2,3 -gt 1
"hallo","hello","foo" -like "*ll*"
"hallo","hello","foo" -replace 'l','L'
Keep in mind that PowerShell operates on lists when it can. A single value is nothing but a list of length 1.
Upvotes: 2