Converting date error

Ask your 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.
This topic is 12 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Converting date error

Post by ITEngineer »

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: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Converting date error

Post by ITEngineer »

yeah, somehow it doesn't work for some servers but it works for some others.
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Converting date error

Post by jvierra »

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

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

What do you see?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Converting date error

Post by jvierra »

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
This topic is 12 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked