1

我想比较两个日期值,但它们的格式不同。我从 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"
        }
}
4

1 回答 1

2

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

$LastBoot = (Get-WmiObject -Class Win32_OperatingSystem -computername $computer) | %{$_.ConverttoDateTime($_.LastBootUpTime)}
于 2013-06-05T08:16:22.920 回答