重复输出Pso对象

时间:2017-11-24 06:09:49

标签: powershell active-directory

我正在组合Get-ADUserGet-ADComputer,但在环境中我们有7台计算机和60个用户,输出会复制每个用户3-5,感谢任何帮助。

$outtbl = @()
$users = Get-ADUser -Filter * -Properties displayname, lastlogondate

foreach ($user in $users) {
    $x = Get-ADComputer -Filter * -Properties operatingsystem, lastlogondate |
    Select-Object name, lastlogondate, operatingsystem

    foreach ($x in $x) {
        $t = New-Object PSObject -Property @{
            SamAccountName  = $user.SamAccountName
            UserDisplayname = $user.displayname
            ComputerName    = $x.Name
            operatingsystem = $x.operatingsystem
            Clastlogondate  = $x.lastlogondate
            Userlastlogon   = $user.lastlogondate
        }

        $outtbl += $t |
                   Select-Object userdisplayname, Samaccountname, userlastlogon,
                       Computername, clastlogondate, operatingsystem |
                   Export-Csv C:\result.csv -NoTypeInformation -Append
    }
}

输出为

userdisplayname, Samaccountname, userlastlogon, Computername, clastlogondate, operatingsystem 
adminstrator,adminstrator,10/12/2017,computer1,10/12/2017,server2012
adminstrator,adminstrator,10/12/2017,computer1,10/12/2017,server2012
adminstrator,adminstrator,10/12/2017,computer1,10/12/2017,server2012
adminstrator,adminstrator,10/12/2017,computer1,10/12/2017,server2012
otheuser,user10/12/2017,server,10/12/2017,server2003

它应显示实际输出。

0 个答案:

没有答案