none
Powershell commmand (Return all connected RDP sessions in domain)

    Question

  • I am running this command, and funny enough, it works, but only on 1 computer (The one I am one I suspect)

    How do I get this to run all the returned computers?

    Bonus points,  return the name as well

    Get-ADComputer -Filter * -prop name| select name |quser /server:$_ ;select $_


    BlankMonkey

    Tuesday, December 4, 2018 1:59 AM

Answers

  • No, it does not work;


    BlankMonkey

    It doesn't work because you are still using your bad code.

    It also only works against RDS servers.

    Your code is wrong.  You cannot use "select $_" in a new line. If you would format code correctly this would become obvious.

    You code formatted correctly.

    Get-ADComputer -Filter * -prop name |
        Select-Object name | quser /server:$_
    Select-Object $_
    Can you see what you did and are doing wrong? Can you see why my code was different?


    \_(ツ)_/

    • Marked as answer by BlankMonkey Tuesday, December 4, 2018 11:28 PM
    Tuesday, December 4, 2018 5:35 PM

All replies

  • What?  The command returns all computers in the domain.

    TO get local users drop the select.

    Get-ADComputer -Filter *| ForEach-Object{ quser /server $_.Name}

    Don't you think it is about time to actually learn PowerShell instead of guessing every time you want to write a script?  Your guesses are actually getting worse with each attempt.  One year and you are learning backwards.

    "Blank Monkey".  Even monkeys are better at learning.  Yes monkeys mostly just mindlessly copy everything but they actually do think at times.  Don't make the monkeys the only thinkers.  Exercise your brain and learn PowerShell.


    \_(ツ)_/

    Tuesday, December 4, 2018 3:56 AM
  • wow.

    BlankMonkey

    Tuesday, December 4, 2018 5:24 PM
  • No, it does not work;


    BlankMonkey

    Tuesday, December 4, 2018 5:27 PM
  • No.  Just do it.  It will  make PS more fun.  You will also stop making the same set of mistakes.

    This is a very good place to start.  You may have to do this more than once to understand what it is trying to teach you but it is worth the time.

    Microsoft Virtual Academy - Getting Started with Microsoft PowerShell


    \_(ツ)_/

    Tuesday, December 4, 2018 5:28 PM
  • No, it does not work;


    BlankMonkey

    It doesn't work because you are still using your bad code.

    It also only works against RDS servers.

    Your code is wrong.  You cannot use "select $_" in a new line. If you would format code correctly this would become obvious.

    You code formatted correctly.

    Get-ADComputer -Filter * -prop name |
        Select-Object name | quser /server:$_
    Select-Object $_
    Can you see what you did and are doing wrong? Can you see why my code was different?


    \_(ツ)_/

    • Marked as answer by BlankMonkey Tuesday, December 4, 2018 11:28 PM
    Tuesday, December 4, 2018 5:35 PM
  • To access non-RDS servers do this:

    Get-ADComputer -Filter * |
        ForEach-Object{
            Invoke-Command -ComputerName $_.Name { quser }
        }


    \_(ツ)_/

    Tuesday, December 4, 2018 5:38 PM
  • Thank you. I only spend a very small amount of time doing Powershell, but I will definitely look at the academy you suggested. I really like PS, and wish I could do more, but time is the enemy, hence my repeated mistakes.

    No, I don't understand, can you be more specific;

    • Formatting: I see my command reformatted by you, but I do not see the difference, beyond different lines.  Are you saying it is a matter of good coding practice?  I am good with that, and will adjust.
    • "It doesn't work because you are still using your bad code." : I am trying to show in the picture that the command 'works' in the since that it does not return an error, and does return a result but for only one computer.  How would you better phrase it?
    • "It also only works against RDS servers." I am not entirely sure this is true.  My testing on single computers shows that the quser command does return results.  But all my computers do have RDP turned on, but it stands to reason, because I am looking for all the rdp sessions anyway.  I can run the command manually 1 by 1 and the results do get returned (with an expected error if there is no rdp)
    • "You cannot use "select $_" in a new line."  I think this is the core issue here, I do not understand this.  A link to a tutorial is fine, but it does not flow in my understanding

    This is what I am expecting it to do

    Get-ADComputer -Filter * -prop name

    ----------------------Get an array of all the computers in the domain

     | select name

    ---------------------Take the array and turn it into a list

     | quser /server:$_ ;select $_

     Using the list by the $_ variable, run the two commands

    I am asking less of "How to do it" and more of "What did I do wrong"


    BlankMonkey

    Tuesday, December 4, 2018 7:56 PM
  • Many difference but because you don't have any PowerShell training you can't see them.  Start by answering my question about the select statement that won't work as you have written it.

    For formatting instructions and standards go here: PowerShell Style Guidelines

    To learn and understand the pipeline read this:

    help about_pipelines -showwindow

    What you did wrong is obvious once you understand how to use the pipeline.  Reading the help will fix you incorrect understanding of how it works.  I cannot correctly explain it in two lines because you have too many misunderstandings that have to be changed before you understand the pipeline.

    Also learn what this ";" character is and does and why you don't need it and why it can cause issues when used incorrectly as you are using it.

    I will note this.  It is not possible to learn PowerShell by copying what you see in forums and most blogs will be only slightly helpful or very misleading if you do not know basic PowerShell.


    \_(ツ)_/

    Tuesday, December 4, 2018 8:29 PM
  • To be more specific.

    Here is what I posted to fix your script. 

    Get-ADComputer -Filter * | ForEach-Object{ quser /server $_.Name }
    

     It is all on one line which I will do for very simple lines but the following is the preferred format for readability and understandability.

    Get-ADComputer -Filter * |
        ForEach-Object{
            quser /server $_.Name
        }

    Having the ForEach-Object start on a new line makes it explicit to the eye that we are going to enumerate the contents of a pipeline.  When a command is too long some may not notice the "ForEach" dangling at the end.

    Notice we do not need to specify default properties with Get-AdUser but they must be specified in the ForEach-Object block.

    There is no need to select "Name" because it is already there and can just be referenced.  It is a pointless step.

    "quser" cannot take pipeline input so we must explicitly define the property we want.

    Putting a semicolon at the end of a pipeline is useless.  Using a pipeline property outside of a pipeline won't work.  It will just be null and you will get an error.

    "quser" will fail on all non-RDS servers remotely if the firewall gas not been opened to allow its use. RDS servers open that port by default.


    \_(ツ)_/

    Tuesday, December 4, 2018 9:01 PM
  • I very much appreciate the web tutorial and the pipe help (this looks interesting). As mentioned before, now I just need to find the time. Thank you for your help.

    BlankMonkey

    Tuesday, December 4, 2018 11:29 PM
  • It is taking you more time to ask questions and fight to understand the answers.  You would save that 100 times over by learning PowerShell.  "No time" is as bad as "the dog ate my homework".


    \_(ツ)_/

    Tuesday, December 4, 2018 11:45 PM