none
Loop part of powershell script X times RRS feed

  • Question

  • Hi all,

    I'm currently working on some smaller school scripting projects for upcoming exams. However I can't seem to manage to run a certain part of a script X amount of times by a variable.

    So far I have the code shown below:

    (comment blocks are in Dutch but you can ignore those, it's for students to understand what it has to do)
    
    #Dit script is te gebruiken om een X aantal OUs toe te voegen aan je domein.
    #Het script vraagt hoeveel OUs je aan wilt maken, en de te gebruiken naam/descriptie voor het OU.
    #Ik ben van plan deze later nog uit te breiden om zo meer opties toe te staan voor de installaties etc.
    #De reden voor deze scripts is puur voor het gemak tijdens examens om het zo gemakkelijk voor alle opdrachten te kunnen gebruiken.
    
    
    #Dit deel van het script is voor het verzamelen van informatie die in de commands gestopt wordt.
    
    $repeatX = Read-Host -Prompt 'How many OUs should be made?'
    
    
    #Dit deel van het script is om de taken uit te voeren met de informatie die eerder verkregen is.
    
    Write-Host "Starting to make the OU..."
    
    Do {
    
      $OUnaam = Read-Host -Prompt 'Insert desired OU name'
    
      $OUdescription = Read-Host -Prompt 'Insert desired OU description'
    
      New-ADOrganizationalUnit "$OUnaam" -ProtectedFromAccidentalDeletion $False -Description "$OUdescription"
    
      Write-Host "Adding OU..."
    
      Write-Host "Amount of OUs left to be made: $($repeatX - 1)"
    
    } Until ($repeatX -eq 0)
    
    #Dit deel van het script is om aan te geven als er foutmeldingen zijn geweest tijdens de installatie.
    Start-Sleep 1
    Write-Host "
    Was there an error in the script during installation? (Default is Nee)" -
      $foutmelding = Read-Host " ( j / n) "
        Switch ($foutmelding)
        {
          j {
            Write-Host "Foutmelding gevonden...
            Pauzeren voor 120 seconden om de foutmelding te lezen..."
            Start-Sleep 120
          }
          n {
            Write-Host "Geen foutmelding gevonden...
            Script beeindigen..."
            Start-Sleep 5
          }
          Default {
            Write-Host "Geen foutmelding gevonden...
            Script beeindigen..."
            Start-Sleep 5
          }
        }
    
    
    Write-Host "Einde van script: 'Nieuwe-OU-aanmaken-universeel' ..."

    As you can see, at the start of the script it'll ask the amount of OUs that have to be made using the $repeatX variable. However the Do/Until part doesn't work for me, I've spent several hours trying to fix it so far however not been able to make it fully work, it just keeps on repeating forever.

    Hope any of you guys can help me solve this :)


    • Edited by RSDavyy Tuesday, October 9, 2018 11:11 AM
    Tuesday, October 9, 2018 11:09 AM

Answers

  • I would do it with a for loop:

    $repeatX = Read-Host -Prompt 'How many OUs should be made?'
    
    Write-Host "Starting to make the OU..."
    
    for($i = 1; $i -le $repeatX; $i++) {
    
      $OUnaam = Read-Host -Prompt 'Insert desired OU name'
      $OUdescription = Read-Host -Prompt 'Insert desired OU description'
      New-ADOrganizationalUnit "$OUnaam" -ProtectedFromAccidentalDeletion $False -Description "$OUdescription"
    
      Write-Host "Adding OU..."
      Write-Host "Amount of OUs left to be made: $($repeatX - $i)"
    }

    • Marked as answer by RSDavyy Tuesday, October 9, 2018 2:52 PM
    Tuesday, October 9, 2018 12:42 PM
  • The problem is that you never assign a new value to your variable repeatX

    you substract 1 from your variable in your script, but you never update your variable, so it just stays the same.

    You should change the last few lines of your loop to this

    $repeatX = $repeatX - 1
    Write-Host "Amount of OUs left to be made: $repeatX"

    • Proposed as answer by Roel E Tuesday, October 9, 2018 12:50 PM
    • Marked as answer by RSDavyy Tuesday, October 9, 2018 2:52 PM
    Tuesday, October 9, 2018 12:50 PM

All replies

  • Oh also, you can ignore the bottom part of the script as that is also in dutch :) Sorry for that!
    Tuesday, October 9, 2018 11:10 AM
  • I would do it with a for loop:

    $repeatX = Read-Host -Prompt 'How many OUs should be made?'
    
    Write-Host "Starting to make the OU..."
    
    for($i = 1; $i -le $repeatX; $i++) {
    
      $OUnaam = Read-Host -Prompt 'Insert desired OU name'
      $OUdescription = Read-Host -Prompt 'Insert desired OU description'
      New-ADOrganizationalUnit "$OUnaam" -ProtectedFromAccidentalDeletion $False -Description "$OUdescription"
    
      Write-Host "Adding OU..."
      Write-Host "Amount of OUs left to be made: $($repeatX - $i)"
    }

    • Marked as answer by RSDavyy Tuesday, October 9, 2018 2:52 PM
    Tuesday, October 9, 2018 12:42 PM
  • The problem is that you never assign a new value to your variable repeatX

    you substract 1 from your variable in your script, but you never update your variable, so it just stays the same.

    You should change the last few lines of your loop to this

    $repeatX = $repeatX - 1
    Write-Host "Amount of OUs left to be made: $repeatX"

    • Proposed as answer by Roel E Tuesday, October 9, 2018 12:50 PM
    • Marked as answer by RSDavyy Tuesday, October 9, 2018 2:52 PM
    Tuesday, October 9, 2018 12:50 PM
  • Thank you, simple and working solution! :)
    Tuesday, October 9, 2018 2:52 PM
  • Thanks a lot! tried this solution and working like a charm, definitely something I look forward to using as example in the future :)
    Tuesday, October 9, 2018 2:53 PM