[英]Pull Active Directory user information
Hi I have a script that will partially work if I write "write-host" but doesn't work at all when exporting the information to a text file. 嗨,我有一个脚本,如果我编写“ write-host”,它将部分起作用,但是在将信息导出到文本文件中时根本不起作用。 I want to find the user ID's description, name, displayname and manager. 我想找到用户ID的描述,名称,显示名称和管理器。 Please help me understand why it isn't working. 请帮助我了解为什么它不起作用。
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
Ok. 好。 The reason why it's not working is probably because the if statement inside the foreach is wrong. 它不起作用的原因可能是因为foreach中的if语句错误。 You should use $user inside the foreach, not $users like you use here. 您应该在foreach中使用$ user,而不要像在此使用$ users那样。
That being said, you should read up on creating custom objects, and get the data you want, create a custom object and write that to the pipeline. 话虽这么说,您应该通读创建自定义对象的知识,并获取所需的数据,创建一个自定义对象并将其写入管道。 That way you can use the output in a lot of different ways, whether that is to write to a text file, a csv file, xml or just to the screen. 这样,您可以以多种不同的方式使用输出,无论是写入文本文件,csv文件,xml还是仅写入屏幕。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.