none
Piping ADComputer to Registry Check RRS feed

  • Question

  • 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"
    }
    Thursday, August 18, 2016 3:43 PM

Answers

  • Maybe I'm being misunderstood.  What I want to accomplish is to query computer objects in AD and then run the list that is returned and check for a registry value.  If the value exists, then write to a file or to the display, if the value is false write a different response to a file or the display.

    I've figured it out, the end result isn't too different...I'd have expected the larger brains on here to see what I wanted and point me in the right direction.

    $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}
    }



    Friday, August 19, 2016 1:36 PM

All replies

    • Proposed as answer by jrv Friday, August 19, 2016 7:32 AM
    • Unproposed as answer by USMCGrunt0307 Friday, August 19, 2016 12:48 PM
    Thursday, August 18, 2016 4:31 PM
  • Ya....I'm not super-versed in scripting so even with him breaking down the commands, I don't fully understand the syntax to be able to apply what he's talking about. I also tried using his function that adds the Get-PendingReboot command but when piping in my list of computers I get: 

    The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)

    If I run the same command against an individual computer it works fine...so I don't know what that's about.

    Friday, August 19, 2016 5:13 AM
  • I suggest starting by learning PowerShell. It is not possible to understand you issue because you are not giving us any usable information.

    \_(ツ)_/

    Friday, August 19, 2016 5:27 AM
  • Umm.....it's not a difficult issue to understand, even the simple commands I gave in the OP can be read and understood fairly easily.  On top of that, each block of scripting I put a heading to help in understanding its intended purpose.  I can reorganize my initial post:

    Problem: Have many servers

    Need:  Want see if server is pending reboot

    What I know: 

       Compile list of AD Computers in Powershell using: 

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

       Query registry key for existence and write to screen

          $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"
    }

    Friday, August 19, 2016 7:21 AM
  • You cannot use that code to check remote registry.  The link I posted shows you how to do this.

    \_(ツ)_/

    Friday, August 19, 2016 7:32 AM
  • It's unfortunate that you're so headstrong in thinking it can't be done, powershell is actually quite versatile.  I've got the code to work, to an extent, I'll post an update when I can get a copy of the error I'm getting.
    Friday, August 19, 2016 12:49 PM
  • You can't use that code on remote machines unless you're using Invoke-Command. What jrv is telling you is correct.

    Friday, August 19, 2016 1:09 PM
  • Maybe I'm being misunderstood.  What I want to accomplish is to query computer objects in AD and then run the list that is returned and check for a registry value.  If the value exists, then write to a file or to the display, if the value is false write a different response to a file or the display.

    I've figured it out, the end result isn't too different...I'd have expected the larger brains on here to see what I wanted and point me in the right direction.

    $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}
    }



    Friday, August 19, 2016 1:36 PM
  • That's exactly what was in jrv's original link, so I have no idea what you're complaining about.

    Friday, August 19, 2016 1:47 PM
  • If you call what I wrote the same as this....I just...I don't have words for it. While the desired end result may be the same, the actual end result is not (His script/function doesn't work for me). Not to mention Brian's code is WAY more complex (and most probably, more elegant). The blog, I'm sure, does a good job of explaining itself to someone who knows Powershell and WMI, it doesn't do much for a novice (which I explicitly state I am in my first response to jrv).

    This is a forum intended to help spread knowledge, if I'm not mistaken.  Telling someone to go read a page and figure it out (especially when the question asked is relatively simple) is not spreading knowledge, it's pushing people away.

    Friday, August 19, 2016 2:18 PM
  • You are mistaken. This isn't a 'teach me to do something' forum, it's for technicians who are asking specific questions.

    Links to previously written explanations are always acceptable here. If you don't understand what is being proposed, the onus is on you to do additional research. You can always ask specific questions as long as you demonstrate that you've already tried to solve the problem yourself.

    I help plenty of novices here, but I always stop helping once I determine that the OP doesn't want to read and learn.

    Think of it this way - we don't have time to teach. We're not paid for this, we're volunteers.


    Friday, August 19, 2016 2:29 PM
  • I am sorry.  The script I posted does exactly what you asked and requires no special knowledge to run.  It also explains why it works.  It was perfect for a Novice.  If you just want someone to read your mind and do exactly what you are thinking of then this is not the forum for you.  The assumption here is that you are a trained technician and not just someone looking for free consulting or script writing.

    I hope in your searching around that you have learned a little bit about programming with computers.  I am sure you have learned something more than when you started.  Like so many you seem to not want to read things that are more than a tweet.  Had you actually read the blog you would be one step closer to being a successful Windows tech.


    \_(ツ)_/

    Friday, August 19, 2016 2:33 PM
  • I know there's no winning with you guys and your minds are made up about what I am but...

    That may be so, that it requires no "special" knowledge to run, but having a little bit of "special" knowledge, whatever that is, causes it to not run.  Go ahead, run the Get-ADComputer string that I have up in the OP and see it return an error with the Get-PendingReboot function.  I'm genuinely positive you know exactly why it's happening and what needs to be done to resolve it to make it work with the pendingreboot script.  Instead of trying to understand why a 'cut and paste' solution isn't working, you just assumed I'm someone expecting a 'tweet' answer and basically tell me to F off.  What exactly is a 'trained technician' in the context of using this forum, MCSE, decades of experience in the industry with scripting knowledge?  I'll admit, I've only been in the IT industry for a few years and have a degree and some certifications that prove I know some things but I guess I'm not a 'trained technician'.

    I did learn something, its unfortunate though that it took literally hours of searching, experimentation and hacking together my own script to stumble onto a solution that works for the 'cut and paste' solution you provided instead of you taking a minute to look at it and say,

    "Oh well, the get-adcomputer query you're running is building a list of a property.  This isn't the same as what the get-pendingreboot is asking for.  See x, y, or z for an explanation." 

    Even knowing how to fix my problem doesn't mean I understand why it even works!  NOW I have to go and search and learn why there's a difference after having wasted so many hours that could have been used to learn.

    Friday, August 19, 2016 3:32 PM
  • Works absolutely perfectly.  You have to read the directions for any and all CmdLets you use.  Reading is the most important technical lesson a technician can learn to do as a habit.

    Get-AdComputer -Filter * | %{Get-PendingReboot $_.Name}|ft

    This would have not taken more than 10 minutes had you not been so stubborn about actually reading the links posted.  You are just grabbing things and throwing them at a prompt and then crying foul.

    Each person who comes to computing must learn these things to become technically proficient.  Many refuse and flouder in frustration,  Those who relax and perform the basic exercises become proficient and independent quickly.

    If you want to play baseball you better start by learning how to throw and catch a ball.  You cannot start as a pitcher if you have not learned these things. You will forever remain a spectator.


    \_(ツ)_/



    • Edited by jrv Friday, August 19, 2016 3:50 PM
    Friday, August 19, 2016 3:46 PM
  • Show me where it says what I was doing isn't going to work, I've read it three times now and don't see anything about it.  Additionally, your string that you typed there is not anywhere either, what does the % do, that character doesn't even exist on the blog page you linked.

    Friday, August 19, 2016 3:53 PM
  • Even knowing how to fix my problem doesn't mean I understand why it even works!  NOW I have to go and search and learn why there's a difference after having wasted so many hours that could have been used to learn.

    You must start by actually formally studying any technical system. You must study and learn the basics.

    Here is one simple place to start.  https://technet.microsoft.com/en-us/dd742419

    There are also MVA videos for PowerShell beginners and there are many books for beginners.  Here are a few dozen to start with: https://www.amazon.com/s/ref=nb_sb_ss_c_2_10?url=search-alias%3Dstripbooks&field-keywords=powershell&sprefix=powershell%2Caps%2C146


    \_(ツ)_/

    Friday, August 19, 2016 3:55 PM
  • If you don't know what % does you're trying to run before you've even learned to walk.

    % is an alias for ForEach-Object.


    Friday, August 19, 2016 3:55 PM
  • Show me where it says what I was doing isn't going to work, I've read it three times now and don't see anything about it.  Additionally, your string that you typed there is not anywhere either, what does the % do, that character doesn't even exist on the blog page you linked.

    If you learned the basics you would, in the first few lessons, have all of those answers and dozens more.

    As noted above.  We are not here to teach you PowerShell  We assume that those coming here are technicians who do their homework first.  The basics are your responsibility.


    \_(ツ)_/

    Friday, August 19, 2016 3:57 PM
  • Sorry about any frustration. There is a learning curve with PowerShell, and we've all experienced it.

    There's something you need to understand: There is an assumption in technical forums that you have at least some fundamental understandings of the technology or tool you're asking about. A forum is not interactive by nature and is not a good place to try to learn how to use a new technology.

    Of course, we can answer fundamental questions, but it's not a good use of our time (we are volunteers, after all) - particularly when a simple web search can answer most of these kinds of questions. (Why wait for an answer in a forum when you can find it yourself?)

    Keep in mind that the participants here are volunteers (not Microsoft employees) and there's no SLA.


    -- Bill Stewart [Bill_Stewart]

    Friday, August 19, 2016 5:09 PM
    Moderator