Powershell; Piping information

USMCGrunt

2[H]4U
Joined
Mar 19, 2010
Messages
3,103
Hey guys, I am needing to find out servers that are pending reboot in a large environment. I put together a one-liner powershell command that will pull the servers I want to check, and I know I want to check a certain registry key, but I don't know how to pipe the information from one command into the other. So here's what I got, if you guys can help me out, I'd appreciate it.


Pull list of servers
$Computers = Get-ADComputer -Filter * -SearchBase 'OU=s,OU=n,dc=o,dc=t' | Where-Object {$_.Name -like '??s*'} | Select Name

Check Registry Key
$val = Get-ItemProperty -Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\Component Based Servicing" -Name "RebootPending" -ErrorAction SilentlyContinue
if($val.RebootPending -eq 1)
{
Write-Host "Pending Reboot"
}
ELSE
{
Write-Host "No Reboot Pending"
}
 
I got it to work using the script below. The only issue I have now is that some of these objects aren't on the network any longer (I'm a new employee and have no idea why they're enabled if they don't exist, so don't get on me about it, lol). I'd like to be able to take what's in $Server, ping the hosts, take those that respond and stick them in a new variable, and run that new variable in the foreach statement.

$Server = Get-ADComputer -Filter * | Where-Object {$_.Name -like '??s*'} | Select -ExpandProperty Name | Sort
Foreach ($Name in $Server) {
$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $Name)
$regKey= $reg.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending")
if ($regKey -eq $null){
Write-Output "$Name No Reboot"
} else {
Write-Output "$Name Reboot" | Out-File -Append C:\temp\ServerList.txt}
}
 
USMCGrunt, this answer may help you as well adding a timeout to batch/powershell

Basically, instead of pinging the host (which may not work even if it's up as many hosts don't respond), decide a timeout and run the remote registry commands in a script block that you wait with a specified timeout. For those that time out, pass them down the pipeline as error values with Write-Error and continue processing.
 
USMCGrunt, this answer may help you as well adding a timeout to batch/powershell

Basically, instead of pinging the host (which may not work even if it's up as many hosts don't respond), decide a timeout and run the remote registry commands in a script block that you wait with a specified timeout. For those that time out, pass them down the pipeline as error values with Write-Error and continue processing.
Turns out the PendingReboot that you linked to DOES work, it was my Get-ADComputer command that was the issue. I was piping the results into and selecting a property (name), apparently this is stored as more than just a text readout of the machine name. By using the ExpandProperty argument with Select it, I guess, turned the values into 'true' text. I've done some light research trying to find out what ExpandProperty does exactly but haven't found anything.

Regardless, I still have the same issue with having non-responsive machines. I tested it in full today and came back with a couple Access Denied responses as well. The problem I have with using pendingreboot is that when I pipe it into Out-File, it doesn't include errors. I somewhat understand errors are printed to display on a different output stream....or something like that, so now I need to solve how to get ALL output redirected to a file.

I like this timeout script you linked to btw, I'm definitely gonna try and incorporate it, thanks Tawnos.
 
Back
Top