Forum Discussion

dmarquesgn's avatar
dmarquesgn
Iron Contributor
Sep 19, 2022

Add column to an output

Hi,

I'm creating a list of HotFixes installed, to control another script that deploys the patches.
So I'm creating a command to check if an update has been installed on the last 10 days, plus if the server has been restarted, and now I want to output this into csv as well. But I need to add one columns to an output so I can make my life easier.
So this is my code:

Invoke-Command -ComputerName $server -Scriptblock {
       Get-HotFix | ?{$_.InstalledOn -gt [datetime]::Today.AddDays(-10)}
       Get-CimInstance -ClassName win32_operatingsystem | select lastbootuptime }

And this is the output:

But I need that the "lastbootuptime" is shown as a column above with the other data, so everything is together and I can export it to CSV.

How can I do this?
Thanks

  • dmarquesgn 

     

    Hi,

     

    For a single additional column, you can just cram it into the Select-Object as shown below.

     

    If you were going to work with multiple new or existing columns, you'd probably branch out into a dedicated hash table.

     

     

    Invoke-Command -ComputerName $server -ScriptBlock {
        # Get the last boot time. Getting it here up front once rather than many times within the loop is more efficient.
        $LastBootTime = (Get-CimInstance -ClassName Win32_OperatingSystem).LastBootUpTime;
    
        # Same principle for the cutoff date. No reason to have this in the loop.
        $CutoffDate = [datetime]::Today.AddDays(-10);
    
        # Loopy-da-loop time.
        Get-HotFix |
            Where-Object { $_.InstalledOn -gt $CutoffDate; } |
                Select-Object -Property @{name="LastBootUpTime"; expression={ $LastBootTime }}, CSName, Description, HotFixID, InstalledBy, InstalledOn;
    }

     

     

    Which gives you:

     

     

    Note that the real variable name for Source is CSName. You can re-brand that if you like back to Source using the same approach as for the new LastBootUpTime column but I haven't bothered doing so in this example.

     

    Cheers,

    Lain

  • LainRobertson's avatar
    LainRobertson
    Silver Contributor

    dmarquesgn 

     

    Hi,

     

    For a single additional column, you can just cram it into the Select-Object as shown below.

     

    If you were going to work with multiple new or existing columns, you'd probably branch out into a dedicated hash table.

     

     

    Invoke-Command -ComputerName $server -ScriptBlock {
        # Get the last boot time. Getting it here up front once rather than many times within the loop is more efficient.
        $LastBootTime = (Get-CimInstance -ClassName Win32_OperatingSystem).LastBootUpTime;
    
        # Same principle for the cutoff date. No reason to have this in the loop.
        $CutoffDate = [datetime]::Today.AddDays(-10);
    
        # Loopy-da-loop time.
        Get-HotFix |
            Where-Object { $_.InstalledOn -gt $CutoffDate; } |
                Select-Object -Property @{name="LastBootUpTime"; expression={ $LastBootTime }}, CSName, Description, HotFixID, InstalledBy, InstalledOn;
    }

     

     

    Which gives you:

     

     

    Note that the real variable name for Source is CSName. You can re-brand that if you like back to Source using the same approach as for the new LastBootUpTime column but I haven't bothered doing so in this example.

     

    Cheers,

    Lain

    • dmarquesgn's avatar
      dmarquesgn
      Iron Contributor
      Hi,
      Thanks, I've learned a new thing and acomplished the goal.

Resources