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?
 
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
 
Shaun VermaakTechnical Specialist/DeveloperCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.