Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用非管理员用户帐户在c或vb中获取远程系统的上次启动时间_C#_Vb.net - Fatal编程技术网

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

C# 如何使用非管理员用户帐户在c或vb中获取远程系统的上次启动时间,c#,vb.net,C#,Vb.net,任何人都可以告诉我们,如果没有管理员帐户,是否可以使用c或vb或任何命令行工具获取远程系统的上次启动时间、正常运行时间等详细信息。非常感谢您的帮助。您可以使用本机方法 声明本机方法: internal static class NativeMethods { [DllImport("kernel32")] internal static extern UInt64 GetTickCount64(); } 在代码中使用它: private static DateTime GetL

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

您可以使用本机方法

声明本机方法:

internal static class NativeMethods
{
    [DllImport("kernel32")]
    internal static extern UInt64 GetTickCount64();
}
在代码中使用它:

private static DateTime GetLastReboot()
{
    return DateTime.Now - TimeSpan.FromMilliseconds(NativeMethods.GetTickCount64());
}
更新2018-09-24 您可以作为非管理员查询;尽管你仍然需要一些权限。 根据,您需要在服务器上具有管理员权限的人授予您访问相关WMI命名空间的权限。他们可以使用以下脚本执行此操作:

.\Set-WmiNamespaceSecurity.ps1-命名空间“Win32\u OperatingSystem”-操作添加-帐户myUserAccount-权限@'Enable'、'RemoteAccess'、'ReadSecurity'-计算机名“myRemoteServer”

或通过以下UI说明:

授予此访问权限后,您可以使用下面的WMI解决方案

原始答案和代码 遗憾的是,我不知道有一种方法可以解决您没有权限的问题

帮助别人回答这个问题;如果您对远程计算机具有权限,则可以使用以下方法:

动力壳 C
您将如何使用管理员帐户?@Jodrell使用WMI我们可以获得远程系统的正常运行时间。仅供参考:如果8年后这仍然与您有关,解决方法是授予WMI命名空间的权限;i、 因此,在不给予管理员权限的情况下授予访问权限。然后,您可以使用下面我的解决方案中的代码。这里有更多:这提供了本地机器的正常运行时间,我想获得远程系统的正常运行时间。有什么想法吗?附言。在服务器故障上问了同样的问题,以防这些人有解决方案:
$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)}}
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);
}