$OSInfo = get-wmiobject -class win32_operatingsystem -computername c83323
($OSInfo `
| Format-List `
@{Name="OS Boot Time";Expression={$_.ConvertToDateTime($_.LastBootUpTime)}} `
| Out-String).Trim() #Output OS Name, Boot Time, and Install Date
output --> OS Boot Time : 12/6/2016 4:09:20 PM
$osinfo = get-wmiobject -class win32_operatingsystem -computername c83323
$osinfo.ConvertToDateTime($osinfo.LastBootUpTime)
output --> Tuesday, December 06, 2016 4:09:20 PM
为什么在运行第一组时会以一种方式获得时间,而在运行第二组时却以完全不同的格式获得时间呢?
最佳答案
第二个实例的输出将为 DateTime 类型。此格式取决于您在系统上选择的日期时间格式。
我修改了您的代码以获取类型:
$osinfo = get-wmiobject -class win32_operatingsystem; ($osinfo.ConvertToDateTime($osinfo.LastBootUpTime)).GetType()
但是,在第一个实例中,您使用了一种称为的计算属性 (check this link) ,该属性基本上可以让您“格式化”并以您喜欢的方式显示属性。就您而言,通过您提供的日期和时间表达式已转换为数组,因此失去了格式。
获取类型:
($OSInfo | Format-List @{Name="OS Boot Time";Expression={$_.ConvertToDateTime($_.LastBootUpTime)}}).GetType()
上面的类型是数组。
编辑:
下面的代码段应该可以解决问题!
@("Computer1","Computer2") | foreach {
$OSInfo = get-wmiobject -class win32_operatingsystem -computername $_;
"Boot time for Computer: $_ = " + $OSInfo.ConvertToDateTime($OSInfo.LastBootUpTime);
} | Out-File "C:\thefolderYouPrefer\boottime.txt"
关于powershell - Powershell计算机的上次启动时间,显示两个不同的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41026356/