Avatar of K B
K B
Flag for United States of America asked on

PowerShell: Why is this variable causing Microsoft.Online.Administration.AccountSkuDetails ?

I am presuming line #8 ($Bsku) is the origin of the issue.

$licensetype  = Get-MsolAccountSku
$AccountSkuId = $licensetype.accountskuid
$object = New-Object -TypeName PSObject
for($i = 0; $i -lt $AccountSkuId.count; $i++) {
    $Bsku = Get-MsolAccountSku | ? {$_.accountskuid -eq $AccountSkuId[$i]}
    for($j = 0; $j -lt $Bsku.ServiceStatus.count; $j++){
        $aservice = $Bsku.ServiceStatus[$j].ServicePlan.ServiceName
        $object | Add-Member -MemberType NoteProperty -Name AccountSkuID -Value $Bsku
        $object | Add-Member -MemberType NoteProperty -Name Service -Value $aservice
        Write-Output $object
    }
}   

Open in new window


2017-03-15_1111.png
PowershellMicrosoft 365Active DirectoryExchange.NET Programming

Avatar of undefined
Last Comment
K B

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
oBdA

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
K B

ASKER
You are awesome thank you!
oBdA

Just had a closer look at your script, trying to understand what it is you're trying to do, instead of just fixing the obvious error.
Seems like you're trying to get a list of services for each SKU, only in a very complicated way? You already have everything you need in $LicenseType, so no need to run Get-MsolAccountSku over and over again.
Anyway, I can't test these, but I think they should do what you want.
Using the pipeline for real, in the spirit of Powershell:
Get-MsolAccountSku | ForEach-Object {
	$AccountSkuId = $_.AccountSkuId
	$_.ServiceStatus | Select-Object -Property @{n='AccountSkuId'; e={$AccountSkuId}}, @{n='Service'; e={$_.ServicePlan.ServiceName}}
}

Open in new window

Or, even shorter, but using a loop statement instead of the pipeline:
ForEach ($Sku In (Get-MsolAccountSku)) {
	$Sku.ServiceStatus | Select-Object -Property @{n='AccountSkuId'; e={$Sku.AccountSkuId}}, @{n='Service'; e={$_.ServicePlan.ServiceName}}
}

Open in new window

K B

ASKER
Thank you.  I know I can learn from that.. I will study it.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes