none
Powershell wildcard problem

    Question

  • (get-content c:\file.txt) |
    Foreach-Object {$_ -replace "cn=", ""} | 
    Foreach-Object {$_ -replace ",*", ""}

    contents of file.txt:
    CN=COMPUTERNAME,OU=ITOU,DC=domain,DC=com
    CN=COMPUTERNAME2,OU=DIFFRENTOU,DC=domain,DC=com

    The above is the code i run as a powershell script. and the goal is to take away cn= and , and everything behind ,
    So naturally i just replaced "cn=" with "" and ",*" with "". This, does not work as intended.

    what i am left with is:
    COMPUTERNAMEOU=ITOUDC=domainDC=com
    COMPUTERNAME2OU=DIFFRENTOUDC=domainDC=com

    How could i achieve my goal of only being left with:
    COMPUTERNAME
    COMPUTERNAME2

    please provide an explaination along with your suggested changes! Would be much obliged!

    Friday, February 17, 2012 1:30 PM

Answers

All replies

  • I believe a dot is required for the replace command you are attempting. Could you try this:

    -replace ",ou.*",""

    Friday, February 17, 2012 1:36 PM
  • Just for to be complete I have added into your example

    (get-content c:\file.txt) |
    Foreach-Object {$_ -replace "cn=", ""} | 
    Foreach-Object {$_ -replace ",.*", ""} 

    Friday, February 17, 2012 1:38 PM
  • Or even easier:

    (get-content c:\scripts\file.txt) |
    Foreach-Object {($_ -split ',')[0] -replace 'CN='}


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)


    Friday, February 17, 2012 1:39 PM
  • Or:
    (get-content c:\scripts\file.txt) -replace '^cn=(.+?),(ou|dc)=.+$','$1'


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Friday, February 17, 2012 2:11 PM
  • Thanks alot you guys.

    Seems like all you do is wait for noobs to impress with your scripting knowledge!

    • Proposed as answer by Bigteddy Friday, February 17, 2012 2:29 PM
    • Unproposed as answer by Bigteddy Friday, February 17, 2012 2:29 PM
    Friday, February 17, 2012 2:27 PM
  • Na, it's just mj with his latest Regex magic trick.  It's just not poplular with the masses, Rob.

    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Friday, February 17, 2012 2:34 PM
  • We all  have our favorite ways of doing things, I think it's good for the 'noobs' to get exposed to different ways of solving a problem.  Then they have more ideas about what their options are for solving similar problems later.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Friday, February 17, 2012 2:35 PM
  •  It's just not poplular with the masses, Rob.

    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Give me time........

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Friday, February 17, 2012 4:45 PM