Invoke-Command How to control error output help needed

I want to be able to pipe the PSCustomObject’s out. It’s not working and I’ve tried everything I can think of.

I have it setup like this so I get no error output to the screen. I want to create a list of all machines that could not be connected to by Invoke-Command.

Can anyone help or make this work or re-write it please. This is getting pending reboot info.

I am trying to come up with a template for using just code in a script block and output the results and failures formated.

| Select-Object ‘Computer Name’,‘Reboot Required’,‘Status’ | Sort-Object -Property PSComputerName | Format-Table -AutoSize

$sb = {
$RebootRequired = $False

Try {
if (Test-Path -Path “HKLM:\Software\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending”) { $RebootRequired = $true }
if (Test-Path -Path “HKLM:\Software\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootInProgress”) { $RebootRequired = $true }
if (Test-Path -Path “HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired”) { $RebootRequired = $true }
if (Test-Path -Path “HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Services\Pending”) { $RebootRequired = $true }
if (Test-Path -Path “HKLM:\Software\Microsoft\Windows\CurrentVersion\Component Based Servicing\PackagesPending”) { $RebootRequired = $true }
if (Test-Path -Path “HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\PostRebootReporting”) { $RebootRequired = $true }

if ($RebootRequired -eq $True) { $RebootRequired = $True }
$status = “Success”
}
Catch {
$status = “Failed”
}

[PSCustomObject]@{
‘ComputerName’= $env:COMPUTERNAME
‘RebootRequired’ = $RebootRequired
‘Status’= $status
}
}

$ComputerList = ‘online-pc’,‘online-pc’,‘offline-pc’
Try {
Invoke-Command -ComputerName $ComputerList -ScriptBlock $sb -ErrorAction Stop
}
Catch {
[PSCustomObject]@{
‘ComputerName’= $_
}
}

I use Get-PendingReboot which is in the PowerShell gallery.

PS C:\temp> 'D01','D03' | Get-PendingReboot

Computer : D01
CBServicing : False
WindowsUpdate : False
CCMClientSDK :
PendComputerRename : False
PendFileRename : True
PendFileRenVal : {\??\C:\Windows\system32\spool\V4Dirs\EB63AFFA-5845-43E7-8290-1F042362EC28\9a072afe.BUD, ,
\??\C:\Windows\system32\spool\V4Dirs\EB63AFFA-5845-43E7-8290-1F042362EC28\9a072afe.gpd, ...}
RebootPending : True

Computer : D03
CBServicing : False
WindowsUpdate : False
CCMClientSDK :
PendComputerRename : False
PendFileRename : True
PendFileRenVal : {\??\C:\Windows\system32\spool\V4Dirs\DF7DDC25-E2AD-4E77-A1B8-A861FAD66BC3\9a072afe.BUD, ,
\??\C:\Windows\system32\spool\V4Dirs\DF7DDC25-E2AD-4E77-A1B8-A861FAD66BC3\9a072afe.gpd, ...}
RebootPending : True

What I am trying to do is blast through 1200 machines that have psremoting enabled. I can’t do foreach computer like the get-pendingreboot does. It would take forever. If I run invoke-command as a scriptblock as what I like to call a Blaster. I can hit all 1200 machines really fast and wait for their results to come back and then organize those results and then output it to my screen or to a CSV file. I’m just having trouble with getting the organized output to return to me correctly. I appreciate your help.

Checks to see if there is a pending reboot required to finish windows updates.

$ScriptBlock = {
##########
$RebootRequired = $False
$ErrorActionPreference = ‘Stop’

Try {

if (Test-Path -Path “HKLM:\Software\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending”) { $RebootRequired = $true }
if (Test-Path -Path “HKLM:\Software\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootInProgress”) { $RebootRequired = $true }
if (Test-Path -Path “HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired”) { $RebootRequired = $true }
if (Test-Path -Path “HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Services\Pending”) { $RebootRequired = $true }
if (Test-Path -Path “HKLM:\Software\Microsoft\Windows\CurrentVersion\Component Based Servicing\PackagesPending”) { $RebootRequired = $true }
if (Test-Path -Path “HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\PostRebootReporting”) { $RebootRequired = $true }

$status = “Success”
}

Catch {
$status = “Failed”
}

[PSCustomObject]@{
‘ComputerName’= $env:COMPUTERNAME
‘RebootRequired’ = $RebootRequired
‘Status’= $status
}
##########
} # End ScriptBlock

Write-Host -ForegroundColor Yellow “Checking all Windows computers for a required reboot to install Windows Updates.”

$StartTime = (Get-Date)
Write-Host -ForegroundColor Green ('Start time: ’ + $StartTime)

$ComputerList = Get-ADComputer -Filter {(enabled -eq “true”) -and (OperatingSystem -Like “Windows 10”)} | Select-Object -ExpandProperty Name

$Results = Invoke-Command -ComputerName $ComputerList -ErrorAction SilentlyContinue -ScriptBlock $ScriptBlock
Clear-Host
$Results | Select-Object ComputerName,RebootRequired,Status | Sort-Object ComputerName | Format-Table

Write-Output “”
Write-Output ‘Could Not Connect To PowerShell Remoting…’
$FailedConnections = Compare-Object $ComputerList $Results.ComputerName
#----------

Adds failed computers to a forth column but

computer names will not be on the same column.

#$Failed = $FailedConnections.InputObject | ConvertFrom-Csv -Header FailedToConnect
#$Failed
#----------
$Failed = foreach ($Item in $FailedConnections.InputObject) {
[PSCustomObject]@{
‘ComputerName’ = $Item
‘RebootRequired’ = ‘—’
‘Status’ = ‘Failed to connect’
}
}
$Failed | Select-Object ComputerName,RebootRequired,Status | Sort-Object ComputerName | Format-Table

Output all results and failures to a csv file.

$OutputToFile = $Results + $Failed
$OutPutToFile | Select-Object ComputerName,RebootRequired,Status | Export-CSV “C:\Users$env:USERNAME\Desktop\PendingReboot.csv”

Write-Output “”
Write-Output ('Start time: ’ + $StartTime)
Write-Output ('End time: ’ + (Get-Date))