【问题标题】:Powershell last bootup time of computer showing two different outputs显示两个不同输出的计算机的 Powershell 上次启动时间
【发布时间】:2016-12-07 20:04:09
【问题描述】:
$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

输出 --> 操作系统启动时间:12/6/2016 4:09:20 PM

$osinfo = get-wmiobject -class win32_operatingsystem -computername c83323
    $osinfo.ConvertToDateTime($osinfo.LastBootUpTime)

输出 --> 2016 年 12 月 6 日星期二下午 4:09:20

为什么当我运行第一组时,我以一种方式获得时间,但当我运行第二组时,我以完全不同的格式获得时间?

【问题讨论】:

    标签: powershell time output


    【解决方案1】:

    这是因为您在第一种情况下使用了Format-List 和/或Out-String。使用这些时,PowerShell 会格式化 DateTime 对象的输出,就像您这样写:

    "$(Get-Date)"
    

    【讨论】:

      【解决方案2】:

      第二个实例的输出将是 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()
      

      上述类型将是一个数组

      编辑:

      低于 sn-p 应该可以解决问题!

      @("Computer1","Computer2") | foreach { 
          $OSInfo = get-wmiobject -class win32_operatingsystem -computername $_;
          "Boot time for Computer: $_ = " + $OSInfo.ConvertToDateTime($OSInfo.LastBootUpTime);    
      } | Out-File "C:\thefolderYouPrefer\boottime.txt"
      

      【讨论】:

      • 有没有办法将第一个示例的计算属性存储到一个变量中......没有标题?我想获取多台计算机的启动时间并将其存储在一个文本文件中,每次使用标题都没有好处。
      • $osinfo = get-wmiobject -class win32_operatingsystem; $osinfo.ConvertToDateTime($osinfo.LastBootUpTime).ToString("MM/dd/yyyy hh:mm:ss tt") 用于您之前的请求..
      • 根据您的需要用 sn-p 更新了答案!!看看有没有帮助! :)
      猜你喜欢
      • 2017-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多