如何使用 Get-AzureADMSConditionalAccessPolicy cmdlet 将 "System.Collections.Generic.List`1[System.String]" 转换为可读格式

How to convert "System.Collections.Generic.List`1[System.String]" into readable format Using Get-AzureADMSConditionalAccessPolicy cmdlet

已编写自定义 PowerShell 脚本来检索 azure ad 条件访问策略信息并将输出保存在 csv 文件中。我正在使用 Get-AzureADMSConditionalAccessPolicy cmdlet queries to achieve that.

某些 Get-AzureADMSConditionalAccessPolicy cmdlet 参数值正在返回 System.Collections.Generic.List`1[System.String] 值,我需要此信息的可读格式或至少 none 如果参数值为空。

下面是我的脚本:


$allpolicies = Get-AzureADMSConditionalAccessPolicy 

$allpolicies | ForEach-Object{

    $policyID = $_.Id
    $policydisplayname = $_.DisplayName
    $policystate = $_.State
    $applications = ($_.Conditions).Applications.IncludeApplications
    $includedusers = ($_.Conditions).Users.IncludeUsers
    $includedgroups = ($_.Conditions).Users.IncludeGroups
    $includedlocation = ($_.Conditions).Locations.includelocations


    New-object -typename PSobject -property @{

        ID = $policyID
        DisplayName = $policydisplayname
        Policy_State = $policystate
        Includedapps =  $applications
        Users_Applied_The_Policy = $includedusers
        Groups_Applied_The_Policy = $includedgroups
        Locations_Applied_The_Policy = $includedlocation


    }

} | Sort-Object ID, DisplayName, Policy_State, Includedapps, Users_Applied_Policy, Groups_Applied_Policy, Locations_Applied | Export-Csv -Path C:\CAccessResults3.csv -NoTypeInformation

在csv文件中输出;

关于运行没有将信息保存到csv文件的脚本,结果不同,下面是截图。

我希望 csv 文件具有 Includedapps, Users_Applied_Policy, Groups_Applied_Policy, Locations_Applied 参数的可读信息。

您可以使用 -join 运算符将列表转换为字符串:

$allpolicies = Get-AzureADMSConditionalAccessPolicy 

$allpolicies | ForEach-Object {
    New-object -typename PSobject -property @{
        ID                           = $_.Id
        DisplayName                  = $_.DisplayName
        Policy_State                 = $_.State
        Includedapps                 = ($_.Conditions).Applications.IncludeApplications -join ';'
        Users_Applied_The_Policy     = ($_.Conditions).Users.IncludeUsers -join ';'
        Groups_Applied_The_Policy    = ($_.Conditions).Users.IncludeGroups -join ';'
        Locations_Applied_The_Policy = ($_.Conditions).Locations.includelocations -join ';'
    }
} | Sort-Object ID, DisplayName, Policy_State, Includedapps, Users_Applied_Policy, Groups_Applied_Policy, Locations_Applied | Export-Csv -Path C:\CAccessResults3.csv -NoTypeInformation

或者你可以输出一个对象 每个条件目标(1 个对象 = 1 行):

$allpolicies = Get-AzureADMSConditionalAccessPolicy 

$allpolicies | ForEach-Object {
    foreach($application in ($_.Conditions).Applications.IncludeApplications){
        New-object -typename PSobject -property @{
            ID                           = $_.Id
            DisplayName                  = $_.DisplayName
            Policy_State                 = $_.State
            TargetType                   = 'Application'
            TargetName                   = $application
        }
    }

    foreach($user in ($_.Conditions).Users.IncludeUsers){
        New-object -typename PSobject -property @{
            ID                           = $_.Id
            DisplayName                  = $_.DisplayName
            Policy_State                 = $_.State
            TargetType                   = 'User'
            TargetName                   = $user
        }
    }

    foreach($group in ($_.Conditions).Users.IncludeGroups){
        New-object -typename PSobject -property @{
            ID                           = $_.Id
            DisplayName                  = $_.DisplayName
            Policy_State                 = $_.State
            TargetType                   = 'Group'
            TargetName                   = $group
        }
    }

    foreach($location in ($_.Conditions).Locations.includelocations){
        New-object -typename PSobject -property @{
            ID                           = $_.Id
            DisplayName                  = $_.DisplayName
            Policy_State                 = $_.State
            TargetType                   = 'Location'
            TargetName                   = $location
        }
    }
} | Sort-Object ID, DisplayName, TargetType, TargetName | Export-Csv -Path C:\CAccessResults3.csv -NoTypeInformation