Import-csv exclusions


hello,
i'm using construction getting users except ones in exclusion filter

$exclude = @(  'user1'  'user2'  )    $filter = ($exclude | foreach {'(name -ne ' + "'$_')"}) -join ' -and '    $users = get-aduser -filter $filter

its working nice...

i have csv

"enabled";"name";"alias";"primarysmtpaddress";"secondary1";"secondary3"
"true";"name1";"alias3";"alias1@domain.com";"alias1@contoso.com";
"true";"name2";"alias2";"alias2@domain.com";"alias2@contoso.com";
"true";"name3";"alias3";"alias3@domain.com";"alias3@contoso.com";
"true";"name4";"alias4";"alias4@domain.com";"alias4@contoso.com";

i thought, can same exclusion similar way ..

normal line without using filter:
import-csv .\testing.csv -delimiter ";" |?{($_.name -ne 'name1') -and ($_.name -ne 'name3')}

using filter:

$filter = ($exclude | foreach {'($_.name -ne ' + "'$_')"}) -join ' -and '

import-csv .\testing.csv -delimiter ";" |?{$filter}  -- realized pretty wouldn't work. how it?

1 working solution this

$exclude = @(  'name1'  'name3'  )  $csv = import-csv .\testing.csv -delimiter ';'   foreach ($objignore in $exclude) {  $csv = $csv |?{$_.name -ne $objignore}  }  $csv |select name,alias,primarysmtpaddress,secondary1,secondary2   
working meh .. has better way  ..

use -notin operator compare value against list this:
import-csv -path .\testing.csv -delimiter ';' | where-object { $_.name -notin $exclude }



cheers..uc



Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

Error: 0x80073701 when trying to add Print Services Role in Windows 2012 Standard

Disconnecting from a Windows Server 2012 R2 file sharing session on a Windows 7,8,10 machine

Windows 2016 RDS event 1306 Connection Broker Client failed to redirect the user... Error: NULL