简体   繁体   中英

Powershell won't pull every computer name from Active Directory

Basically I have written a script that will take an input that is either a username or full name it will output the user's full name, username and computer name. Within AD, the only place where the user's name or username is found is within the description of the computer. This is shown by the variable $new.

The problem I am having is that the code works for some people, but not others. It will still give the username and full name, but leaves the computer name blank. I don't know if this is a problem with my code, or if it is something within AD. Below is the code that I am using:

$input = Read-Host "Enter username or full name" 

if ($input -like "* *") #Used if the user types in the full name
{
    $blah = Get-ADUser -Filter{name -like $input}
    $new = $input.ToUpper() + "; " + $blah.SamAccountName.ToUpper()
    $name = $input.ToUpper()
    $username = $blah.SamAccountName.ToUpper()
}
else                    #Used if the user types in a username
{
    $blah = Get-ADUser $input
    $new = $blah.name.ToUpper() + "; " + $input.ToUpper() 
    $name = $blah.name.ToUpper()
    $username = $input.ToUpper()
}

    $new = $name.ToUpper() + "; " + $username
    $output = Get-ADComputer -Filter{description -like $new} -Properties name
    $strComputer = $output.Name

Write-Host " "
Write-Host "Name:               "$name
Write-Host "Username:           "$username
Write-Host "Computer Name:      "$strComputer
Write-Host " "

I apologize in advanced for my strange variable names. Thanks!

Your filter on Get-ADComputer uses -like , but doesn't use any wildcards. Is this intentional?

Since you're getting multiple values back, try this:

$strComputer = [System.String]::Join(", ", $output.Name)

Thank you guys for the help, you both pointed me in the right direction. This is what I did in the end based on y'all's suggestions and it works just fine!

$input = Read-Host "Enter username or full name" 

if ($input -like "* *") #Used if the user types in the full name
{
    $blah = Get-ADUser -Filter{name -like $input}
    $new = $input.ToUpper() + "; " + $blah.SamAccountName.ToUpper()
    $name = $input.ToUpper()
    $username = $blah.SamAccountName.ToUpper()
}
else                    #Used if the user types in a username
{
    $blah = Get-ADUser $input
    $new = $blah.name.ToUpper() + "; " + $input.ToUpper() 
    $name = $blah.name.ToUpper()
    $username = $input.ToUpper()
}

$new = $name.ToUpper() + "; " + $username
$outputs = Get-ADComputer -Filter{description -eq $new} -Properties name

foreach ($output in $outputs)
{
    $strComputer = $output.Name

Write-Host " "
Write-Host "Name:               "$name
Write-Host "Username:           "$username
Write-Host "Computer Name:      "$strComputer
Write-Host " "
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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