Script for exporting local admins in a csv

I need to export this to a CSV

Please help

function get-localadmins{
  [cmdletbinding()]
  Param(
  [string]$computerName
  )
  $group = get-wmiobject win32_group -ComputerName $computerName -Filter "LocalAccount=True AND SID='S-1-5-32-544'"
  $query = "GroupComponent = `"Win32_Group.Domain='$($group.domain)'`,Name='$($group.name)'`""
  $list = Get-WmiObject win32_groupuser -ComputerName $computerName -Filter $query
  $list | ForEach{$_.PartComponent} | ForEach{$_.substring($_.lastindexof("Domain=") + 7).replace("`",Name=`"","\")}
}

foreach($computer in (Get-ADComputer -filter * | select name).name){
Write-Verbose "Checking $computer" -Verbose
get-localadmins -computerName $computer
}
MargaritaSystems AdministratorAsked:
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.

KimputerCommented:
CSV based on comma (Otherwise, just look at the code, replace comma with ;, and replace the ; with a comma, in line 17, 20 and 23):

function get-localadmins{
  [cmdletbinding()]
  Param(
  [string]$computerName
  )
  $group = get-wmiobject win32_group -ComputerName $computerName -Filter "LocalAccount=True AND SID='S-1-5-32-544'"
  $query = "GroupComponent = `"Win32_Group.Domain='$($group.domain)'`,Name='$($group.name)'`""
  $list = Get-WmiObject win32_groupuser -ComputerName $computerName -Filter $query
  $list | ForEach{$_.PartComponent} | ForEach{$_.substring($_.lastindexof("Domain=") + 7).replace("`",Name=`"","\")}
}

$outfile = "d:\results.csv"

#header
"Computer,Local_admins" | Out-File $outfile -Encoding utf8
#data
foreach($computer in (Get-ADComputer -filter * | select name).name){
[System.IO.File]::AppendAllText($outfile,$computer + ",",[System.Text.Encoding]::UTF8)
Write-Verbose "Checking $computer" -Verbose
$list1 = get-localadmins -computerName $computer
$result1 = $list1 -join ";" | Out-File $outfile -Append -Encoding utf8
} 

Open in new window


Output:

Computer,Local_admins
PC101,"PC101\Administrator";"NLPC102\admin"
PC102,"PC102\Administrator";"NLPC102\admin"



Obviously, replace the $outfile variable with your real file location.
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
Shaun VermaakTechnical SpecialistCommented:
If you do not want to keep auditing remote computers and rather manage local administrators via AD, you can use my process
https://www.experts-exchange.com/articles/29652/Strategy-to-centrally-manage-Local-Administrators-group-from-Active-Directory.html
0
MargaritaSystems AdministratorAuthor Commented:
Thank you
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
Active Directory

From novice to tech pro — start learning today.