Need help in fixing the error and format the output

hello there!

I am trying to get the installed hotfix report similiar to wmic qfe list using powershell. I have a problem here.

When the installedby field value is empty, the value is getting assigned to the immediate next property name, how can I avoid this, for more details please see the images attached

b



Function Get-MSHotfix  
{  
    $outputs = Invoke-Expression "wmic qfe list"  
    $outputs = $outputs[1..($outputs.length)]  
      
      
    foreach ($output in $Outputs) {  

    pause
        if ($output) { 
        
       
            $output = $output -replace 'Security Update','Security-Update'  
            $output = $output -replace 'NT AUTHORITY','NT-AUTHORITY'  
            $output = $output -replace '\s+',' '  
            $parts = $output -split ' ' 
            if ($parts[5] -like "*/*/*") {  
                $Dateis = [datetime]::ParseExact($parts[5], '%M/%d/yyyy',[Globalization.cultureinfo]::GetCultureInfo("en-US").DateTimeFormat)  
            } else {  
                $Dateis = get-date([DateTime][Convert]::ToInt64("$parts[5]", 16)) -Format '%M/%d/yyyy'  
            }  
            New-Object -Type PSObject -Property @{  
                Caption = [string]$parts[0]  
                Computername = [string]$parts[1]  
                Description = [string]$parts[2]  
                FixComments = [string]$parts[6]  
                HotFixID = [string]$parts[3]  
                InstalledOn = Get-Date($Dateis)-format "yyyy-MM-dd"  
                InstalledBy = [string]$parts[4]  
                InstallDate = [string]$parts[7]  
                Name = [string]$parts[8]  
                ServicePackInEffect = [string]$parts[9]  
                Status = [string]$parts[10]  
            }  
        }  
    }  
} 




Get-MSHotfix|Sort-Object installedon -Descending|Select-Object -Property Computername,InstalledOn, HotFixID, InstalledBy|Format-Table

Please help

When that property doesn’t exist, you’re going have fewer elements in the array, so getting the values using the array index notation, expecting the same property at the same index each time, isn’t going to work.

I’m really curious why you’re trying to roll your own solution here, when

Get-CIMInstance Win32_QuickFixEngineering | 
    Select-Object PSComputerName,InstalledOn,HotFixID,InstalledBy | 
        Format-Table

will give you the same result.

2 Likes

I agree with @matt-bloomfield. To parse text output from an external tool is mostly the worst idea for a given task.
If you wanted to do it anyway you cannot use the output like a space separated CSV because it is a fixed grid. You have to use the absolut positions of the text in each line.

Function Get-MSHotfix {  
    $WMICqfeList = Invoke-Expression "wmic qfe list" | Select-Object -Skip 1

    foreach ($WMICqfe in $WMICqfeList) {
        if ($WMICqfe.Length -eq 0) { continue }
        $WMICqfe = '{0,-180}' -f $WMICqfe
        [PSCustomObject]@{
            Caption             = $WMICqfe.substring(0, 43).Trim()
            Computername        = $WMICqfe.substring(44, 12).Trim()
            Description         = $WMICqfe.substring(57, 16).Trim()
            FixComments         = $WMICqfe.substring(74, 12).Trim()
            HotFixID            = $WMICqfe.substring(87, 10).Trim()
            InstallDate         = $WMICqfe.substring(98, 12).Trim()
            InstalledBy         = $WMICqfe.substring(111, 21).Trim()
            InstalledOn         = $WMICqfe.substring(132, 12).Trim()
            Name                = $WMICqfe.substring(145, 5).Trim()
            ServicePackInEffect = $WMICqfe.substring(151, 20).Trim()
            Status              = $WMICqfe.substring(172).Trim()
        }
    }
}

You might have another order of your properties. At least I had another than you showed in your question. :wink:

I think you guys are making this way, way, way harder than it has to be.

$csvoutput = wmic qfe list /FORMAT:csv | ConvertFrom-Csv

$csvoutput | Format-Table

Output

3 Likes

Doug for the winner … :+1:t4: :wink: :smiley: :love_you_gesture:t4: :metal:t4:

1 Like

Can also do html, form or table.

wmic qfe list /FORMAT:hform | Set-Content c:\temp\qfe_form.html

Invoke-Item $$

wmic qfe list /FORMAT:htable | Set-Content c:\temp\qfe_table.html

Invoke-Item $$

Well, now you’re exaggerating. :stuck_out_tongue_winking_eye: :stuck_out_tongue_winking_eye: :rofl: :rofl: :kissing_heart:

Just thought it was cool too :sunglasses:

@krzydoug Thanks for the answer.

I made it like below, but it is working on local machine but I am not able to query on a remote machine.

(wmic qfe list /FORMAT:csv | ConvertFrom-Csv|
select @{n="InstalledOn";e={Get-Date $($_.installedon) -Format yyyy-MM-dd}}|
sort InstalledOn -Descending|Select -First 1).InstalledOn

Above is working, but this isn’t, can you please correct me:

(wmic /node:$system qfe list /FORMAT:csv | ConvertFrom-Csv|
select @{n="InstalledOn";e={Get-Date $($_.installedon) -Format yyyy-MM-dd}}|
sort InstalledOn -Descending|Select -First 1).InstalledOn

I sure can’t. I have no idea what’s in $system