5 Replies Latest reply on Oct 6, 2019 9:54 AM by JStars

    PowerCLI/PowerShell script to retrieve local admins on Windows servers

    JStars Enthusiast

      Hello, I hope I'm not too cheeky to ask the question here... I'm trying to retrieve the local admins group members of all Windows VM servers and export them to a csv. I wrote this code but it only exports to csv one server. So the loop is not correctly set, however I'm new to this -scriptblock feature and I don't know how to append all VMs data:


      $winVMS = Get-VM  | ?{$_.Guest.GuestFamily -like "win*" -and $_.PowerState -eq "PoweredOn"  }
      $Credential = Get-Credential
      ForEach($vm in $winVMS){
          $Computer = $vm + "<domainName>"
          Invoke-Command -ComputerName $Computer -Credential $Credential -ScriptBlock { 
              Get-LocalGroupMember -Group "Administrators" 
          } | Export-Csv -Path 'C:\Users\xxxxxx\Desktop\localAdmins.csv' -NoTypeInformation