Forum Discussion
Add column to an output
- Sep 20, 2022
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
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
Thanks, I've learned a new thing and acomplished the goal.