无法将Get-aduser值存储到变量中

时间:2016-10-12 15:13:31

标签: powershell active-directory

我无法将Get-aduser -filter *的值存储到下面的变量中是代码,$ a显示为空白。

$a=Get-ADUser -filter * | Select samaccountname, mail, surname, givenname,LastLogondate 

这个适用于单个对象:

$a=Get-ADUser -identity 'name' | Select samaccountname, mail, surname, givenname,LastLogondate 

但我想要所有用户,是否有另一种方法来处理此请求。?

2 个答案:

答案 0 :(得分:0)

暂无评论:(

代码

$a=Get-ADUser -filter * | Select samaccountname, mail, surname, givenname,LastLogondate 

是正确的,对我来说很好,如果你在没有变量的情况下正常运行会发生什么?

作为答案你可以尝试

$a=Get-ADUser -Filter 'Name -like "*"' | Select samaccountname, mail, surname, givenname,LastLogondate | ft

答案 1 :(得分:0)

我还是不能在变量中存储-filter输出。因此,另一种方式导出到文本文件,然后将文本文件信息转换为foreach循环以执行进一步的活动,下面的部分代码可能会帮助某人,不要忘记在每次运行后删除aduser.txt,将调查以后更好的解决方案:

Get-ADUser -filter * -property name | select samaccountname -ExpandProperty samaccountname|out-file c:\aduser.txt -Append
 $usersname=""  
 $usersname=get-content c:\aduser.txt
 $usersname.count
 $vmarray=""
 $vmarray=@()
 foreach ($item in $usersname)

 {
    $list=""
        $list=get-aduser -Identity $item -property mail, surname, givenname, LastLogondate,memberof| Select samaccountname, mail, surname, givenname,LastLogondate,memberof
        $member=get-aduser -Identity $item -property memberof| Select memberof -ExpandProperty memberof

                $mem=""
                $ou=""
                foreach ($mem in $member)
                {
                $ou= ($mem.memberof).replace("OU=","")
            }



            $obj= new-object psobject -Property @{
            EmailId=$list.mail
            SurName=$list.surname
            FirstName=$list.givenname
            lastlogon=$list.LastLogondate
            Adid=$list.samaccountname
            FullmembershipOU=$ou
            InfoRecordedON=$date
            }
            $vmarray+=$obj
            }



 $vmarray.count
 $vmarray|ft