0

不确定我是否做错了什么,但我没有得到我期望的结果。

我有以下函数来返回 UTC 日期/时间值,因此我可以请求本地和远程机器。

function Get-UTCTime {
     Param([string]$computername=$env:computername)

    $utc = Get-WmiObject win32_utctime -ComputerName $computername 

        return $sysdt = ([string]$utc.month + "/" + [string]$utc.day + "/" + [string]$utc.year + " " + [string]$utc.hour + ":" + [string]$utc.minute + ":" + [string]$utc.second)

}

      [datetime]$lsysdt = Get-UTCTime "."
        Write-Host "Local:" $lsysdt

      [datetime]$rsysdt = Get-UTCTime $computer 
        Write-Host "Remote:" $rsysdt.addminutes(6)

        $tester1 = $lsysdt - $rsysdt 
        $tester2 = New-TimeSpan -Start $lsysdt -End $rsysdt 

        Write-Host "Tester 1" $tester1 
        Write-Host "Tester 2" $tester2 

我的结果如下

Local: 6/21/2013 6:06:43 PM
Remote: 6/21/2013 6:12:45 PM
Tester 1 -00:00:02
Tester 2 00:00:02

您可以看到我的时间相差 6 分 2 秒,但返回的结果值仅显示秒。

我扩展了 tester1 变量的所有属性,但仍然看不到它包括分钟数。

Ticks             : -20000000
Days              : 0
Hours             : 0
Milliseconds      : 0
Minutes           : 0
Seconds           : -2
TotalDays         : -2.31481481481481E-05
TotalHours        : -0.000555555555555556
TotalMilliseconds : -2000
TotalMinutes      : -0.0333333333333333
TotalSeconds      : -2

日期时间比较缺少什么简单的小概念?

4

2 回答 2

0

哦,天哪...我看到了我的问题。

我的 addminutes() 方法仅被添加到我的主机输出中,而不是实际存储的变量中。

叹息......当我工作的时候已经很晚了。

[datetime]$lsysdt = Get-UTCTime "."
    Write-Host "Local:" $lsysdt

[datetime]$rsysdt = Get-UTCTime $computer 
    Write-Host "Remote:" $rsysdt.addminutes(-3)

    $dtdiff = New-TimeSpan -Start $lsysdt -End $rsysdt.addminutes(-3)
    $dtdiffmin = $($dtdiff.TotalMinutes).ToString("n", $dtdiffmin) 
    if ($dtdiffmin -lt [int]-3 -and $dtdiffmin -lt [int] 3) { 
            $timeok = $true 
            } 
            else {
                $timeok = $false
            } 

    if ($timeok -eq $false) {
        Write-Host "Remote System Time Ok:" $dtdiffmin  
于 2013-06-21T20:08:29.603 回答
0
     function Get-UTCTime {
     Param([string]$computer)
     $computer=$env:COMPUTERNAME

$utc = Get-WmiObject win32_utctime -ComputerName $computer

    return $sysdt = ([string]$utc.month + "/" + [string]$utc.day + "/" + 
    [string]$utc.year + " " + [string]$utc.hour + ":" + [string]$utc.minute + ":" + [string]$utc.second)

    }

  [datetime]$lsysdt = Get-UTCTime "."
    Write-Host "Local:" $lsysdt

  [datetime]$rsysdt = Get-UTCTime $computer
    Write-Host "Remote:" $rsysdt.addminutes(6)
    $newRsysdt = $rsysdt.addminutes(6)

    $tester1 = $lsysdt - $newRsysdt
    $tester2 = New-TimeSpan -Start $lsysdt -End $newRsysdt

    Write-Host "Tester 1" $tester1 
    Write-Host "Tester 2" $tester2 
于 2013-06-21T23:27:03.153 回答