如何使用<new-object -typename =“” psobject =“” -property =“”>收集有关2 foreach的信息

时间:2018-10-11 16:16:00

标签: powershell active-directory

我需要汇总2 foreach的结果,这样我才能运行一个不错的报告-这是脚本:

$users = Get-ADGroupMember -Identity 'TESTGRoup'
$result1 = ForEach ($user in $users){

    New-Object -TypeName psobject -Property @{
        User   = $user
        CN     = $User.SamAccountName
        Domain = $user.name
        Name   = $user.SID
    }
}

$Result4 = Get-ADGroupMember 'TESTGRoup' |
    Where-Object {$_.objectClass -eq 'User'} |
    Get-AdUser |
    ForEach {
        Get-MsolUser -UserPrincipalName $_.UserPrincipalName | Select UserprincipalName, Blockcredential  
        $result7 = New-Object -TypeName psobject -Property @{
            User1   = $Result4.UserPrincipalName
            CN1     = $Result4.BlockCredential
        }
    }

最终结果是所有用户都在同一行而不是在表上。

很抱歉,代码格式-不确定如何更好地展示它。

谢谢

1 个答案:

答案 0 :(得分:0)

如果我正确理解了您的问题,下面的代码将使用每个用户一个对象上的所需汇总数据生成对象:

#requires -Version 3
$results = foreach ($user in Get-ADGroupMember -Identity 'TESTGRoup')
{
    $user | ? objectClass -eq User | Get-ADUser | % {
        $msol = Get-MsolUser -UserPrincipalName $PSItem.UserPrincipalName

        [pscustomobject]@{
            'User'   = $user
            'CN'     = $user.SamAccountName
            'Domain' = $user.Name
            'Name'   = $user.SID
            'User1'  = $msol.UserPrincipalName
            'CN1'    = $msol.BlockCredential
        }
    }
}