With PowerShell how do I export the remote administrators from a list of desktops into a CSV file?

Hello Experts,

My goal is to loop through the sample.txt file where we'll find a list of desktops. Afterward I'd like to record the Administrators membership list from each remote desktop and export to the test.csv file a comprehensive appended list of remote Administrators on each system. Any suggestions?

Each time I run the code I receive this message:
"cmdlet ForEach-Object at command pipeline position 1:
Supply values for the following parameters:
Process [0]"

*************************************************
Set-StrictMode -Version Latest
Set-ExecutionPolicy RemoteSigned -Force

$GroupName = "Administrators"
$AllGroups = @()
$OutputFile = "test.csv"
$ComputerName = Get-Content c:\Temp\Scripts\sample.txt

ForEach-Object -Begin {Test-Connection -ComputerName $ComputerName -Buffersize 16 -Count 2 -TimeToLive 5 -Quiet}
       -Process {
       $objWMI = (Get-WmiObject -ComputerName $ComputerName -Query "SELECT * FROM Win32_GroupUser WHERE GroupComponent=`"Win32_Group.Domain='$ComputerName',Name='$GroupName'`"")
          If ($objWMI -ne $Null) {
            ForEach ($objItem In $objWMI) {
                $DomainName = $objItem.PartComponent.Split(",")[0].SubString($objItem.PartComponent.Split(",")[0].IndexOf("`"")).Replace("`"", "")
                $UserName = $objItem.PartComponent.Split(",")[1].Replace("Name=", "").Replace("`"", "")
                $arrUsers = [Ordered]@{
                    ComputerName="$ComputerName"
                    Group="$GroupName"
                    Member="$DomainName\$UserName"
                                           }
                $objUsers = New-Object PsObject -Property $arrUsers
                $AllGroups += $objUsers
            }
        }
    }
      -End  
{$AllGroups | Export-csv $OutputFile -append -NoTypeInformation}
CuriousMAUserAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dan McFaddenSystems EngineerCommented:
Here is an article with a better method.  The script is done and ready to use.  It also avoids having to use WMI to any operations.

link:  https://4sysops.com/archives/create-a-list-of-local-administrators-with-powershell/

Dan
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CuriousMAUserAuthor Commented:
Thank you, Dan.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.