简体   繁体   English

如何在没有WMI的.Net中获取远程机器操作系统版本?

[英]How to get remote machine OS version in .Net, without WMI?

Is there any way to get OS version of remote machine in .Net(C#)? 有没有办法在.Net(C#)中获取远程机器的操作系统版本? Using of WMI is not acceptable for me. 使用WMI对我来说是不可接受的。

I have IP address of remote machine :) and administrator credentials 我有远程机器的IP地址:)和管理员凭据

based on sgmoore answer with some usings 基于sgmoore回答与一些使用

    public string GetOsVersion(string ipAddress)
    {
        using (var reg = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, ipAddress))
        using (var key = reg.OpenSubKey(@"Software\Microsoft\Windows NT\CurrentVersion\"))
        {
            return string.Format("Name:{0}, Version:{1}", key.GetValue("ProductName"), key.GetValue("CurrentVersion"));
        }
    }

If you have administrator credentials for the remote machine you could use PsExec to run a command remotely to get the OS version eg 如果您拥有远程计算机的管理员凭据,则可以使用PsExec远程运行命令以获取操作系统版本,例如

CMD /c ver CMD / c ver

You can write a wrapper to run PsExec in C# by using the Process class. 您可以使用Process类编写一个包装器来在C#中运行PsExec。

Providing you have remote registry access, 提供远程注册表访问权限,

 var reg = Microsoft.Win32.RegistryKey.OpenRemoteBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, ipaddress); 
 var key = reg.OpenSubKey(@"Software\Microsoft\Windows NT\CurrentVersion\");
 string version = (string) key.GetValue("CurrentVersion");
 reg.Close();

Reading registry values is the best solution, but sometimes, when you don't have WMI and RemoteRegedit service is not working, you can use another option -> SystemInfo.exe 读取注册表值是最好的解决方案,但有时,当您没有WMI并且RemoteRegedit服务不起作用时,您可以使用另一个选项 - > SystemInfo.exe

So you need.. 所以你需要......

Powershell reference: Powershell参考:

using System.Management.Automation; //Get via NuGet "Microsoft.Powershell.X.ReferenceAssemblies"
                                   //where X = your ps preferred version 
                                  //-> in this example I'm using version 5

Some method for powershell invoke: powershell调用的一些方法:

    private static List<PSObject> InvokePowershell(string command, string remoteMachine = null)
    {
        //In powershell 5 version, we don't have to use Runspace... PoweShell class is enough
        List<PSObject> result = new List<PSObject>();

        using (PowerShell ps = PowerShell.Create())
        {
            //If remoteMachine is set, we have to change our ps command for remote invoke
            if (!string.IsNullOrWhiteSpace(remoteMachine))
            {
                command = string.Format("Invoke-Command -ComputerName {0} -ScriptBlock{{{1}}}", remoteMachine, command);
            }

            ps.AddScript(command);

            return ps.Invoke().Cast<PSObject>().ToList();
        }

    }

And simply using example: 并简单地使用示例:

string command = "systeminfo /fo LIST | Select-String \"System Type\", \"OS Name\", \"OS Version\"";
List<PSObject> resultOS = InvokePowershell(command, remoteMachineName);

string nameOS = resultOS.Find(obj => obj.ToString().Contains("OS Name")).ToString().Split(':').Last().Trim();
string versionOS = resultOS.Find(obj => obj.ToString().ContainsString("OS Version")).ToString().Split(null).Last().Trim();
string architectureOS = resultOS.Find(obj => obj.ToString().ContainsString("System Type")).ToString().Split(':').Last().Trim();

SystemInfo.exe has a lot more data, just type systeminfo in PS and you can choose what you want. SystemInfo.exe有更多数据,只需在PS中键入systeminfo即可选择所需内容。 Also.. in PS 5 there is modernization for SystemInfo via Get-ComputerInfo, but for this purpose is SystemInfo better, because has better compatibility (even PS version 2) and is faster 另外..在PS 5中,通过Get-ComputerInfo对SystemInfo进行了现代化,但为了这个目的,SystemInfo更好,因为它具有更好的兼容性(即使是PS版本2)并且速度更快

One can use pinvoke 可以使用pinvoke

[DllImport("Netapi32.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern int NetServerGetInfo(string serverName, int level, out IntPtr serverInfo);

See http://www.pinvoke.net/default.aspx/netapi32/netservergetinfo.html for marshalling types and http://msdn.microsoft.com/en-us/library/windows/desktop/aa370624(v=vs.85).aspx for usage 有关编组类型,请参阅http://www.pinvoke.net/default.aspx/netapi32/netservergetinfo.htmlhttp://msdn.microsoft.com/en-us/library/windows/desktop/aa370624(v=vs。 85).aspx用法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM