$servers= get-content 'C:\Localdata\5admin.csv'
$output = 'c:\localdata\alladmin.csv'
$results = @()
foreach($server in $servers)
{
$admins = @()
$group =[ADSI]"WinNT://$server/Administrators"
$members = @($group.psbase.Invoke("Members"))
$members | foreach {
$obj = new-object psobject -Property @{
Server = $Server
Admin = $_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)
}
$admins += $obj
}
$results += $admins
}
$results| Export-csv $Output -NoTypeInformation
$servers= Get-Content -Path 'C:\Localdata\5admin.csv'
$output = 'C:\localdata\alladmin.csv'
$exclude = @(
"L RG*"
"LocalAdmin"
"Domain Admins"
)
$pattern = '^(' + (($exclude | ForEach-Object {"($(($_.Replace('*', '.*').Replace('?', '.'))))"}) -join '|') + ')$'
$servers | ForEach-Object {
$server = $_
Write-Host "Processing $($server)"
$group = [ADSI]"WinNT://$($server)/Administrators"
$group.psbase.Invoke('Members') | ForEach-Object {
New-Object PSObject -Property ([ordered]@{
Server = $server
Admin = $_.GetType().InvokeMember('Name', 'GetProperty', $null, $_, $null)
})
}
} |
Where-Object {$_.Admin -notmatch $pattern} |
Export-Csv -Path $Output -NoTypeInformation
$servers= Get-Content -Path 'C:\Localdata\5admin.csv'
$output = 'C:\localdata\alladmin.csv'
$exclude = @(
"L RG*"
"LocalAdmin"
"Domain Admins"
)
$pattern = '^(' + (($exclude | ForEach-Object {"($(($_.Replace('*', '.*').Replace('?', '.'))))"}) -join '|') + ')$'
$servers | ForEach-Object {
$server = $_
Write-Host "Processing $($server)"
$group = [ADSI]"WinNT://$($server)/Administrators"
$group.psbase.Invoke('Members') | ForEach-Object {
$adsPath = $_.GetType().InvokeMember('AdsPath', 'GetProperty', $null, $_, $null).Split('/', [StringSplitOptions]::RemoveEmptyEntries)
New-Object PSObject -Property ([ordered]@{
Server = $server
Admin = $adsPath[-1]
AdminRealm = $adsPath[-2]
})
}
} |
Where-Object {($_.AdminRealm -eq $_.Server) -and ($_.Admin -notmatch $pattern)} |
Export-Csv -Path $Output -NoTypeInformation
Open in new window
If you have multiple conditions, it is probably better to use -notmatch with a regular expression.