Converting date error

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
ITEngineer
Posts: 145
Joined: Wed Oct 12, 2011 10:52 am

Converting date error

Post by ITEngineer » Mon Jan 23, 2012 3:34 pm

Hi All,

Can anyone help me in correcting the following script which cannot display / convert the LastPatchDate please ?
the value sometimes cannot be converted and is like "01cc8accc01c8dc0"

# Create Server List for Patching
$report = @()
foreach($vm in gc C:ListofVMs.txt){
$row = "" | Select Name, State, Status, Host, CPU_Allocated, RAM_Allocated, LastReboot, LastPatchApplied, LastPatchDate, Rebooted, Issues
$row.Name = $vm.Name
$row.State = $vm.PowerState
$row.Status = $vm.ExtensionData.Summary.OverallStatus
$row.Host = $vm.VMHost
$row.CPU_Allocated = $vm.numcpu
$row.RAM_Allocated = $vm.memorymb
$row.LastReboot = [System.Management.ManagementDateTimeConverter]::ToDateTime((Get-WmiObject win32_operatingsystem -cn $row.Name).lastbootuptime)
$lastPatch = Get-WMIObject Win32_QuickFixEngineering -ComputerName $row.Name |
where {($_.psBase.properties[InstalledOn].Value).Length -gt 0} |
Sort-Object -Property {
if($vm.Guest.GuestId -eq "WinLonghornGuest"){
$_.InstalledOn
}else{
[datetime]($_.psbase.Properties[InstalledOn].Value)
}
} |
Select -Last 1
$row.LastPatchApplied = $lastPatch.HotfixId
if($vm.Guest.GuestId -eq "WinLonghornGuest"){
$row.LastPatchDate = $lastPatch.InstalledOn
}
else{
$row.LastPatchDate = $lastPatch.psbase.Properties[InstalledOn].Value
}
$row.Rebooted = "YES/NO"
$row.Issues = "YES/NO"
$report += $row
}
# Create the Coma Seperated File
$report | Export-Csv C:Patching.csv -NoType


Thanks,

AWT
/* IT Engineer */

User avatar
ITEngineer
Posts: 145
Joined: Wed Oct 12, 2011 10:52 am

Converting date error

Post by ITEngineer » Mon Jan 23, 2012 4:14 pm

yeah, somehow it doesn't work for some servers but it works for some others.
/* IT Engineer */

User avatar
jvierra
Posts: 13725
Joined: Tue May 22, 2007 9:57 am
Contact:

Converting date error

Post by jvierra » Mon Jan 23, 2012 4:40 pm

Here - provethis to yourself. Execute this bit of cde only.

foreach($vm in gc C:ListofVMs.txt){ $vm}

What do you see?

User avatar
jvierra
Posts: 13725
Joined: Tue May 22, 2007 9:57 am
Contact:

Converting date error

Post by jvierra » Mon Jan 23, 2012 4:56 pm

Good for you. Those consultants are just so good.

If you do not have any idea of what you are posting then I suspect you won't understand the answers.

The last patch date is most often not set.

This line makes littel sense:
$lastPatch = Get-WMIObject Win32_QuickFixEngineering -ComputerName $row.Name | where {($_.psBase.properties[InstalledOn].Value).Length -gt 0} | Sort-Object -Property { if($vm.Guest.GuestId -eq "WinLonghornGuest"){ $_.InstalledOn }else{ [datetime]($_.psbase.Properties[InstalledOn].Value) } } | Select -Last 1

What is it trying to do? THe date is a single value. YOu cannot tell what dat that the updates were last run in this way.

Use WU to report the last update time. Or read it fom the windowsupdate.log file

Locked