简体   繁体   English

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

[英]Cannot store Get-aduser value to variable

I am unable to Store value of Get-aduser -filter * into a variable below is the code, $a shows blank.我无法将 Get-aduser -filter * 的值存储到以下代码中的变量中,$a 显示为空白。

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

This one works fine for single object:这个适用于单个对象:

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

But i want for All users, is there an alternate way to handle this request.?但我想要所有用户,有没有其他方法来处理这个请求。?

Can't comment yet :(还不能评论:(

the code代码

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

is correct and works fine for me, what happens if you run normally without the variable?是正确的并且对我来说工作正常,如果您在没有变量的情况下正常运行会发生什么?

as an answer you could try作为答案,您可以尝试

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

Well I was Still not able store -filter output in a variable.好吧,我仍然无法将 -filter 输出存储在变量中。 Hence went another way round of exporting to text file and then get the text file info into foreach loop to perform further activity, below part of code might help someone, don't forget to remove the aduser.txt after each run, will look into better solution sometime later:因此,另一种方式导出到文本文件,然后将文本文件信息放入 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM