Skip to content
Advertisement

Detect if a remote computer is Windows or Linux OS

I have IP Address and Server name of a remote computer. I am able to query WMI to get the OS version if the computer is running Windows but is there a way i can query the remote computer and get the OS version if the computer is not running Windows (Linux, Solaris)?

Advertisement

Answer

I guess Active Directory is going to give me what i need. This is what i have so far. Looks promising.

    using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
    {
        ComputerPrincipal computer = ComputerPrincipal.FindByIdentity(ctx, "mylinuxservername");

        if (computer != null)
        {
            var unObject = computer.GetUnderlyingObject() as System.DirectoryServices.DirectoryEntry;
            if(null != unObject)
            {
                var osProperty = unObject.Properties.Cast<System.DirectoryServices.PropertyValueCollection>().Where(p => p.PropertyName == "operatingSystem");

                if(null != osProperty.FirstOrDefault())
                {
                    Console.WriteLine(osProperty.FirstOrDefault().Value);
                }
            }
        }
    }    
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement