[英]Export array with “sub-array”
我目前正在嘗試使用PowerShell在多個合作伙伴租戶中自動化Office 365中的許可證計數。
我當前的代碼(從互聯網上獲得)經過一些修改后,輸出如下:
Column A Column B Column C -------- -------- -------- CustA LicA,LicB 1,3 CustB LicA,LicB 7,3 CustC LicA 4
但是我想要從此代碼輸出是:
Column A Column B Column C -------- -------- -------- CustA LicA 1 LicB 3 CustB LicA 7 LicB 3
這是我當前使用Export-Csv -NoType
:
$tenantID = (Get-MsolPartnerContract).tenantid
foreach($i in $tenantID){
$tenantName = Get-MsolPartnerInformation -TenantId $i
$tenantLicense = Get-MsolSubscription -TenantId $i
$properties = [ordered]@{
'Company' = ($tenantName.PartnerCompanyName -join ',')
'License' = ($tenantLicense.SkuPartNumber -join ',')
'LicenseCount' = ($tenantLicense.TotalLicenses -join ',')
}
$obj = New-Object -TypeName psobject -Property $properties
Write-Output $obj
}
我已經嘗試過將其與其他幾次代碼迭代一起徹底失敗:
$properties = [ordered]@{
'Company' = ($tenantName.PartnerCompanyName -join ','),
@{'License' = ($tenantLicense.SkuPartNumber -join ',')},
@{'LicenseCount' = ($tenantLicense.TotalLicenses -join',')}
}
我當時正在考慮制作一個“子數組” $tenantLicense.SkuPartnumber
和$tenantLicense.TotalLicenses
,但是我不太確定如何將其附加到對象或“主數組” $tenantLicense.TotalLicenses
。
每個$tenantLIcense
第二個循環將為您解決問題。 我無法訪問您這樣的環境,因此無法測試。
$tenantID | ForEach-Object{
$tenantName = Get-MsolPartnerInformation -TenantId $_
$tenantLicense = Get-MsolSubscription -TenantId $_
# Make an object for each $tenantLicense
$tenantLicense | ForEach-Object{
$properties = [ordered]@{
'Company' = $tenantName.PartnerCompanyName
'License' = $_.SkuPartNumber
'LicenseCount' = $_.TotalLicenses
}
# Send the new object down the pipe.
New-Object -TypeName psobject -Property $properties
}
}
由於您有多個$tenantLicenses
具有相同的公司名稱,因此,我們只需將它們循環並在輸出中使用相同的公司名稱即可。 假定此方法有效,將不會獲得所需的輸出,因為在隨后的行中沒有省略公司的邏輯。 我認為這樣做更好,因為您現在可以對數據進行排序而不會丟失數據/理解。
注意,我將foreach()
更改為ForEach-Object
。 這樣可以更輕松地將對象發送到管道中。
如果沒有提供代碼解決方案,我可以說您需要構建陣列。 在編程方面,您將需要迭代數組ARRAY1,並用多余的行填充另一個ARRAY2。 例如,如果列A,B是簡單值並且C是3個項目的數組,那么您將在新表中添加3行,其中A,B,C1
, A,B,C2
和A,B,C3
。 在循環的每次迭代中,您需要計算所有排列,例如,在您的情況下,由columnB和columnC生成排列。
使用ForEach-Object
cmdlet進行流水線操作也應該可行,但這更加困難,並且正如您提到的與powershell的相對較新的關系一樣,除非您想粗略學習,否則我不會走這條路。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.