Using Invoke-Command, display which machine responded

Welcome Forums General PowerShell Q&A Using Invoke-Command, display which machine responded

Viewing 8 reply threads
  • Author
    Posts
    • #227833
      Participant
      Topics: 5
      Replies: 10
      Points: 87
      Rank: Member

      On a conference call today and my team was asked to look into why another teams machine BSOD’d since they couldn’t find anything. Found out dump files were off entirely. Got me wanting to check all our servers we monitor (3 months in to job) to find out if any of our servers have dumps disabled.

      I have the below, but I’m wanting to inject which server responded. Any help would be appreciated.

       

      • This topic was modified 4 months, 1 week ago by ALombardi01.
      • This topic was modified 4 months, 1 week ago by ALombardi01.
    • #227863
      Participant
      Topics: 8
      Replies: 562
      Points: 2,149
      Helping Hand
      Rank: Community Hero

      By default invoke-command returns property PSComputerName. You have to add parameter -HideComputerName to have it not return it. Now if it isn’t showing in the default output, you can ask for it.

      Output:

      Hope this helps

    • #227908
      Participant
      Topics: 13
      Replies: 1758
      Points: 3,153
      Helping Hand
      Rank: Community Hero

      Also, just as a side note, you are looping though the computers but not adding a -ComputerName param and it’s invoking the command on the local machine? Next, you may want to look at about_Remote_Variables as you are passing external variables into the scriptblock that is executing remotely. Those setting should be managed by GPO, so you may want to validate those policy settings, OU links and servers are in the correct OU.

      • This reply was modified 4 months, 1 week ago by Rob Simmers.
      • This reply was modified 4 months, 1 week ago by Rob Simmers.
    • #227911
      Participant
      Topics: 5
      Replies: 10
      Points: 87
      Rank: Member

      By default invoke-command returns property PSComputerName. You have to add parameter -HideComputerName to have it not return it. Now if it isn’t showing in the default output, you can ask for it.

      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

      Hope this helps

      I think I may have figured out another option. I had another command I ran to check versions of PowerShell on systems and I threw the $Computer command in there. Originally on this code it didn’t like it. I made it part of it’s own label/result now and it doesn’t complain anymore.

       

      Test Environment Results:

      Input:

       

      Output:

       

      Using it as Format-List (in lieu of table) returns the following, so it shows good either way I want to utilize the info. I’ll likely stick with a table.

    • #227920
      Participant
      Topics: 5
      Replies: 10
      Points: 87
      Rank: Member

      Also, just as a side note, you are looping though the computers but not adding a -ComputerName param and it’s invoking the command on the local machine? Next, you may want to look at about_Remote_Variables as you are passing external variables into the scriptblock that is executing remotely. Those setting should be managed by GPO, so you may want to validate those policy settings, OU links and servers are in the correct OU.

       

      Not sure if you are asking me or telling me, sorry. So I am only running the command against the local machine and not against the other computers?

    • #227935
      Participant
      Topics: 13
      Replies: 1758
      Points: 3,153
      Helping Hand
      Rank: Community Hero

      Sorry, that was a statement, not a question. You are executing the same code on your local machine, not the remote machine. Try something like this:

    • #228124
      Participant
      Topics: 5
      Replies: 10
      Points: 87
      Rank: Member

      Sorry, that was a statement, not a question. You are executing the same code on your local machine, not the remote machine. Try something like this:

      It doesn’t seem to like the use of the $Using variable. I’ll need to check it out more.

       

      I figured it out. I pulled the -ComputerName $Computer portion out of the ScriptBlock.

      Input:

      Output:

       

      I guess my question would be, what is the major benefit of doing it the way I did versus this?

       

       

      • This reply was modified 4 months, 1 week ago by ALombardi01.
      • This reply was modified 4 months, 1 week ago by ALombardi01.
      • This reply was modified 4 months, 1 week ago by ALombardi01. Reason: Trying to fix this quote dammit
      • This reply was modified 4 months, 1 week ago by ALombardi01.
      • This reply was modified 4 months, 1 week ago by ALombardi01.
    • #228199
      Participant
      Topics: 13
      Replies: 1758
      Points: 3,153
      Helping Hand
      Rank: Community Hero

      I guess my question would be, what is the major benefit of doing it the way I did versus this?

      First, sorry for the incorrect placement of the Computer param, was juggling multiple things. The major benefit is the code works. 🙂 The only reason the code you were running was working was it was local. When you execute the script block, that is all that the remote computer sees, basically:

      This would work:

      $Using basically replaces those variables with values when sending to the remote session, otherwise the remote system has no idea what any of those variables are as they are outside of the script block.

    • #228217
      Participant
      Topics: 5
      Replies: 10
      Points: 87
      Rank: Member

      I guess my question would be, what is the major benefit of doing it the way I did versus this?

      First, sorry for the incorrect placement of the Computer param, was juggling multiple things. The major benefit is the code works. 🙂 The only reason the code you were running was working was it was local. When you execute the script block, that is all that the remote computer sees, basically:

      PowerShell
      4 lines

      <textarea class=”ace_text-input” style=”opacity: 0; height: 18px; width: 6.59781px; left: 44px; top: 0px;” spellcheck=”false” wrap=”off”></textarea>

      1
      2
      3
      4
      Get-ItemProperty path | Format-Table @{Label=‘Server’;e={$Computer}},,,}
      #or
      Get-ItemProperty path NULL | Format-Table @{Label=‘Server’;e={$Computer}},NULL, NULL, NULL
      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

      This would work:

      PowerShell
      14 lines

      <textarea class=”ace_text-input” style=”opacity: 0; height: 18px; width: 6.59781px; left: 51px; top: 0px;” spellcheck=”false” wrap=”off”></textarea>

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      $Computers = (Get-ADComputer Filter * SearchBase “DC=Lombardi,DC=local”).Name
      # Script/Command to run against each machine found within the given OU
      $results = Foreach ($Computer in $Computers) {
      Invoke-Command ComputerName $Computer ScriptBlock {
      $Path = ‘HKLM:\System\CurrentControlSet\Control\CrashControl’
      $Key1 = ‘CrashDumpEnabled’
      $Key2 = ‘AlwaysKeepMemoryDump’
      $Key3 = ‘Overwrite’
      Get-ItemProperty path $using:Path ComputerName $computer |
      Select-Object Property $using:Key1, $using:Key2, $using:Key3
      }
      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

      $Using basically replaces those variables with values when sending to the remote session, otherwise the remote system has no idea what any of those variables are as they are outside of the script block.

      Gotcha. Thanks for clarifying.

Viewing 8 reply threads
  • The topic ‘Using Invoke-Command, display which machine responded’ is closed to new replies.