ohai icon indicating copy to clipboard operation
ohai copied to clipboard

Domain and FQDN not picked-up correctly on Windows Server 2016

Open karim-jaouadi opened this issue 6 years ago • 6 comments

Internal issue CHEF-607 ... might relate to #1733

Description

Ohai don't seem to pick-up and update the attributes related to domain and FQDN correctly on Windows Server 2016. The below output is what I see from Chef Manage standpoint. On the server I can see the domain and FQDN as the server joined the Active Directory. I've try to run another time to see if it change a thing but no success.

machinename: 123401XYZW007
domain:
hostname: 123401XYZW007
fqdn: 123401XYZW007

Looking at the code it seems that the way of collecting those information don't seems supported on Windows Server 2016 (See below). I've provided suggested way to read those value (tested from Powershell) it that could be validated and implemented.

Ohai Version

PS C:\Windows\system32> ohai --version
Ohai: 14.5.5

Platform Version

Windows Server 2016

Output of current Ohai call

Base on the code below https://github.com/chef/ohai/blob/308a32b138ac700d1ecd39cb67b2ebfbd338fad7/lib/ohai/plugins/hostname.rb#L162-L181

The powershell instruction are not recognized.

PS C:\Windows\system32> Socket.gethostbyname(Socket.gethostname)
Socket.gethostname : The term 'Socket.gethostname' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the 
name, or if a path was included, verify that the path is correct and try again.
At line:1 char:22
+ Socket.gethostbyname(Socket.gethostname)
    + CategoryInfo          : ObjectNotFound: (Socket.gethostname:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 

PS C:\Windows\system32> Socket.gethostname
Socket.gethostname : The term 'Socket.gethostname' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the 
name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ Socket.gethostname
    + CategoryInfo          : ObjectNotFound: (Socket.gethostname:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Suggested method to call

Seems the following methods are supported out of the box and provide either machine_name and domain or the FQDN.

$myFQDN=(Get-WmiObject win32_computersystem).DNSHostName+"."+(Get-WmiObject win32_computersystem).Domain
Write-Host $myFQDN
[System.Net.Dns]::GetHostByName($env:computerName).HostName
[System.Net.Dns]::GetHostByName((hostname)).HostName

karim-jaouadi avatar Oct 16 '18 16:10 karim-jaouadi

@karim-jaouadi We have a fix that may resolve this that was just merged into master and will ship in Ohai 14.8

tas50 avatar Nov 28 '18 18:11 tas50

Hey. FWIW I'm still seeing the FQDN resolution failure on Windows Server 2019 with ohai version 14.8.10.

Ryuzavi avatar Jan 09 '19 13:01 Ryuzavi

I'm noticing this as well, the hint always returns empty values on Windows 2016.

It's supposed to gather this from the metadata available at http://169.254.169.254/latest/meta-data, but this doesn't work out of the box. There's a script here that fixes the issue:

https://forums.aws.amazon.com/thread.jspa?messageID=790984

Add-Content 'c:\script.log' (date)
 
# This fixes a bug in AWS startup script processing.
 
# 169.254.169.254 is for metadata service
# 169.254.169.250 is for KmsInstanceVpc1
# 169.254.169.251 is for KmsInstanceVpc2
$ipAddrs = @("169.254.169.254/32", "169.254.169.250/32", "169.254.169.251/32")
 
$sleepTime = 1
$count = 0
 
# Retry logic for querying primary network interface and adding routes.
while($true)
{
    try
    {
        Add-Content 'c:\script.log' "Checking primary network interface"
 
        $ipConfigs = Get-NetIPConfiguration | Sort-Object -Property "InterfaceIndex" | select InterfaceIndex, IPv4DefaultGateway
        if(-not $ipConfigs -or $ipConfigs.Length -eq 0)
        {
            throw New-Object System.Exception("Failed to find the primary network interface")
        }
        $primaryIpConfig = $ipConfigs[0]
        $interfaceIndex = $primaryIpConfig.InterfaceIndex
        $defaultGateway = $primaryIpConfig.IPv4DefaultGateway.NextHop
        $interfaceMetric = 1
 
        Add-Content 'c:\script.log' "Primary network interface found. Adding routes now..."
 
        foreach ($ipAddr in $ipAddrs)
        {
            try
            {
                Remove-NetRoute -DestinationPrefix $ipAddr -PolicyStore ActiveStore -Confirm:$false -ErrorAction SilentlyContinue
                Remove-NetRoute -DestinationPrefix $ipAddr -PolicyStore PersistentStore -Confirm:$false -ErrorAction SilentlyContinue
                New-NetRoute -DestinationPrefix $ipAddr -InterfaceIndex $interfaceIndex `
                    -NextHop $defaultGateway -RouteMetric $interfaceMetric -ErrorAction Stop
                Add-Content 'c:\script.log' ("Successfully added the Route: {0}, gateway: {1}, NIC index: {2}, Metric: {3}" `
                    -f $ipAddr, $defaultGateway, $interfaceIndex, $interfaceMetric)
            }
            catch
            {
                Add-Content 'c:\script.log' ("Failed to add routes: {0}" -f $ipAddr)
            }
        }
 
        # Break if routes are added successfully.
        break
    }
    catch
    {
        Add-Content 'c:\script.log' ("Failed to add routes.. attempting it again {0}" -f $_.Exception.Message)
    }
 
    # It logs the status every 2 minutes.
    if (($count * $sleepTime) % 120 -eq 0)
    {
        Add-Content 'c:\script.log' "Message: Failed to add routes.. attempting it again"
    }
 
    Start-Sleep -seconds $sleepTime
    $count ++
}

stellarsquall avatar Apr 24 '19 23:04 stellarsquall

Updated code is available in Ohai version 15.0.13. chef-client: 14.12.9 that I installed with MSI is using Ohai-14.8.11

Nimesh-Msys avatar Jul 03 '19 16:07 Nimesh-Msys

Additionally, To get the FQDN in powershell, we could try

$a = [System.Net.Dns]::GetHostByName(($env:computerName))
$hostName = [System.Net.Dns]::GetHostByAddress($a.AddressList.IpAddressToString)
$hostName.HostName

Nimesh-Msys avatar Jul 03 '19 16:07 Nimesh-Msys

The PowerShell equivalent of:

info = Socket.gethostbyname(Socket.gethostname)
...
  fqdn info.first`

Is: [System.Net.Dns]::GetHostByName([System.Net.Dns]::GetHostName()).HostName

affieuk avatar Aug 26 '21 12:08 affieuk