【问题标题】:Convert different values DateTime and TimeSpan to the same type?将不同的值 DateTime 和 TimeSpan 转换为相同的类型?
【发布时间】:2013-06-05 16:16:22
【问题描述】:

我想比较两个日期值,但它们的格式不同。我从 WMI 获取 LastBootTime 并将其转换为 DateTime - 但我在 powershell 中获得的名称将其定义为“System.TimeSpan

另一种类型来自 Get-EventID 和 TimeGenerated 成员。这显示为“System.DateTime”。

如何将上述两个转换为相同的类型,以便进行比较?

我基本上想看看我从 WMI 得到的(当天)是否与 eventlog 显示特定 eventid 的日期相同。

完整的代码如下,在这个例子中它显示了两个条目之间的差异是否为 12 小时。

两者不同的是:

$LastBoot = (Get-WmiObject -Class Win32_OperatingSystem -computername $computer).LastBootUpTime
$WMIsysuptime = (Get-Date) – [System.Management.ManagementDateTimeconverter]::ToDateTime($LastBoot)


PS C:\Users\gaachm5\Documents> ($WMIsysuptime).gettype()

IsPublic IsSerial Name                                     BaseType                                                                                              
-------- -------- ----                                     --------                                                                                              
True     True     TimeSpan                                 System.ValueType     

这是另一个值:

$sysuptime = (get-eventlog -ComputerName $computer -LogName System | where-object {$_.EventID -eq "6009"} | select -First 1)[0].TimeGenerated

PS C:\Users\gaachm5\Documents> ($sysuptime).gettype()

IsPublic IsSerial Name                                     BaseType                                                                                              
-------- -------- ----                                     --------                                                                                              
True     True     DateTime                                 System.ValueType     

代码:

clear

$server_event = Get-Content -Path "c:\Temp\Servers.txt"

foreach($computer in $server_event)
{

    try 
    {
        (Test-Connection -ComputerName $computer -Quiet)


        $LastBoot = (Get-WmiObject -Class Win32_OperatingSystem -computername $computer).LastBootUpTime
        $WMIsysuptime = (Get-Date) – [System.Management.ManagementDateTimeconverter]::ToDateTime($LastBoot)

        $WMIdays = $WMIsysuptime.Days
        $WMIDaystoHours = ($WMIsysuptime.Days)*24
        $WMIhours = $WMIsysuptime.hours
        $WMITotalHours = $WMIDaystoHours + $hours

        $sysuptime = (get-eventlog -ComputerName $computer -LogName System | where-object {$_.EventID -eq "6009"} | select -First 1)[0].TimeGenerated

        $days = $sysuptime.Day
        $DaystoHours = ($sysuptime.Day)*24
        $hours = $sysuptime.hour
        $TotalHours = $DaystoHours + $hours

        #$dateget = Get-Date -Format d

        if($TotalHours -gt '12')
        {
            Write-EventLog -LogName GSITWinTradFRA -Source RPcMon -EntryType Error -EventId 5 -Message "$computer - FAILED - Servers Uptime is GREATER then 12 hours or not contactable - Uptime from EVENT LOG shows: $days Days and $hours Hours - This is the Sunday patching run. Uptime from WMI shows: $WMIdays and $WMIhours"
        }
        else
        {
            Write-EventLog -LogName GSITWinTradFRA -Source RPcMon -EntryType Information -EventId 4 -Message "$computer - SUCCESS - Servers uptime is less than 12 hours - Uptime is $days Days and $hours Hours - This is the Sunday patching run"
        }
    }
    catch [System.Management.Automation.ActionPreferenceStopException]
        {
            Write-EventLog -LogName GSITWinTradFRA -Source RPcMon -EntryType Error -EventId 5 -Message "$computer - FAILED - Server is not contactable - This is the Sunday patching run"
        }
}

【问题讨论】:

    标签: powershell powershell-2.0 powershell-3.0


    【解决方案1】:

    要从 wmi 获取 DateTime 格式的 lastbootuptime,请尝试:

    $LastBoot = (Get-WmiObject -Class Win32_OperatingSystem -computername $computer) | %{$_.ConverttoDateTime($_.LastBootUpTime)}
    

    【讨论】:

      猜你喜欢
      • 2010-11-12
      • 1970-01-01
      • 2011-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多