不确定我是否做错了什么,但我没有得到我期望的结果。
我有以下函数来返回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
我在日期时间比较中错过了什么简单的小概念?
答案 0 :(得分: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
答案 1 :(得分: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