问题
i want this by import csv file and export to the file without the "ou=service account".......
回答1:
Your code is very close!
The only thing is that the -notcontains
operator is meant for finding elements in an array and should not be used on single strings.
The same goes for -contains
, -in
, -notin
. All of these work on arrays.
What should work is using the -notmatch
regex operator.
Because this is regex, you should be aware that the string to search can hold characters that have special meaning in regex, like the dot. In your case, there is no such character, but if there were, you need to perform a [regex]::Escape()
on the string to search for.
Import-CSV "C:\temp\temp.csv" |
Where-Object { $_.DistinguishedName -notmatch ",OU=Service Account," } |
Export-Csv "C:\temp\$date-$domain.csv" -NoTypeInformation
Or by using the -notlike
operator with wildcards *
on either side of the string:
Import-CSV "C:\temp\temp.csv" |
Where-Object { $_.DistinguishedName -notlike "*OU=Service Account,*" } |
Export-Csv "C:\temp\$date-$domain.csv" -NoTypeInformation
The confusion with -contains
or -notcontains
is easy to make, because a string object has a .Contains()
method and yes, that does work on the string you provide.
Therefore, this should also work for you:
Import-CSV "C:\temp\temp.csv" |
Where-Object { !($_.DistinguishedName.Contains(",OU=Service Account,")) } |
Export-Csv "C:\temp\$date-$domain.csv" -NoTypeInformation
Hope that helps
P.S. You do not show what is inside the $date
variable, but as long as this does not have invalid filename characters like \
or :
it should be fine.
来源:https://stackoverflow.com/questions/61416161/how-can-i-filter-ou-service-account-from-distinguishedname-in-csv-file