Good Morning,<\/p>\n
I hope everyone is doing well. I am working on an issue for work. We are needing to audit all computers on our network to get list of all local admin accounts. I would like to export the list to CSV file. Here is my current script.<\/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.PartComponent | % {$_.substring($_.lastindexof(\"Domain=\") + 7).replace(\"`\",Name=`\"\",\"\\\")}\n\n foreach($computer in (Get-ADComputer -Filter * -SearchBase 'OU=XXXX,DC=XXX,DC=XXX' | select name).name){\nWrite-Verbose \"Checking $computer\" -Verbose\nget-localadmins -computerName $computer \n}\n<\/code><\/pre>\n
Advertisement
It works fine as long as the computers are turned on. What would I need to add to Export it to CSV file? I have seen some examples where they just pipe it using Export-CSV. Would that be it? If so where would i need to add it? I am still new and learning. I tried to research it before posting.<\/p>\n
Thanks,<\/p>","upvoteCount":6,"answerCount":8,"datePublished":"2022-03-16T13:59:29.000Z","author":{"@type":"Person","name":"jeffbearden2425","url":"https://community.spiceworks.com/u/jeffbearden2425"},"suggestedAnswer":[{"@type":"Answer","text":"
Good Morning,<\/p>\n
I hope everyone is doing well. I am working on an issue for work. We are needing to audit all computers on our network to get list of all local admin accounts. I would like to export the list to CSV file. Here is my current script.<\/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.PartComponent | % {$_.substring($_.lastindexof(\"Domain=\") + 7).replace(\"`\",Name=`\"\",\"\\\")}\n\n foreach($computer in (Get-ADComputer -Filter * -SearchBase 'OU=XXXX,DC=XXX,DC=XXX' | select name).name){\nWrite-Verbose \"Checking $computer\" -Verbose\nget-localadmins -computerName $computer \n}\n<\/code><\/pre>\nIt works fine as long as the computers are turned on. What would I need to add to Export it to CSV file? I have seen some examples where they just pipe it using Export-CSV. Would that be it? If so where would i need to add it? I am still new and learning. I tried to research it before posting.<\/p>\n
Thanks,<\/p>","upvoteCount":6,"datePublished":"2022-03-16T13:59:29.000Z","url":"https://community.spiceworks.com/t/script-to-audit-local-admin-accounts-export-to-csv-file/827881/1","author":{"@type":"Person","name":"jeffbearden2425","url":"https://community.spiceworks.com/u/jeffbearden2425"}},{"@type":"Answer","text":"
I would use a custom PS object to build a formatting structure for your data and then export it with Export-Csv.<\/p>\n