powershell - Powershell计算机的上次启动时间,显示两个不同的输出

标签 powershell time output

$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/

相关文章:

powershell - Pester 测试非导出的 PowerShell cmdlet/函数

python - 如何在 Google App Engine 中获取当前英国时间

c++ - 将午夜后的毫秒数转换为时间对象

powershell - 使用PowerShell执行火灾并忘记Web请求

function - 函数中的空参数不为 Null

mysql - 每天统计消息数(17 :00 counts for next day) 之后

JavaScript,计算器中的输出

c - 如何在输出中创建多个答案?

java - 阵列输出? java

powershell - 每个用户的错误处理