【问题标题】:Powershell Cascading Foreach to export to CsvPowershell 级联 Foreach 以导出到 Csv
【发布时间】:2022-01-03 17:30:33
【问题描述】:

我正在将我的 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 专家。

【问题讨论】:

标签: powershell azure-powershell


【解决方案1】:

按照您对底线的要求,请参阅以下脚本调整:

$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 *,因为它在大多数情况下会提取大部分属性,并且在代码中看起来更整洁。)

【讨论】:

  • DisplayName 只是我忘记删除的第一个测试字段,我们必须在 Street(Address) 等其他字段上执行此操作不确定,我们有一些带引号等等...
猜你喜欢
  • 2016-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-10
  • 1970-01-01
  • 2015-01-17
  • 2014-12-05
  • 2017-12-31
相关资源
最近更新 更多