Search code examples
powershellhostname

Get-ADComputer does not find hostname but I can find it manually


So I have a script that has worked forever and I have never bat an eye at it. However out of nowhere it has stopped working. When I try to search for hostnames in AD by using the Get-ADComputer cmdlet, it returns the following error message:

Get-ADComputer : Cannot find an object with identity: 'hostname' under: 'DC=domain,DC=name,DC=edu'

Even if I try a basic Get-ADComputer hostname it returns the same error message. However if I pull up AD myself and search for the hostname I find it just fine. There seems to be some sort of disconnect between AD and Powershell.

Anyone else ran into this where basic PS cmdlet's won't work properly?


Solution

  • Figured it out. I had "$ComputerName" when it should have been $ComputerName no quotes. Not sure why I need to search for something "like" the computername rather than the name itself, but it works. Final script looks something like:

    Param(
       [Parameter(Mandatory=$True)]
       [string]$ComputerName
    )
    
    echo ""
    echo ""
    
    $Properties =
    @(
     'CanonicalName',
     'Enabled',
     'Created',
     'LastLogonDate',
     'IPv4Address',
     'OperatingSystem',
     'OperatingSystemServicePack',
     'OperatingSystemVersion'
    )
    Get-ADComputer -Filter 'Name -like $ComputerName' -Properties $Properties | select $Properties```