1

I am trying to use the below PowerShell script

$get_AD_Server = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration).DnsDomain | Out-String
$get_Nearest_DC = (Get-ADDomainController -DomainName $get_AD_Server -Discover -NextClosestSite).Name

The output of $get_AD_Server contains contoso.com, however when I pass the variable $get_AD_Server in the next variable it always errors out. What am I doing wrong?

Get-ADDomainController : The format of the specified domain name is invalid
At line:2 char:20
+ ... arest_DC = (Get-ADDomainController -DomainName $get_AD_Server.ToStrin ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (:) [Get-ADDomainController], ADException
    + FullyQualifiedErrorId : GetADDomainController:BeginProcessingOverride:DiscoverDC:1212,Microsoft.ActiveDirectory.Management.Commands.GetADDomainController
5
  • Remove | Out-String - the DnsDomain property is already a string Commented Jun 24, 2020 at 11:31
  • Hi Chris, Still the same it still says The format of the specified domain name is invalid. Commented Jun 24, 2020 at 11:55
  • try (Get-ADDomain).DNSRoot Commented Jun 24, 2020 at 12:13
  • Please be more precise with your question, the message $get_AD_Server.ToStrin ... differs from the "below powershell script" (which doesn't have a .ToString()). Anyways, try: -DomainName "$get_AD_Server" (also Removing | Out-String). Commented Jun 24, 2020 at 12:16
  • Just use $get_AD_Server = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration).where({$_.DNSDomain},'First').DNSDomain Commented Jun 24, 2020 at 14:43

2 Answers 2

1

Automatic variable unrolling will return a collection.

DNSDomain property may not be populated. In my case it isn't. Assuming you've got that covered I think you might have better luck if you isolate the NIC configuration you care about. If you narrow the return to 1 object .DNSDomain will be a string.

In my case this looks like:

(Get-WmiObject win32_NetworkAdapterCOnfiguration -Filter "IPEnabled = 'True'").DnsDomain

If needed just work on the filter until you find something that reliably only returns the NIC you care about.

Note: I may have misread something, but I worry you'll have an issue with the next step. You may have trouble querying the AD domain when you aren't authenticated. If you do hit something like that you may consider using the -Credential parameter on Get-ADDomainController. Of course it'd be interactive at that point.

Sign up to request clarification or add additional context in comments.

Comments

1

Instead of fetching the DNS domain associated with the NIC, pull the computers domain from the Win32_ComputerSystem class:

$domain = (Get-WmiObject -Class Win32_ComputerSystem).Domain
$nearestDC = (Get-ADDomainController -DomainName $domain -Discover -NextClosestSite).Name

3 Comments

Hi Mathias, This works perfectly on a Machine which is connected to a Domain, However i am trying to run this command from Windows PE, That is the reason why i am fetching this information from the NIC.
Does the correct domain name actually appear if you run (Get-WmiObject -Class Win32_NetworkAdapterConfiguration).DnsDomain in WinPE?
Yes it does both your query and mine has literally the same output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.