title: Trouble executing powershell script on multiple remote machines
body: <p>I need to generate a list of all users on our network who are members of their workstation's local administrators group. I found a script here <a href="https://gallery.technet.microsoft.com/scriptcenter/List-local-group-members-762b48c5#content" rel="nofollow">https://gallery.technet.microsoft.com/scriptcenter/List-local-group-members-762b48c5#content</a> which was written to list local group members by executing a WMI query through Powershell. I've tested this script and it works well, but I've been trying to modify it to take in a list of computers to check and that's where I've run into trouble. Here's what I've done:</p>
  
  <pre><code>function LocalAdmins
  {
    param([string]$GroupName = "Administrators")
  
      begin
      {
          # Get all workstations listed in this text file
          $WorkStations = Get-Content -Path C:\useful_lists\testLocal.txt
  
          # Initialize an array to hold the results of the query
          $arr = @()
  
      # hash table for storing computer name, member pairings
      $hash = @();
      }
  
      process
      {
          foreach ($machine in $WorkStations)
          {
              $wmi = Get-WmiObject -ComputerName $machine -Query `
                  "SELECT * FROM Win32_GroupUser WHERE GroupComponent=`"Win32_Group.Domain='$machine',Name='$GroupName'`""
  
              # Parse out the username from each result and append it to the array
              if ($wmi -ne $null)
              {
                  foreach($item in $wmi)
                  {
                      $arr += ($item.PartComponent.Substring($item.PartComponent.IndexOf(',') + 1).Replace('Name=', '').Replace("`"", ''))
                  }
              }
  
              # Return a hash table comprised of two columns: Computer Name &amp; Members
              $hash += @{ComputerName=$machine;Members=$arr}
          }
      }
  
      end
      {
          return $hash
      }
  }
  </code></pre>
  
  <p>When I ran the unmodified script here's what I got as output:</p>
  
  <pre><code>PS &gt; (Get-LocalGroupMembers -ComputerName "&lt;COMPUTER NAME&gt;" -GroupName "Administrators").Members
  
  ACCOUNTNAME
  ACCOUNTNAME
  ACCOUNTNAME
  
  PS &gt;
  </code></pre>
  
  <p>However, when I run the version of this script that I modified I get this:</p>
  
  <pre><code>PS &gt; (LocalAdmins -GroupName "Administrators").Members
  PS &gt;
  </code></pre>
  
  <p>I'm fairly certain that the issue lies either in how I've setup the first foreach loop to run the wmi query or how the results of that query are being stored in the hash table. I'm not sure what I could do differently to fix the issue.</p>
  
  <p>Thanks in advance to anyone who can help!</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Per mortenya's suggestion, I edited my test text file to only include one computer in it. Doing so, along with taking out the <code>foreach ($machine in $computers)</code> loop worked as expected producing the following result:</p>
  
  <pre><code>&gt;&gt; LocalAdmins -GroupName "Administrators"
  
  Name               Value
  ----               ----
  ComputerName       {computerName.domain}
  Members            {account, account, account, account}
  
  &gt;&gt;
  </code></pre>
  
  <p>However, going back and trying to get this to work when incorporating multiple machines using the code above (I've updated it since my initial post), I get the following:</p>
  
  <pre><code>&gt;&gt; LocalAdmins -GroupName "Administrators"
  
  Name               Value
  ----               -----
  ComputerName       computerName1.domain
  Members            {}
  ComputerName       computerName2.domain
  Members            {}
  
  &gt;&gt;
  </code></pre>
  
  <p>Why is it that with one machine in the list I can get the members of the Administrator group, but adding a second computer to the list makes it so I can not retrieve members from that group on either machine?</p>