有没有办法在.Net(C#)中获取远程机器的操作系统版本? 我不接受使用WMI。
我有远程机器的IP地址:)和管理员凭据
答案 0 :(得分:8)
基于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"));
}
}
答案 1 :(得分:2)
答案 2 :(得分:1)
提供远程注册表访问权限,
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();
答案 3 :(得分:1)
读取注册表值是最好的解决方案,但是有时,当您没有WMI并且RemoteRegedit服务不起作用时,可以使用另一个选项-> SystemInfo.exe
所以你需要..
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
一些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{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();
}", remoteMachine, command);
}
ps.AddScript(command);
return ps.Invoke().Cast<PSObject>().ToList();
}
}
并仅使用示例:
try {
const portfolioSnap = await db.collection("portfolio").get();
let portfolioDoc = portfolioSnap.docs;
let categoriesRef = [];
portfolioDoc.forEach(p => {
categoriesRef.push(p.data().category.get());
});
let categories = await Promise.all(categoriesRef);
let portfolio = [];
portfolioDoc.map((p, i) => {
let portfolioObject = {
...p.data(),
category: categories[i].data().name
};
portfolio.push(portfolioObject);
});
return portfolio;
} catch (error) {
console.warn("ERROR: ", error);
}
SystemInfo.exe有很多数据,只需在PS中键入systeminfo即可选择所需的内容。 另外,在PS 5中,通过Get-ComputerInfo对SystemInfo进行了现代化,但是为此,SystemInfo更好,因为它具有更好的兼容性(甚至是PS版本2),并且速度更快
答案 4 :(得分:0)
可以使用pinvoke
[DllImport("Netapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int NetServerGetInfo(string serverName, int level, out IntPtr serverInfo);
有关编组类型,请参阅http://www.pinvoke.net/default.aspx/netapi32/netservergetinfo.html,有关用途,请参阅http://msdn.microsoft.com/en-us/library/windows/desktop/aa370624(v=vs.85).aspx