question

Ragz-2119 avatar image
0 Votes"
Ragz-2119 asked ·

Export Foreach-Object Powershell

Hi ,

Need to export the below code to csv

Get-MsolAccountSku | ForEach-Object {
Write-Host "`n"$.AccountSkuId
Write-Host "`t* SKUId:"$
.SkuId
$result = $.ServiceStatus | ForEach-Object {
Write-Host "`t`t+" $
.ServicePlan.ServiceName
Write-Host "`t`t`t- ServiceType:" $.ServicePlan.ServiceType
select $
.ServicePlan.ServiceName
}
}

$result | export-CSV 'e:\temp\Chart.csv'

windows-server-powershell
· 1
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Aren't you already doing that with this code? Is there a problem?

0 Votes 0 ·
RanaBanerjee avatar image
0 Votes"
RanaBanerjee answered ·

What is your main objective, to try and get licence information for each user?

· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered ·

Hi,

There's no object piped to select so the output is null. And the inner for-each loop is unnecessary as each object has only one ServiceStatus property.

 $result = Get-MsolAccountSku | ForEach-Object {
     Write-Host "`n"$_.AccountSkuId 
     Write-Host "`t* SKUId:"$_.SkuId
     Write-Host "`t`t+" $_.ServiceStatus.ServicePlan.ServiceName
     Write-Host "`t`t`t- ServiceType:" $_.ServiceStatus.ServicePlan.ServiceType
     $_.ServiceStatus.ServicePlan.ServiceName
 } 
 $result | export-CSV 'e:\temp\Chart.csv'

Best Regards,
Ian
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

BillStewart avatar image
0 Votes"
BillStewart answered ·

Keep in mind that Write-Host does not produce output to the pipeline (it goes to the host only, so there's nothing to export).

· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.