Search code examples
windowspowershellactive-directory

Get-DHCPServerinDC via PowerShell Remote


I have more than one Active Directory Forest and I want to get the authorized DHCP server for each forest. I wanted to invoke the PowerShell CmdLet Get-DHCPServerinDC. If I execute the command on a domain controller in forest A it will put out the correct DHCP server. If I execute the command via Invoke-Command -ComputerName DCforestA -ScriptBlock {Get-DHCPServerinDC} from forest B, I get the autorized DHCP server from forest B. Is this a bug? Can anyone reproduce this problem?


Solution

  • I'm posting here as the question is very old, but having the answer here is easier than having to find the equivalent question on ServerFault :)

    I have just encountered the same issue, and it looks like the issue is due to the cmdlet getting the domain against which to execute from the user account. Which makes a certain amount of sense, though it would be nice to have the cmdlet be domain-aware and able to accept domains as parameters.

    If you pass credentials for the remote domain which you want to execute the command, the correct list of servers is retrieved. In the example below, you're on a machine on domain1 and connecting to server1 which is on domain2:

    $cred=Get-Credential -Message "Enter credentials for an account on domain2";
    Invoke-Comand -Computername server1 -Credential $cred -Scriptblock {Write-Host $env:USERDOMAIN;Get-DCHPServerInDC}
    

    The USERDomain environment variable is printed to provide confirmation of the context in which the scriptblock is executing.