Avatar of Can
Can
 asked on

Exclude results from powershell CSV Exports.

Hi All,

I got a script to export a list of local administrators on servers. I would like to exclude certain names and groups.  For example exclude names that starts with "L RG"
Any tips to apply this?

$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

Open in new window


Thanks in advance.
Powershell

Avatar of undefined
Last Comment
oBdA

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
oBdA

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Qlemo

There are a lot of ways to do that. E.g. you can replace line 17 with
$results += $admins | ? { $_.Admin -notlike 'L RG *' }

Open in new window

If you have multiple conditions, it is probably better to use -notmatch with a regular expression.
Can

ASKER
Thanks you both, that worked for me. Another thing: how can i add multiple objects to the -notlike parameter? I now got this:

Where-Object {$_.Admin -notlike "L RG*" -and $_.Admin -notlike "LocalAdmin" -and $_.Admin -notlike "Domain Admins"} |

Is there an easier way to add exclusions instead of adding -notlike everytime?
SOLUTION
Qlemo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
oBdA

This will allow you to use a list with * or ? as wildcards; it will turn that into a regex:
$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

Open in new window

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Can

ASKER
And the last: Is there an option to filter out domain accounts? I would like to retrieve only local accounts
oBdA

This adds a property "AdminRealm" with the member's domain (or the computer name in case of a local account)
$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