narue1992
narue1992

Reputation: 351

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. I want to find the user ID's description, name, displayname and manager. 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

Upvotes: 0

Views: 457

Answers (1)

ojk
ojk

Reputation: 2542

Ok. The reason why it's not working is probably because the if statement inside the foreach is wrong. You should use $user inside the foreach, not $users like you use here.

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.

Upvotes: 1

Related Questions