读取注册值并转换为日期 [英] Read Registery value and convert to date

查看:65
本文介绍了读取注册值并转换为日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码查找Windows计算机的正常运行时间(上次关闭时间):

I am trying to find the uptime of windows machine (last shutdown time) using following code:

$computernames = gc LegAservers.txt
foreach ($computername in $computernames) {
  $baseKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("LocalMachine",$computername)
  $sKey = "System\CurrentControlSet\Control\Windows"
  $key = $baseKey.OpenSubKey($sKey)
  $kvalue = $key.GetValue("ShutdownTime")
  $kvalue
  "------------------------------------------"
  $baseKey.Close()
}

输出为:

13
152
105
75
132
217
208
1
------------------------------------------

我无法隐瞒此 $ kvalue DateTime 格式使用:

I am unable to covert this $kvalue in DateTime format using:

[DateTime]::FromFileTime($kvalue)

请建议转换日期和时间格式的 $ kvalue 的最佳方法,以便用户可以理解。

Please suggest proper way to convert $kvalue in date and time format so that it can be understood by user.

推荐答案

$regKey = Get-ItemProperty -Path Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Windows
$shutDown = $regKey.ShutdownTime
$Int64Value = [System.BitConverter]::ToInt64($shutDown, 0)
$date = [DateTime]::FromFileTime($Int64Value)

$date

这篇关于读取注册值并转换为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆