开发者

Powershell - Split strings of text file into chunks, then strip out unwanted info

Working with Active Directory export script. Currently, the script easily exports user objects from a given group into a .txt file as fully qualified names (ie. instead of reporting as "username", you get the ugly "CN=username,OU=foo,OU=bar,DC=foo,DC=bar").

My Powers开发者_运维知识库hell experience is novice at best. What I've gotten so far:

  • load contents of text file
  • split contents of text file at the ","
  • write these contents into a second text file.

  • resulting text file looks like this:

    "CN=username  
    OU=foo  
    OU=bar  
    DC=foo  
    DC=bar"
    

My goal:

  • Save any resulting line of text that begins with "CN= (yes I'd like to keep that " at the beginning)
  • Delete everything else
  • Strip "CN= from the beginning of the resulting lines
  • Note: usernames vary in length

What I have so far:

$a = (Get-Content c:\test.txt | foreach-Object {$_.split(",,") })  
Out-File c:\results.txt -inputObject $a

I'm completely lost as to how to search text files for a pattern with a wildcard (ie. seach for any lines that contain "CN=* and keep those lines and those lines only) and then stick the results into a .txt.

Thank you so much for any and all guidance you might provide.


Select-String would allow that. It's kinda grep for PowerShell.

However, if I understand you correctly you can do that a bit easier with

$a = (Get-Content c:\test.txt | foreach-Object {$_.split(",,") }) -match '^CN='

The comparison operators can be applied to a list of objects and return only the matching items.


Perhaps also take a look at the Quest AD cmdlets and use that as your export script. That way you can choose exactly which attributes you want without having to filter the output of a text file e.g.

Get-QADUser -Searchroot foo.bar/MyOu | Select Name, DisplayName, email 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜