[英]How to get owner of process running on remote machine without WMI
[英]How to get remote machine OS version in .Net, without WMI?
有沒有辦法在.Net(C#)中獲取遠程機器的操作系統版本? 使用WMI對我來說是不可接受的。
我有遠程機器的IP地址:)和管理員憑據
基於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"));
}
}
提供遠程注冊表訪問權限,
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();
讀取注冊表值是最好的解決方案,但有時,當您沒有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{{{1}}}", remoteMachine, command);
}
ps.AddScript(command);
return ps.Invoke().Cast<PSObject>().ToList();
}
}
並簡單地使用示例:
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有更多數據,只需在PS中鍵入systeminfo即可選擇所需內容。 另外..在PS 5中,通過Get-ComputerInfo對SystemInfo進行了現代化,但為了這個目的,SystemInfo更好,因為它具有更好的兼容性(即使是PS版本2)並且速度更快
可以使用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用法
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.