# Need help in fixing the error and format the output

**URL:** <https://forums.powershell.org/t/need-help-in-fixing-the-error-and-format-the-output/18033>\
**Category:** PowerShell Help\
**Created:** [November 24, 2021, 9:07pm UTC](https://forums.powershell.org/t/need-help-in-fixing-the-error-and-format-the-output/18033 "2021-11-24T21:07:44Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rahul20](https://avatars.discourse-cdn.com/v4/letter/r/838e76/32.png) [@Rahul20](https://forums.powershell.org/u/Rahul20)\
**Post date:** [November 24, 2021, 9:07pm UTC](https://forums.powershell.org/t/need-help-in-fixing-the-error-and-format-the-output/18033/1 "2021-11-24T21:07:44Z")

</div>

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](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/d/d3d8fad264f595f5b77ef3c25a2d286137efe8e4.png)

 ![a](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/5/5095d7dee628ad1f6da9e91f567bf0d6ea41a20e.png)

```auto

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

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [November 24, 2021, 9:28pm UTC](https://forums.powershell.org/t/need-help-in-fixing-the-error-and-format-the-output/18033/2 "2021-11-24T21:28:05Z")

</div>

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

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

```

will give you the same result.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [November 24, 2021, 9:54pm UTC](https://forums.powershell.org/t/need-help-in-fixing-the-error-and-format-the-output/18033/3 "2021-11-24T21:54:32Z")

</div>

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.

```auto
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. 😉

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [November 24, 2021, 10:13pm UTC](https://forums.powershell.org/t/need-help-in-fixing-the-error-and-format-the-output/18033/4 "2021-11-24T22:13:07Z")

</div>

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

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

$csvoutput | Format-Table

```

Output

 ![2021-11-24 16_11_50-Window](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/8/8a14a06d42346d2e3e09df75ae3e61883e2561bc.jpeg)

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [November 24, 2021, 10:16pm UTC](https://forums.powershell.org/t/need-help-in-fixing-the-error-and-format-the-output/18033/5 "2021-11-24T22:16:05Z")

</div>

Doug for the winner … 👍🏽 😉 😃 🤟🏽 🤘🏽

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [November 24, 2021, 10:36pm UTC](https://forums.powershell.org/t/need-help-in-fixing-the-error-and-format-the-output/18033/6 "2021-11-24T22:36:46Z")

</div>

Can also do html, form or table.

```auto
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 $$

```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [November 25, 2021, 12:25am UTC](https://forums.powershell.org/t/need-help-in-fixing-the-error-and-format-the-output/18033/7 "2021-11-25T00:25:38Z")

</div>

Well, now you’re exaggerating. 😜 😜 🤣 🤣 😘

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [November 25, 2021, 1:03am UTC](https://forums.powershell.org/t/need-help-in-fixing-the-error-and-format-the-output/18033/8 "2021-11-25T01:03:42Z")

</div>

Just thought it was cool too 🕶

---

<div class="post-metadata">

**Author:** ![Rahul20](https://avatars.discourse-cdn.com/v4/letter/r/838e76/32.png) [@Rahul20](https://forums.powershell.org/u/Rahul20)\
**Post date:** [December 3, 2021, 5:37am UTC](https://forums.powershell.org/t/need-help-in-fixing-the-error-and-format-the-output/18033/9 "2021-12-03T05:37:59Z")

</div>

@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.

```auto
(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:

```auto
(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

```

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [December 3, 2021, 6:25am UTC](https://forums.powershell.org/t/need-help-in-fixing-the-error-and-format-the-output/18033/10 "2021-12-03T06:25:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:20pm UTC](https://forums.powershell.org/t/need-help-in-fixing-the-error-and-format-the-output/18033/11 "2024-05-16T20:20:09Z")

</div>

Closed on May 16, 2024, 8:20pm UTC


