Powershell 级联 Foreach 导出到 Csv

Powershell Cascading Foreach to export to Csv

我正在将我的 AAD 用户导出到 CSV 文件,使用此代码可以正常工作。

$allUsers = Get-AzureADUser -All $true

$users = $allUsers |
              Select-Object -Property ObjectId,ObjectType,UserPrincipalName,DisplayName,AccountEnabled,AgeGroup,City,CompanyName,ConsentProvidedForMinor,Country,CreationType,Department,DirSyncEnabled,FacsimileTelephoneNumber,GivenName,IsCompromised,ImmutableId,JobTitle,LastDirSyncTime,LegalAgeGroupClassification,Mail,MailNickName,Mobile,OnPremisesSecurityIdentifier,PasswordPolicies,PhysicalDeliveryOfficeName,PostalCode,PreferredLanguage,RefreshTokensValidFromDateTime,ShowInAddressList,State,StreetAddress,Surname,TelephoneNumber,UsageLocation,UserState,UserStateChangedOn,UserType,DeletionTimestamp,AssignedLicenses,AssignedPlans,ProvisionedPlans |
              ForEach-Object{
                $_.DisplayName = $_.DisplayName -replace "\n", ' ' -replace '"', '`'
                $_
                }
$users | Export-Csv -Path $TempFileName -NoTypeInformation


$provisionedPlans = $users = $allUsers |
              Select-Object -Property ObjectId,DisplayName,ProvisionedPlans

但是,ProvisionedPlans 以列表的形式出现,所以我想将列表中的每个条目导出为 1 行。 这是字段示例

ProvisionedPlans               : {class ProvisionedPlan {
                                   CapabilityStatus: Enabled
                                   ProvisioningStatus: Success
                                   Service: MicrosoftCommunicationsOnline
                                 }
                                 , class ProvisionedPlan {
                                   CapabilityStatus: Deleted
                                   ProvisioningStatus: Success
                                   Service: MicrosoftCommunicationsOnline
                                 }
                                 , class ProvisionedPlan {
                                   CapabilityStatus: Deleted
                                   ProvisioningStatus: Success
                                   Service: MicrosoftCommunicationsOnline
                                 }
                                 , class ProvisionedPlan {
                                   CapabilityStatus: Enabled
                                   ProvisioningStatus: Success
                                   Service: SharePoint
                                 }
                                 ...}

所以我希望在输出中看到的底线是

ObjectId,DisplayName,CapabilityStatus,ProvisioningStatus,Service
id1,User1,Enabled,Success,MicrosoftCommunicationsOnline
id1,User1,Deleted,Success,MicrosoftCommunicationsOnline
id1,User1,Deleted,Success,MicrosoftCommunicationsOnline
id1,User1,Enabled,Success,SharePoint
id2,User2,Enabled,Success,Whatever

请放心,我不是 Powershell 专家。

根据您对底线的要求,请查看以下对脚本的调整:

$allUsers = Get-AzureADUser -All $true

$Results = Foreach ($user in $allusers){
    Foreach($Plan in $user.ProvisionedPlans){
        $Plan | Select @{Name = 'ObjectId';  Expression = {$user.Objectid}},@{Name = 'DisplayName';Expression = {$user.DisplayName}},CapabilityStatus,ProvisioningStatus,Service
    }
}

$Results | Export-Csv -Path $TempFileName -NoTypeInformation

我不确定您为什么要替换 DisplayName 中的字符,但我已将其添加到 Select Statemant 中的计算属性。

我也在每个提供的计划的迭代中这样做,因为这似乎是最简单的方法。

我已经删除了包含所有属性的初始 select,因为正在导出您感兴趣的属性。 (如果您需要所有属性,我建议您使用 Select *,因为在大多数情况下它会提取大部分属性,并且在代码中看起来更整洁。)