【问题标题】:Cannot store Get-aduser value to variable无法将 Get-aduser 值存储到变量
【发布时间】:2020-07-16 03:13:16
【问题描述】:

我无法将 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 

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

【问题讨论】:

  • $a 是您使用的实际变量名吗?

标签: powershell active-directory


【解决方案1】:

还不能评论:(

代码

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

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

作为答案,您可以尝试

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

【讨论】:

  • 没有变量,输出显示正常。建议一个也不好用。
  • 如果你尝试不同的变量名会发生什么,它可能与这个superuser.com/questions/777416/…有关
  • 也尝试了不同的变量。我正在寻找处理它的新方法,因为我能够将数据导出到带有 Samaccount 名称的 txt 文件,然后将 samaccount 的内容放入不同的变量中并通过 Get-aduser 循环。
  • Get-ADUser -filter * -property name | select samaccountname -ExpandProperty samaccountname|out-file c:\aduser.txt -Append $usersname=get-content c:\aduser.txt
  • 也许你明确指定所有属性? $a=Get-ADUser -filter * -properties samaccountname, mail, surname, givenname,LastLogondate | Select samaccountname, mail, surname, givenname,LastLogondate
【解决方案2】:

好吧,我仍然无法将 -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

【讨论】:

    猜你喜欢
    • 2013-12-03
    • 1970-01-01
    • 1970-01-01
    • 2020-04-18
    • 1970-01-01
    • 2020-03-31
    • 2021-09-07
    • 1970-01-01
    • 2019-03-10
    相关资源
    最近更新 更多