如何使用非管理员用户帐户在c#或vb中获取远程系统的上次启动时间

时间:2014-04-09 14:28:11

标签: c# vb.net

任何人都可以告诉我们是否可以使用c#或vb或任何命令行工具获取没有管理员帐户的远程系统的最后启动时间,正常运行时间等细节。任何帮助都非常感谢。

2 个答案:

答案 0 :(得分:0)

您可以使用原生方法GetTickCount64

声明本机方法:

internal static class NativeMethods
{
    [DllImport("kernel32")]
    internal static extern UInt64 GetTickCount64();
}

在代码中使用它:

private static DateTime GetLastReboot()
{
    return DateTime.Now - TimeSpan.FromMilliseconds(NativeMethods.GetTickCount64());
}

答案 1 :(得分:0)

更新(2018-09-24)

您可以以非管理员身份查询此内容;尽管您仍然需要一些权限。 对于@yagmoth555's answer here,您需要服务器上具有admin的人员才能授予您对相关WMI名称空间的访问权限。他们可以使用以下脚本执行此操作:https://gist.github.com/Tras2/06670c93199b5621ce2076a36e86f41e

.\Set-WmiNamespaceSecurity.ps1 -namespace 'Win32_OperatingSystem' -operation add -account myUserAccount -permissions @('Enable','RemoteAccess','ReadSecurity') -computerName 'myRemoteServer'

或通过用户界面按照以下说明进行操作:http://vniklas.djungeln.se/2012/08/22/set-up-non-admin-account-to-access-wmi-and-performance-data-remotely-with-powershell/

一旦授予此访问权限,就可以使用下面的WMI解决方案。


原始答案和代码

很遗憾,我不知道您没有权限的方法。

帮助他人解决这个问题;在远程计算机上确实具有权限的位置,可以使用以下方法:

PowerShell

$arrayOfServers = @('myServer1','myServer2')
#method 1
$arrayOfServers | Get-CimInstance Win32_OperatingSystem | select csname, lastbootuptime
#method 2
$arrayOfServers | %{Get-WmiObject Win32_OperatingSystem -ComputerName $_} | select csname, @{N='LastBootupTime';E={$_.ConverttoDateTime($_.LastBootupTime)}}

C#

void Main()
{
    string remoteServer = "myServer";

    Console.WriteLine("GetLastBootUpTimeViaWMI");
    Console.WriteLine(GetLastBootUpTimeViaWMI(remoteServer));

    //code is simpler than WMI version, but requires remote management to be enabled
    Console.WriteLine("GetLastBootUpTimeViaCIM");
    Console.WriteLine(GetLastBootUpTimeViaCIM(remoteServer));

    Console.WriteLine("Done");
    Console.ReadKey();
}

//using System.Management

DateTime GetLastBootUpTimeViaWMI(string computerName = ".")
{
    //https://docs.microsoft.com/en-us/dotnet/api/system.management.managementscope?view=netframework-4.7.2
    var scope = new ManagementScope(string.Format(@"\\{0}\root\cimv2", computerName));
    scope.Connect();
    //https://docs.microsoft.com/en-us/dotnet/api/system.data.objects.objectquery?view=netframework-4.7.2
    var query = new ObjectQuery("SELECT LastBootUpTime FROM Win32_OperatingSystem");
    //https://docs.microsoft.com/en-us/dotnet/api/system.management.managementobjectsearcher?view=netframework-4.7.2
    var searcher = new ManagementObjectSearcher(scope, query);
    var firstResult = searcher.Get().OfType<ManagementObject>().First(); //assumes that we do have at least one result 
    return ManagementDateTimeConverter.ToDateTime(firstResult["LastBootUpTime"].ToString());
}

//using Microsoft.Management.Infrastructure

DateTime GetLastBootUpTimeViaCIM(string computerName = ".")
{
    var namespaceName = @"root\cimv2";
    var queryDialect = "WQL";
    var queryExpression = "SELECT LastBootUpTime FROM Win32_OperatingSystem";
    //https://msdn.microsoft.com/library/microsoft.management.infrastructure.cimsession.aspx
    var cimSession = CimSession.Create(computerName);
    return Convert.ToDateTime(cimSession.QueryInstances(namespaceName, queryDialect, queryExpression).First().CimInstanceProperties["LastBootUpTime"].Value);
}