【问题标题】:Pull Active Directory user information拉取 Active Directory 用户信息
【发布时间】:2014-10-08 12:48:28
【问题描述】:

您好,我有一个脚本,如果我编写“write-host”,该脚本将部分工作,但在将信息导出到文本文件时根本不工作。我想找到用户 ID 的描述、名称、显示名称和经理。请帮助我了解为什么它不起作用。

Import-Module ActiveDirectory

$document = "C:\Temp\ADupdate yyyy.txt"

Clear-Content $document

<# 
-ne = not equal CN=xxpc37254,OU=Standard,OU=Users,OU=Corporate,DC=we,DC=dirsrv
-eq = equal
-lt = less than
-gt = greater than
-ge = greater than or qual to
-le = less than or equal to
#>

$Header = `
"User ID" + "|" + `
"Display Name" + "|" + `
Description" + "|" + `
"ID Owner" + "|" + `
"ID Owner Name"


#Write out the header
$Header | Out-File $document -Append

#$Users = Get-ADUser -Filter {name -like "xxpc*" -or name -like "xxmd*"} - SearchBase "OU=Corporate,DC=we,DC=dirsrv,DC=com" -Properties name, displayname, description, manager 
$Users = Get-ADUser -filter {name -like "xxpc*" -or name -like "xxmd*"} -Properties name,   displayname, description, manager 


foreach ($User in $Users)
{
#manager missing
if ($Users.Manager -eq $null) {
    $owner = "MISSING"
    $ownerid = "MISSING"
    $ownername = "MISSING"

} else {
#grab the manager's name, surname, and department
    $owner = Get-ADUser ($userid.Manager) -Properties GivenName, Surname, Department
    $ownerid = $owner.Name
    $ownername = $owner.Surname + "." + $owner.GivenName
}

$listing = `
$Users.Name + "|" + `
$Users.DisplayName + "|" + `
$Users.Description + "|" + `
$ownerid + "|" + `
$ownername 

$listing | Out-File $document -Append

【问题讨论】:

  • 为什么不起作用?
  • 所以我将 {name -like "xxpc*" -or name -like "xxmd*"} 更改为具有 'xxpc*' 和 'xxmb*' 并且它与 write-host 一起使用。仍然没有输出到文本文件:/
  • 文本文件只显示标题

标签: powershell


【解决方案1】:

好的。它不起作用的原因可能是因为 foreach 中的 if 语句是错误的。您应该在 foreach 中使用 $user,而不是像您在此处使用的 $users。

话虽如此,您应该阅读有关创建自定义对象的内容,获取所需的数据,创建自定义对象并将其写入管道。这样,您可以通过多种不同方式使用输出,无论是写入文本文件、csv 文件、xml 还是仅写入屏幕。

【讨论】:

  • 并将 $listing 更改为 all 作为 $User。 ...它现在可以工作了谢谢!...只是要弄清楚如何使这个问题成为“已回答”
  • @AlyssaCooke 在相应答案旁边的向上/向下箭头下方的复选标记。
  • @AlyssaCooke,别忘了点赞和接受。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多