Home > Enterprise >  Create row from Count Number
Create row from Count Number

Time:09-27

Hy,

im a powershell beginner...so im about to read Office 365 licenses like this:

$licensetype = Get-MsolAccountSku | Where {$_.ConsumedUnits -ge 1}
AccountSkuId ActiveUnits WarningUnits ConsumedUnits
reseller-account:EXCHANGEDESKLESS 10 0 10
reseller-account:SPB 39 0 38
reseller-account:EXCHANGESTANDARD 189 0 187
reseller-account:O365_BUSINESS_PREMIUM 51 0 51
reseller-account:AAD_PREMIUM 1 0 1
reseller-account:O365_BUSINESS_ESSENTIALS 31 0 27

now i need a result for free licenses like this: ($lic.ActiveUnits-$lic.ConsumedUnits)

License Free
SPB
EXCHANGESTANDARD
EXCHANGESTANDARD
EXCHANGESTANDARD
O365_BUSINESS_ESSENTIALS
O365_BUSINESS_ESSENTIALS
O365_BUSINESS_ESSENTIALS
O365_BUSINESS_ESSENTIALS

i tried some foreach params, but without luck...any ideas?

CodePudding user response:

assuming the get-msolaccountssku returns you objects with the properties as declared in the first table you can do:

#Get Lincense Info
$licensetype = Get-MsolAccountSku | Where {$_.ConsumedUnits -ge 1}

$result = @(
    foreach ($lt in $licensetype){
        $attrsHt = [ordered]@{
            AccountSkuId = $lt.AccountSkuId
            ActiveUnits = $lt.ActiveUnits
            WarningUnits = $lt.WarningUnits
            ConsumedUnits = $lt.ConsumedUnits
            Free = ([int]($lt.ActiveUnits) - [int]($lt.ConsumedUnits))
        }
        New-Object -TypeName psobject -Property $attrsHt
    }
)

The variable $result contains the information. Or you could also achieve the same by using calculated property in conjunction with select-object:

$licenseInfo =  Get-MsolAccountSku | Where {$_.ConsumedUnits -ge 1} | Select-Object AccountSkuId,ActiveUnits,WarningUnits,ConsumedUnits,@{Name='Free';Expression={$_.ActiveUnits - $_.ConsumedUnits}}

CodePudding user response:

$licensefree =  Get-MsolAccountSku | Where {($_.ConsumedUnits -ge 1) -and 
($_.AccountSkuId -ne "reseller-account:FLOW_FREE") -and (($_.ActiveUnits - 
$_.ConsumedUnits) -ne 0)}  | Select-Object 
AccountSkuId,@{Name='Free';Expression={$_.ActiveUnits - $_.ConsumedUnits}}


$explodedList = foreach($License in $licensefree) {
for($i = 0; $i -lt $licensefree.Free; $i  ){
    # Create a copy of the object
    [pscustomobject]@{
        License = $licensefree.License
        Free = ''
    }
}
}
$explodedList

Output ERROR:

"0" konnte nicht mit "1 2 4" verglichen werden. Fehler: "Der Wert "System.Object[]" vom Typ "System.Object[]" kann nicht in den Typ "System.Int32" konvertiert werden."
In Zeile:2 Zeichen:17
      for($i = 0; $i -lt $licensefree.Free; $i  ){
                  ~~~~~~~~~~~~~~~~~~~~~~~~
      CategoryInfo          : InvalidOperation: (:) [], RuntimeException
      FullyQualifiedErrorId : ComparisonFailure
 
  • Related