I am using the following Powershell from http://iislogs.com/steveschofield/2009/01/09/list-local-administrators-on-a-machine-using-powershell-adsi/ to extract the local and domain admin accounts configured in PC and servers of an internal network.
Powershell script is below:
function LogToFile ([string]$strFileName, [string]$strComputer)
{
Add-Content $strFileName $strComputer
}
$strComputer = “server1.loc.mydomain.com”
$computer = [ADSI](“WinNT://” + $strComputer + “,computer”)
$Group = $computer.psbase.children.find(“Administrators”)
$members= $Group.psbase.invoke(“Members”) | %{$_.GetType().InvokeMember(“Name”, ‘GetProperty’, $null, $_, $null)}
ForEach($user in $members)
{
Write-Host $user
$a = $strComputer + “!” + $user.ToString()
LogToFile “C:\local-admins.txt” $a
}
When executed, it produces results in the txt file in following format:
server1.loc.mydomain.com!Administrator
server1.loc.mydomain.com!JohnDoe
server1.loc.mydomain.com!Support
server1.loc.mydomain.com!Domain Administrators
Can someone help me to change the output on txt file, so the results are shown in this format:
server1.loc.mydomain.com!Administrator!JohnDoe!Support!Domain Administrators
If reported in this way, then I can easily export to csv and work with it. It'd be super cool if this can be transformed to export on CSV in desired format instead of txt.
- How to make the code on line 6 so e.g. if I want to scan the whole subnet which has names: server1, server2, .... server38... I don't have to manually change that line for each machine. I tried server*, server[*] and it gives me errors.
I am a complete novice in PS, but trying to piece things together to address my needs and hope you may provide me with needed help.
Thanks.
$a = @($strComputer;$members) -join '!'- Mathias R. Jessenforeach(){}loop, just do it once - Mathias R. Jessen