Answered Powershell for loop with a batch command?

  • יום שישי 17 פברואר 2012 08:54
     
     

    Hello!

    (im using Powershell here)

    I got some help yesterday to make a script that queries AD using ldifde and outputs to a file, then i clean it up with some more code and then i want to run a for loop against that file. but im not quite sure how to do it in powershell. I want to make use of the "batch command" dsmove since i cannot load the active directory module in powershell cause we dont have a 2008 R2 DC anywhere.

    So  i was thinking i was just gonna do a simple forloop straight up in powershell like this:

    For /f %%X IN (c:\outputfile.txt) DO dsmove %%X -newparent ou=OrgUnit,dc=domain,dc=com

    Turns out that doesnt work haha! So how do i do the equivalent of that in powershell?(and once again WITHOUT the active directory module)

    Is there a way to do it something like:
    (get-content c:\outputfile.tct)
    Foreach-object dsmove -newparent ou=orgunit,dc=domain,dc=com

    I realize the above is all bolony code but i think it could illustrate what i am trying to achieve.

כל התגובות

  • יום שישי 17 פברואר 2012 09:08
     
      קוד כלול

    This should do the trick:


    $x = get-content "c:\outputfile.txt"

    for ($j=0;$j -lt $x.count;$j++) { dsmove $X -newparent ou=OrgUnit,dc=domain,dc=com }



    • נערך על-ידי Jaap Brasser יום שישי 17 פברואר 2012 09:09 Code block
    •  
  • יום שישי 17 פברואר 2012 10:03
     
     

    I may have forgotten to point out that theres a diffrent computer CN on each new line in the contents of outputfile.txt

    It seems like this script just grabs the whole outputfile contents and tries to do dsmove against it, meaning it doesnt work.

  • יום שישי 17 פברואר 2012 10:08
     
     
    Can you copy paste a line from the inputfile?
  • יום שישי 17 פברואר 2012 10:15
     
      קוד כלול
    (get-content c:\outputfile.txt) | foreach-object { dsmove "$_" -newparent "ou=orgunit,dc=domain,dc=com" }

    this worked with the first line only. but not with the subsequent. Can you tell why?

    this is a sample of the outputfile.txt:

    --------------------------------------------

    CN=computername,OU=computerou,DC=domain,DC=com


    CN=computername2,OU=computerou,DC=domain,DC=com

    --------------------------------------------

    Unfortunately there's break lines between the CN's. is there a way to clean that up in Powershell?

    edit: found a way to edit out the empty lines.

    But i still dont understand why my forloop does not go beyond line 1

    • נערך על-ידי Plindgren יום שישי 17 פברואר 2012 10:28
    •  
  • יום שישי 17 פברואר 2012 10:39
     
      קוד כלול

    How does this work for you

    $x = get-content "c:\outputfile.txt" | where {$_ -ne ""}
    for ($j=0;$j -lt $x.count;$j++) {dsmove $X[$j] -newparent ou=OrgUnit,dc=domain,dc=com}

  • יום שישי 17 פברואר 2012 10:52
     
     תשובה קוד כלול
    (get-content c:\outputfile.txt) | foreach-object { dsmove "$_" -newparent "ou=orgunit,dc=domain,dc=com" }

    To improve upon your code you could try this:

    (get-content c:\outputfile.txt) | where {$_ -ne ""} | foreach-object { dsmove `"$_`" -newparent "ou=orgunit,dc=domain,dc=com" }
    • סומן כתשובה על-ידי Plindgren יום שישי 17 פברואר 2012 12:31
    •  
  • יום שישי 17 פברואר 2012 11:42
    מנחה דיון
     
      קוד כלול
    If that still doesn't filter out those empty lines, try
    (get-content) -match '\S' |

     -ne "" will still match if it's got a space or a tab on the line.

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

  • יום שישי 17 פברואר 2012 12:31
     
      קוד כלול

    Did it like this in the "query part" instead

    (Get-Content C:\output.txt) | 
    Foreach-Object {$_ -replace "dn: ", ""} | 
    Foreach-Object {$_ -replace "changetype: add", ""} | 
    where {$_ -notmatch "^$" } |
    Set-content c:\output.txt

    Thanks for the tips!

  • יום שישי 17 פברואר 2012 12:53
    מנחה דיון
     
     

    where {$_ -notmatch "^$" }

    is still going to let any line that's just whitespace (a space or a tab) through.


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