0

Get-MsolSubscription gives me total number of licenses

$Result=""   
$Results=@()   
$Subscriptions= Get-MsolSubscription | foreach{
 $SubscriptionName=$_.SKUPartNumber
 $SubscribedOn=$_.DateCreated
 $ExpiryDate=$_.NextLifeCycleDate
 $Status=$_.Status
 $TotalLicenses=$_.TotalLicenses
 $Result=@{'Subscription Name'=$SubscriptionName;'Total Licenses'=$TotalLicenses}
 $Results+= New-Object PSObject -Property $Result
 }

Above code works fine, now i want to combine output of above command with output of Get-MsolAccountSku because that command gives me number of actually used licenses

So i modified code as below:

$Result=""   
$Results=@()   
$Subscriptions= Get-MsolSubscription | foreach{
 $SubscriptionName=$_.SKUPartNumber
 $SubscribedOn=$_.DateCreated
 $ExpiryDate=$_.NextLifeCycleDate
 $Status=$_.Status
 $TotalLicenses=$_.TotalLicenses
 $consumedLicences = Get-MsolAccountSku | foreach {
  $consumed = $_.ConsumedUnits
  $name = $_.SkuPartNumber
  } | where {$_.name -like $SubscriptionName }
 $Result=@{'Subscription Name'=$SubscriptionName;'Total Licenses'=$TotalLicenses;'Used'=$consumed}
 $Results+= New-Object PSObject -Property $Result
 }

Problem is that for all licences i'm getting same value for used column

Total Licenses Subscription Name              Used
-------------- -----------------              ----
            48 VISIOCLIENT                       9
            16 VISIOCLIENT                       9
         10000 STREAM                            9
            50 EMSPREMIUM                        9
         10000 POWERAPPS_INDIVIDUAL_USER         9

2 Answers 2

2

Your foreach block after Get-MsolAccountSku never outputs anything, but it always overwrites $consumed on each iteration - hence the repeated count.

Change it to:

$Results = Get-MsolSubscription | ForEach-Object {
    # Get the part number and total licenses
    $SubscriptionName = $_.SKUPartNumber
    $TotalLicenses = $_.TotalLicenses

    # Count used licenses
    $consumedLicences = Get-MsolAccountSku | Where-Object {
        $_.SKUPartNumber -like $SubscriptionName
    } | ForEach-Object {
        $_.ConsumedUnits
    } |Measure-Object -Sum |Select-Object -Expand Sum
    
    # Output resulting object 
    [PSCustomObject]@{
        'Subscription Name' = $SubscriptionName
        'Total Licenses' = $TotalLicenses
        'Used' = $consumedLicences
    }
}

I took the liberty of simplifying your code slightly in the process, but the meat of it is this:

$consumedLicences = Get-MsolAccountSku | Where-Object {
    $_.SKUPartNumber -like $SubscriptionName
} | ForEach-Object {
    $_.ConsumedUnits
} |Measure-Object -Sum |Select-Object -Expand Sum

Here, we apply the filtering with Where-Object before doing anything else - while each output object from Get-MsolAccountSku still has it's properties intact. Then we use ForEach-Object to extract the value of ConsumedUnits, and finally we use Measure-Object to sum all counts (it's unclear to me whether Get-MsolAccountSku |Where-Object ... results in multiple matches).

If there's only one, you can either leave it as is (sum of one value is just the value it self), or you can remove everything after ForEach-Object { ... } completely:

$consumedLicences = Get-MsolAccountSku | Where-Object {
    $_.SKUPartNumber -like $SubscriptionName
} | ForEach-Object {
    $_.ConsumedUnits
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for your effort, i don't need sum, just need ConsumedUnit property for every license, i changed $consumedLicences = Get-MsolAccountSku | Where-Object { $_.Name -like $SubscriptionName } | ForEach-Object { $_.ConsumedUnits } | Select-Object -ExpandProperty ConsumedUnits and getting nothing as output for consumedLicences. With your original code getting 0
Remove Select-Object -Expand... part, it's not needed - since we dereference $_.ConsumedUnits, the output of ForEach-Object is the raw numerical value. In other words, there's no properties to expand
thanks, it works now, it was typo in your answer, i'll post whole code as another answer. Thank you !
@overflowed Great! Please let me know where the typo is (or suggest an edit to my answer) so it can be corrected :-)
$_.Name -like $SubscriptionName should be $_.SkuPartNumber -like $SubscriptionName
0

Thanks to @Mathias R. Jessen, here is complete code which works fine now:

$Result=""   
$Results=@()   
$Subscriptions= Get-MsolSubscription | foreach{
 $SubscriptionName=$_.SKUPartNumber
 $SubscribedOn=$_.DateCreated
 $ExpiryDate=$_.NextLifeCycleDate
 $Status=$_.Status
 $TotalLicenses=$_.TotalLicenses
 $consumedLicences = Get-MsolAccountSku | Where-Object {
        $_.SkuPartNumber -like $SubscriptionName
    } | ForEach-Object {
        $_.ConsumedUnits
    }
 $Result=@{'Subscription Name'=$SubscriptionName;'Total Licenses'=$TotalLicenses;'Used'=$consumedLicences}
 $Results+= New-Object PSObject -Property $Result
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.