I am trying to get the amount of spare licenses available from the Get-MsolAccountSku
command.
Here's the code (output below)
$Licenses = Get-MsolAccountSku
$spare = Foreach ($License in $licenses)
{
($License.ActiveUnits - $License.ConsumedUnits)
}
Get-MsolAccountSku | Select-Object -Property AccountSkuId,ActiveUnits,ConsumedUnits,@{L=’SpareLicenses’;E={$spare}}
I want to add a column to the right of the output to list the amount of licenses available from the subtraction in the ForEach
loop.
ActiveUnits ConsumedUnits
----------- -------------
30 26
1601 1
30 29
25 0
5 3
1 0
12550 12465
1000000 12461
12550 12466
12555 12468
31 19
12550 12464
CodePudding user response:
Your $spare
object is not needed, just update the Select to do the calculation...
Get-MsolAccountSku |
Select-Object -Property AccountSkuId,ActiveUnits,ConsumedUnits,
@{L=’SpareLicenses’;E={$_.ActiveUnits - $_.ConsumedUnits}}
CodePudding user response:
From about Calculated Properties:
The calculated property is defined by a hashtable containing key-value pairs that specify the name of the new property, an expression to calculate the value, and optional formatting information.
expression
- A script block used to calculate the value of the new property.
Following your code, you should change the foreach
loop for a scriptblock:
$Licenses = Get-MsolAccountSku
$spare = { $_.ActiveUnits - $_.ConsumedUnits }
# $_ - References to each object being passed through the pipeline
# See about Automatic Variables for more information
Get-MsolAccountSku |
Select-Object -Property AccountSkuId,ActiveUnits,ConsumedUnits,@{
Name = 'SpareLicenses'
Expression = $spare
}