0

What is the best way, to make a loop in powershell? including powershell Switch, So that every time you finish, you will return to the Switch to be rerun example:

$list_of_scripts=Read-Host "welcome to AD tool :) select: 1: new user 2: new OU Switch ($list_of_scripts){

1 { $name=Read-Host "enter the user name" New-ADUser -Name $name -Accountpassword (Read-Host -AsSecureString "AccountPassword") -Enabled $true -PasswordNeverExpires $true }

2 {$username=Read-Host "Enter OU name" New-ADOrganizationalUnit -name $username -Path "DC=slipknot,DC=main" -ProtectedFromAccidentalDeletion $False}}

what i tried:

$list_of_scripts = $scripts foreach ($script in $scripts) {

"welcome to AD tool :) select: 1: new user 2: new OU"

Switch ($list_of_scripts)

$script=1 {$name=Read-Host "enter the user name" New-ADUser -Name $name -Accountpassword (Read-Host -AsSecureString "AccountPassword") -Enabled $true -PasswordNeverExpires $true}

$script=2 {$username=Read-Host "Enter OU name" New-ADOrganizationalUnit -name $username -Path "DC=slipknot,DC=main"}}}

2
  • 2
    Please format code samples as code. Commented Nov 5, 2022 at 20:02
  • ***As posted, regardless of the use case, that code is not proper. *** Use the PS ISE or VSCode snippet tool CRTL+J and Shift+CRTL+Spacebar to see a snippet sample for you to refactor. Yet, you seem to be asking to build a menu that allows a user to supply data, complete a task and come back to the beginning menu. You can do this using Out-GridView as a GUI, with proper calls to the code nodes. It would be the same approach in any MenuApp and its code behind language. I've posted examples in the locations if you can to see them, you can reach me via the stackoverflow chat feature. Commented Nov 6, 2022 at 0:39

1 Answer 1

0

So you want to ask the user once at the start of the script run whether they want to add users or OU's, and then after that point loop round adding more of the same (eg re-using the result of the Read-Host line)?

If so then probably the easiest option is to simply use a Do While loop between the Read-Host line and the Switch statement. Eg,

$list_of_scripts=Read-Host "welcome to AD tool :) select: 1: new user 2: new OU 

Do {

Switch ($list_of_scripts){

1 { $name=Read-Host "enter the user name" New-ADUser -Name $name -Accountpassword (Read-Host -AsSecureString "AccountPassword") -Enabled $true -PasswordNeverExpires $true }

2 {$username=Read-Host "Enter OU name" New-ADOrganizationalUnit -name $username -Path "DC=slipknot,DC=main" -ProtectedFromAccidentalDeletion $False}

}

While {$name -ne "" -and $username -ne ""}

which will then cause it to loop round prompting to enter the username or OU name as required until you don't enter anything when prompted and just hit return.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.