Powershell - Do While Loop position

Hi,

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})
			  do{
					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
								
								Try
									{
										"[INFO]`t The following user will be created: $($sam)" | Out-File $log -append
										New-ADUser $sam `
										-GivenName $GivenName `
									}
								Catch
									{
									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									
											}
										else
											{
												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"
														Exit
													}
								
										}
				}
			While($Asking -eq "Y")		
		}
	Else
		{
			Write-Host "Bladiebla"
		} 

Open in new window

LVL 2
LoyallAsked:
Who is Participating?
 
LoyallAuthor Commented:
Fiddled around with functions and managed to solve the problem myself.
0
 
RobSampsonCommented:
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?

Rob.
0
 
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.
0
 
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.