Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Help searching list of servers for a list of accounts?

$
0
0

Hey everyone

I am trying to search a list of servers for a list of 'local accounts'. I do not have to find the domain accounts.

I have a script that I found that will search servers for local administrators. I am not sure at all how to search for only local accounts that are in a csv or txt file that I can have input into the script.  Can anyone please help me out?

#>

[CmdletBinding()]

Param(

 [Parameter( ValueFromPipeline=$true,

 ValueFromPipelineByPropertyName=$true

 )]

 [string[]]

 $ComputerName = $env:ComputerName,

 [Parameter()]

 [string]

 $GroupName = "Administrators",

 [Parameter()]

 [string]

 $OutputFolder = "f:\temp\"

)

Begin {

 $OutputFile = Join-Path $OutputFolder "results.CSV"

 Write-Verbose "Script will write the output to $OutputFile folder"

 Add-Content -Path $OutPutFile -Value "ComputerName, GroupName, QueryResult, ObjectType, DomainName, Name"

}

Process {

 ForEach($Computer in $ComputerName) {

 Write-host "Working on $Computer"

 If(!(Test-Connection -ComputerName $Computer -Count 1 -Quiet)) {

 Write-Verbose "$Computer is offline. Proceeding with next computer"

 Add-Content -Path $OutputFile -Value "$Computer,$GroupName,Offline"

 Continue

 } else {

 Write-Verbose "Working on $computer"

 try {

 $group = [ADSI]"WinNT://$Computer/$GroupName"

 $members = @($group.Invoke("Members"))

 Write-Verbose "Successfully queries the members of $computer"

 if(!$members) {

 Add-Content -Path $OutputFile -Value "$Computer,$GroupName,NoMembers"

 Write-Verbose "No members found in the group"

 continue

 }

 } 

 catch {

 Write-Verbose "Failed to query the members of $computer"

 Add-Content -Path $OutputFile -Value "$Computer,,QueryFailed"

 Continue

 }

 foreach($member in $members) {

 try {

 $MemberDisplayName = $member.GetType().Invokemember("Name","GetProperty",$null,$member,$null)

 $ObjectType = $member.GetType().Invokemember("Class","GetProperty",$null,$member,$null)

 $MemberPath $Member.GetType().Invokemember("ADSPath","GetProperty",$null,$member,$null)

 $MemberDomain = $null

 if($MemberPath -match "^Winnt\:\/\/(?<domainName>\S+)\/(?<CompName>\S+)\/") {

 if($ObjectType -eq "User") {

 $ObjectType = "LocalUser" 

 } elseif($ObjectTypee -eq "Group"){

 $ObjectType = "LocalGroup"

 }

 $MemberDomain = $matches["CompName"]

 } elseif($MemberPath -match "^WinNT\:\/\/(?<domainname>\S+)/") {

 if($ObjectType -eq "User") {

 $ObjectType = "DomainUser"

 } elseif($ObjectType -eq "Group"){

 $ObjectType = "DomainGroup"

 }

 $DomainOfMember = $matches["domainname"]

 } else {

 $ObjectType = ""

 $DomainOfMember = "Unknown"

 }

 Add-Content -Path $OutPutFile -Value "$Computer, $GroupName, SUCCESS, $ObjectType, $DomainOfMember, $MemberDisplayName"

 } catch {

 Write-Verbose "failed to query details of a member. Details $_"

 Add-Content -Path $OutputFile -Value "$Computer,,QueryFailed"

 }

 } 

 }

 }

}

End {}

 


Viewing all articles
Browse latest Browse all 6937

Trending Articles