This code Works.
$Searcher = New-Object DirectoryServices.DirectorySearcher
$Searcher.Filter = '(&(objectCategory=computer)(anr=mg10011001))'
$Searcher.SearchRoot = 'LDAP://DC=Acme,DC=org'
$Searcher.FindAll()
It will return the OU path where my workstation lives. My workstation is mg10011001
But when I grab the computer name from a registry key and try to use a variable my code runs without error but nothing is returned.
$GetName = (get-ItemProperty hklm:\SOFTWARE\Microsoft\MPSD\OSD).OSDComputerName
$Searcher = New-Object DirectoryServices.DirectorySearcher
$Searcher.Filter = '(&(objectCategory=computer)(anr=$GetName))'
$Searcher.SearchRoot = 'LDAP://DC=Acme,DC=org'
$Searcher.FindAll()
How come?