How can i Filter “OU=Service account” from DistinguishedName in CSV file

江枫思渺然 提交于 2021-02-11 15:56:42

问题


enter image description here

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!