Active Directory Computer Search / sorting

This topic contains 6 replies, has 4 voices, and was last updated by Profile photo of Tech Gismo Tech Gismo 1 year, 5 months ago.

  • Author
  • #24801
    Profile photo of Tech Gismo
    Tech Gismo

    I have made this Little piece of code, that can find a computer owner by searching after name, and then gets the computer owned by that person.

    Problem is that I can only enter first name.
    Next problem; a first name can be owned by many people, so even though I am searching on the description parameter, I would also very much like it to be showed in the "Description" along side "Name".

    Reason for this is that it helps look up a computer name faster and gets it's owner, so I don't have to ask for computer names all the time. I can just look them up here.

        [String]$Name = [Read-Host "Regular Name"], 
        [String]$ComputerOU = [Read-Host "OU is either Windows7 or Windows8"]
    #Find computer owner via AD description field
    Get-ADComputer -LDAPFilter "[Description=*$Name*]" -SearchBase "OU=$ComputerOU,OU=Computers,OU=LB,DC=LB,DC=NET" | fl Name,Description

    I apreciate all the helpfull people in here, thanks in advance for the help.

    Kind regards.

  • #24804
    Profile photo of Rob Simmers
    Rob Simmers

    It's a wildcard search, so why can you ONLY search by the first name?

    $Description = "Owner: Rob Simmers; Location Building: B"
    $firstNameLike = "R"
    $lastNameLike = "Sim"
    $Description -like ("*{0}*{1}*" -f $firstNameLike, $lastNameLike)

    This would return $true and you could do the same type of search on the description in your LDAP query. I may not be understanding the scenario, so possibly provide a example description and how you would like to search. To show the description beside the name, use Format-Table -AutoSize, not fl (Format-List)

  • #24808
    Profile photo of Tech Gismo
    Tech Gismo

    1. I have created a file with the name param.ps1, so when you run the file inside powershell it asks for regular name, which by the way should be first name. Then it asks for OU, a Windows7 og Windows8 OU.
    2. What you are showing me is a variable called Description "With text in the variable"
    3. I Want PowerShell to show the content of both Name and Description field.

    4. When you look at Active Directory objects like a computer you can insert in the field "Description" who owns the computer, which I already use in my wild Card search, but besides that I also want the text FROM the description field to be shown along side "NAME".

    I you have the possibility try to run my script.

  • #24957
    Profile photo of Tech Gismo
    Tech Gismo
    [String]$Name = [ Read-Host "insert name" ] ,
    [String]$ComputerOU = [ Read-Host "insert OU, either Windows7 or windows8" ]
    Get-ADComputer -LDAPFilter "[Description=*$Name*]" -SearchBase "OU=$ComputerOU,OU=Computers,OU=Domain,DC=Domain,DC=NET" | ft Name

    My problem is not that the script does not work, what my problem is. Is this: If I add "Description field" after Name, så that it gets sorted after Name, I do not get the text from the Description field, all that PowerShell is showing me is the headline "Description" and below it {}

    But there is text written, why is that text not shown?

  • #24960
    Profile photo of Matt Bloomfield
    Matt Bloomfield

    Not all Active Directory attributes are returned by default, only a subset. If you want the Description field then you need to specify it with the -Property parameter.

    Get-ADComputer -LDAPFilter "[Description=*$Name*]" -SearchBase "OU=$ComputerOU,OU=Computers,OU=Domain,DC=Domain,DC=NET" -Property Description | ft Name,Description
  • #24962
    Profile photo of Jack Neff
    Jack Neff

    *EDIT* Matt beat me to it. Nice one Matt.

    Description isn't one of the properties returned by Get-ADComputer by default. If you want it displayed in your table you'll have to use the -Properties parameter.

  • #25377
    Profile photo of Tech Gismo
    Tech Gismo

    Thanks a lot for the help. I'm using ISE a lot for practice and the only suggestion it came up with was -Properties which didn't seemed to help me in this scenario.

    And of course when I see it now, it makes sense.

You must be logged in to reply to this topic.