Powershell - Do While Loop position


I have a script that creates users after done some checks.
After one of the checks, the person who is executing the script is given a question:
Are you sure you want to ceate this user ? (Y/N)

If he/ she answers Yes, the script has to return to a previous line in the script and create the useraccount.
I'm trying to accomplish this task with a do/ while loop.
The problem is, I cannot point the loop to the point where the creation of the useraccount starts.

In (partial) the script below,  the do/while loop works, but it returns always, as expected, the same result.
I have pointed the location where i would like to start the script, after the users answer Y to the "Are you sure you want to ceate this user ? (Y/N)" question

Help is highly appreciated !

If ($SamOK) 
			 $names = $(try {Get-Aduser -filter * -Properties Givenname | Where {$_.Givenname -eq "$Givenname" -and $_.Surname -match "$Lastname"}} catch {$null})
					if ($names -eq $null)
					#########------> I want the loop to start here <------############
							Write-host "`nNo similar account found" -Foregroundcolor Green
							write-host "Please, continue processing the questions:" -Foregroundcolor Green
										"[INFO]`t The following user will be created: $($sam)" | Out-File $log -append
										New-ADUser $sam `
										-GivenName $GivenName `
									Write-Host "[ERROR]`t Oops, Something went wrong: $($_.Exception.Message)`r`n" -Foregroundcolor Red
					else ($names -ne $null)
							write-host "`nFound an (almost ?) identical user:"
							write-host "=================================" -ForegroundColor Magenta
								foreach ($name in $names)
										#Write-host "`n$name Exists"
										$Inlog = $name | Select -ExpandProperty  Samaccountname 
										$DisplayName = Get-Aduser $Inlog -properties Displayname | Select -ExpandProperty  Displayname
										$email = $(try {Get-Aduser $Inlog -properties EmailAddress | Select -ExpandProperty  emailaddress} catch {$null})
										if ($email -eq $null) 
												write-host "`n$DisplayName" -Foregroundcolor Yellow
												write-host "Accountname: $Inlog" 
												write-host "No emailaddress found" -Backgroundcolor Black									
												Write-host "`n$DisplayName" -Foregroundcolor Yellow
												Write-host "Accountname: $Inlog" 
												Write-host "Emailadsress $email"
							write-host "`n=================================" -ForegroundColor Magenta
								$Asking = Read-Host "Are you sure you want to create this user ? (Y/N)"
									if($Asking -eq "N")
											$Answer = Read-Host "`nDo you want to terminate the script  ? (Y/N)"
												If ($Answer -eq "Y")
														write-host "`nThe script will be terminated"
			While($Asking -eq "Y")		
			Write-Host "Bladiebla"

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi, I am struggling to understand what this code is doing.  It appears to check for the existence of a GivenName and Surname match.  Then if no account is found, it creates an account with an ID given by $sam, but we have no idea what that is....I assume it's a login ID that must be unique.

That's the bit that is confusing, in that you cannot continue to loop this block of code to create accounts....it will fail because it will be a duplicate samAccountName.

What is the purpose of the looping to create the accounts?

LoyallAuthor Commented:
Hi Rob,

Thanks for your reply !
The script first creates a unique SamAccountname. It takes the first two characters of the Givenname, the first two characters of the user's Surname and puts two digits after is, starting with 01.
When it finds the combination, for example abcd01, it raises the 01 with 1 point, so the new Samaccountname will be abcd02.

The problem at the company I'm working at for the moment is that there is some history in AD, and the current username policy has not been applied always. Some years ago users did have usernames with only three of four characters. There are some 2000 employees.

Now, at this time in history, a new system has been implemented in the company to which subcontractors need to logon to fill out the numbers of hours they worked, the amount of materials they used etc.
In earlier times, in some cases, shared accounts were used. Now, every user has to have it's own account.
For a lot of subcontractors new accounts must be created, but some already have an account that not always complies to the current username policy.
So even if SamAccountname is unique, the combination Givenname, Surname must be checked to make sure the user does not already have an account.
LoyallAuthor Commented:
Fiddled around with functions and managed to solve the problem myself.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LoyallAuthor Commented:
Slept well last night and did some "playing around"with functions into my script this morning and managed to solve the problem myself.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.