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
| Out-String- theDnsDomainproperty is already a string(Get-ADDomain).DNSRoot$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).$get_AD_Server = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration).where({$_.DNSDomain},'First').DNSDomain