Get-ADUser returning null

This issue has me baffled. I am running the code below. (I have stripped out most of the code while I’m troubleshooting.)
I can run this exact same code with Get-ADComputer and it works fine. The Get-ADUser command works fine by itself. It even works inside the loop. It seems to break when I add the conditional statement. It successfully finds a user, but it just returns null. Anyone have any ideas?

function Get-ForestObject
{
	[CmdletBinding()]
	Param
	(
		# Param1 help description
		[Parameter(Mandatory = $true, Position = 0)]
		[ValidateSet('Computer','User')]$ObjectType,
		# Param2 help description
		[string]$Name
	)

    $domains="mydomain.com"

    foreach($domain in $domains){
        if($ObjectType -eq 'User'){$ADobect = (Get-ADUser -Identity $Name -Server $domain -ErrorAction SilentlyContinue)}
        if($ADobect){$result=$adobject}
    }
    return $result
}

Get-ForestObject -ObjectType User -Name username

I would try removing your ErrorAction so you can at least get some indication of why it is failing.

1 Like

It’s a typo in the variable name that I believe is suppose to be $adobject. in some areas $adobject is $adobect. At the end you are setting your result equal to $adobject which is never set with a value, so its empty, therefore $result is empty. Just update all references of $adobect to $adobject and it should work.

First of all, fix the misprint in variables names (you’ve missed ‘j’ two times))):

and

After that you’ll see the error message saying that PoSh cannot connect to the server $domain because the -Server parameter of Get-ADUser awaits the domain controller name, not the domain name.
You can use the Get-ADDomainController cmdlet to discover an available ADC in the specified domain:

foreach($domain in $domains){
        if($ObjectType -eq 'User'){$ADobject = (Get-ADUser -Identity $Name -Server (Get-ADDomainController -Discover -DomainName $domain).Name -ErrorAction SilentlyContinue)}
        if($ADobject){$result = $adobject}

And at last your $result variable is declared within the foreach loop and exists only in it’s scope. After your script exits the loop, the $result variable doesn’t exist! You should declare it before the foreach loop like this:

This should work fine – I’ve checked…

Hey Drew,

The typo is the reason why the result empty. I had already mentioned that in a previous post :slight_smile:.

Regarding your domain note, just an FYI - that’s not necessarily true. I understand where you’re coming from but it’s very possible in their environment that actually works. I know because it works in my work environment.

Also, $result doesn’t need to be declared - there’s not a scoping issue there. That part works fine the way the OP originally wrote it.

Which you posted while I was typing my answer and noticed that only after I’d posted mine :slightly_smiling_face:
Considering “domain” and “result” – may be you’re right but in my environment nothing worked until I consequently fixed all mentioned issues.
[UPD]: Yeahp… The $result declaration is not an issue. Sorry that!

because of the only ADC in your domain?

I don’t want to derail this too far - but no, my environment we have multiple DCs. I think it works in environments that have A records in DNS that point back to the domain. IIRC, those are automatically created in DNS when you promote a DC.

Thanks for the replies. I can’t believe I missed the typos. That fixed it.

1 Like