Hello everyone,<\/p>\n
I am having a hard time with this one. I am using this script to pull a list of local admins on all domain joined machines. The script works as intended and inputs the results on the console (this is the only script I have found online that works so far). I was wondering how to output the results onto a CSV. I keep running into errors when using the export-csv command. Any help would be greatly appreciated.<\/p>","upvoteCount":3,"answerCount":9,"datePublished":"2020-11-18T21:07:54.000Z","author":{"@type":"Person","name":"spiceuser-xoi83","url":"https://community.spiceworks.com/u/spiceuser-xoi83"},"acceptedAnswer":{"@type":"Answer","text":"
this seem to work fine for me,<\/p>\n
function get-localadmins {\n [cmdletbinding()]\n Param(\n [string]$computerName\n )\n if (test-connection $computer -Count 1 -Quiet) {\n $group = get-wmiobject win32_group -ComputerName $computerName -Filter \"LocalAccount=True AND SID='S-1-5-32-544'\"\n $query = \"GroupComponent = `\"Win32_Group.Domain='$($group.domain)'`,Name='$($group.name)'`\"\"\n $list = Get-WmiObject win32_groupuser -ComputerName $computerName -Filter $query\n $list | ForEach { $_.PartComponent } | ForEach {\n [pscustomobject]@{\n ComputerName = $computerName\n Accounts = $_.substring($_.lastindexof(\"Domain=\") + 7).replace(\"`\",Name=`\"\", \"\\\")\n }\n }\n }\n else {\n [pscustomobject]@{\n ComputerName = $computerName\n Accounts = \"Unable to reach $computername\"\n }\n }\n}\n \n$data = \nforeach ($computer in (Get-ADComputer -filter * | select name).name) {\n Write-Verbose \"Checking $computer\" -Verbose\n get-localadmins -computerName $computer\n}\n \n$data | export-csv \".\\Report.csv\" -NoTypeInformation -Force\n<\/code><\/pre>","upvoteCount":1,"datePublished":"2020-11-20T16:48:28.000Z","url":"https://community.spiceworks.com/t/exporting-a-csv-of-local-admins-on-all-domain-computer/782336/8","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},"suggestedAnswer":[{"@type":"Answer","text":"function get-localadmins{\n [cmdletbinding()]\n Param(\n [string]$computerName\n )\n $group = get-wmiobject win32_group -ComputerName $computerName -Filter \"LocalAccount=True AND SID='S-1-5-32-544'\"\n $query = \"GroupComponent = `\"Win32_Group.Domain='$($group.domain)'`,Name='$($group.name)'`\"\"\n $list = Get-WmiObject win32_groupuser -ComputerName $computerName -Filter $query\n $list | ForEach{$_.PartComponent} | ForEach{$_.substring($_.lastindexof(\"Domain=\") + 7).replace(\"`\",Name=`\"\",\"\\\")}\n}\nforeach($computer in (Get-ADComputer -filter * | select name).name){\nWrite-Verbose \"Checking $computer\" -Verbose\nget-localadmins -computerName $computer\n}\n<\/code><\/pre>\nHello everyone,<\/p>\n
I am having a hard time with this one. I am using this script to pull a list of local admins on all domain joined machines. The script works as intended and inputs the results on the console (this is the only script I have found online that works so far). I was wondering how to output the results onto a CSV. I keep running into errors when using the export-csv command. Any help would be greatly appreciated.<\/p>","upvoteCount":3,"datePublished":"2020-11-18T21:07:54.000Z","url":"https://community.spiceworks.com/t/exporting-a-csv-of-local-admins-on-all-domain-computer/782336/1","author":{"@type":"Person","name":"spiceuser-xoi83","url":"https://community.spiceworks.com/u/spiceuser-xoi83"}},{"@type":"Answer","text":"
how about like so?<\/p>\n
function get-localadmins{\n [cmdletbinding()]\n Param(\n [string]$computerName\n )\n $group = get-wmiobject win32_group -ComputerName $computerName -Filter \"LocalAccount=True AND SID='S-1-5-32-544'\"\n $query = \"GroupComponent = `\"Win32_Group.Domain='$($group.domain)'`,Name='$($group.name)'`\"\"\n $list = Get-WmiObject win32_groupuser -ComputerName $computerName -Filter $query\n $list | ForEach{$_.PartComponent} | ForEach{$_.substring($_.lastindexof(\"Domain=\") + 7).replace(\"`\",Name=`\"\",\"\\\")}\n}\nforeach($computer in (Get-ADComputer -filter * | select name).name){\nWrite-Verbose \"Checking $computer\" -Verbose\nget-localadmins -computerName $computer |\nexport-csv \"report.csv\" -notypeinformation -append\n}\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2020-11-18T21:47:18.000Z","url":"https://community.spiceworks.com/t/exporting-a-csv-of-local-admins-on-all-domain-computer/782336/2","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"