【问题标题】:Powershell script to get only Hour and Minute from NET TIME commandPowershell 脚本仅从 NET TIME 命令获取小时和分钟
【发布时间】:2018-01-03 18:33:16
【问题描述】:

我正在尝试仅从 PowerShell 脚本中检索日期和时间,以下是我迄今为止尝试过的:

脚本:

NET TIME \\ComputerName | Out-File $location

(Get-Content $location)  | % {
    if ($_ -match "2018 : (.*)") {
        $name = $matches[1]
        echo $name
    }
}

net time 输出如下:

\\计算机名的当前时间是 1/3/2018 1:05:51 PM 本地时间 (GMT-07:00) 在 \\Computer Name 是 1/3/2018 11:05:51 AM 命令成功完成。

我只需要当地时间“11:05”的部分。

【问题讨论】:

  • 为什么需要这样做?您的计算机没有与附近的域控制器同步时钟吗?
  • 我需要这个来执行另一个命令“Dumplog”,它只需要开始时间和结束时间作为参数。命令:dumplog ctisvr /bt "Local time" /m "CSTAUniversalFailureConfEvent" 因此我需要服务器的本地时间运行最后 5 分钟。

标签: regex windows powershell time ob-get-contents


【解决方案1】:

虽然Get-Date 不支持查询远程计算机,但是可以使用WMI 检索远程计算机的日期/时间和时区信息;可以在this TechNet PowerShell Gallery page 找到一个示例。使用基于Win32_TimeZone 类调整的Win32_LocalTime 类,将以易于转换为[DateTime] 的形式提供信息,以便在您的脚本中进一步使用。

【讨论】:

    【解决方案2】:

    使用 -match 测试正则表达式 然后使用自动生成的 $matches 数组检查匹配项

    PS> "Current time at \Computer Name is 1/3/2018 1:05:51 PM Local time (GMT-07:00) at \Computer Name is 1/3/2018 11:05:51 AM" -match '(\d\d:\d\d):'
    True
    PS> $matches
    Name                           Value
    ----                           -----
    1                              11:05
    0                              11:05:
    
    PS> $matches[1]
    11:05
    

    【讨论】:

      【解决方案3】:

      简介

      您可以使用此功能来获取您想要的任何信息。我改编了来自this script 的代码。它将使用Get-WmiObject 获得的LocalDateTime 值转换为DateTime 对象。此后,您可以对日期信息做任何您想做的事情。您还可以调整它以使用您想要的任何 DateTime 变量(即上次启动时间)。


      代码

      function Get-RemoteDate {
          [CmdletBinding()]
          param(
              [Parameter(
                  Mandatory=$True,
                  ValueFromPipeLine=$True,
                  ValueFromPipeLineByPropertyName=$True,
                  HelpMessage="ComputerName or IP Address to query via WMI"
              )]
              [string[]]$ComputerName
          )
          foreach($computer in $ComputerName) {
              $timeZone=Get-WmiObject -Class win32_timezone -ComputerName $computer
              $localTime=([wmi]"").ConvertToDateTime((Get-WmiObject -Class Win32_OperatingSystem -ComputerName $computer).LocalDateTime)
              $output=[pscustomobject][ordered]@{
                  'ComputerName'=$computer;
                  'TimeZone'=$timeZone.Caption;
                  'Year'=$localTime.Year;
                  'Month'=$localTime.Month;
                  'Day'=$localTime.Day;
                  'Hour'=$localTime.Hour;
                  'Minute'=$localTime.Minute;
                  'Seconds'=$localTime.Second;
              }
              Write-Output $output
          }
      }
      

      使用以下任一方法调用该函数。第一个用于单台计算机,第二个用于多台计算机。

      Get-RemoteDate "ComputerName"
      Get-RemoteDate @("ComputerName1", "ComputerName2")
      

      【讨论】:

      • 我正在尝试为我的多台计算机列表实现此功能。
      • @prashanthtv 第二个电话正是这样做的。它需要一个字符串数组并在其上循环
      【解决方案4】:

      我知道,如果您没有启用 PowerShell 远程处理,这可能对您不起作用,但如果是,我会这样做。

      Invoke-Command -ComputerName ComputerName -ScriptBlock {(Get-Date).ToShortTimeString()}
      

      【讨论】:

      • 这个不起作用,它给出了时间,但没有给出服务器的本地时间。 :(
      • 我应该测试更多,但我所有的机器都在一个时区。我修改了答案,将时间转换为远程机器上的字符串,现在它应该可以工作了。
      • 有没有办法以 24 小时格式显示结果?
      • "{0:HH:mm}" -f [datetime] $x 用过这个。现在可以了
      猜你喜欢
      • 2018-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多