Skip to main content
added 770 characters in body
Source Link

I am creating a script with basic variables to create users in AD via PS7 rather than remoting in to AD and manually creating. It runs through several questions and then my script will run at the end to create the AD user.

This is the very original starting block when I didn't know of while loop

 $NewPassword = (Read-Host -Prompt 'Please enter User New Password' -AsSecureString)
 $NewPasswordMatch = (Read-Host -Prompt 'Please re-enter User New Password' -AsSecureString)

 $newp1 = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($NewPassword))
 $newp2 = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($NewPasswordMatch))

 if ($newp1 -CEQ $newp2) {
 Write-Host "Passwords matched" -ForegroundColor Green
 } else {
 Write-Host "Passwords incorrect - please reset in AD once user is 
 created" -ForegroundColor Red
 }

I have everything pretty much in place for basics but I am struggling with the password loop function. I got it at one point to loop but it would not write host "Passwords do not match, please try again". I've played with it so much now I haven't been able to get it back to that so asking for help and hoping I have made a silly mistake that I have gone code blind to see.

  • I am asking for the technician to enter a users password twice to ensure there are no typos
  • I have the script working to check if PW1 matches PW2.
  • I can get it to check, and produce a screen prompt of 'OK' or 'Not OK'
  • I can not get it to advise 'Passwords don't match' and then loop back to the top to have the technician enter them again until it is correct. At the point of it being correct it will move on to the next bit of script

I am very new to PowerShell, self taught other than online webinars and books and I understand the premise of While, but not 100% understanding the catch. This script is prior to the AD account being created, the full script IS to create it once you get to the last question. Where after that it will run new-aduser etc etc.

while( $true){ 

$NewPassword = (Read-Host -Prompt 'Please enter User New Password' -AsSecureString)
$NewPasswordMatch = (Read-Host -Prompt 'Please re-enter User New Password' -AsSecureString)

$newp1 = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($NewPassword))
$newp2 = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($NewPasswordMatch))
try{
     if ($newp1 -CEQ $newp2){
     Write-Host "Password Accepted" -ForegroundColor Green
        }
    Break
    }
catch{
    Write-Host "Passwords do not match, please try again" -ForegroundColor Red
    }
}

I am creating a script with basic variables to create users in AD via PS7 rather than remoting in to AD and manually creating. It runs through several questions and then my script will run at the end to create the AD user.

I have everything pretty much in place for basics but I am struggling with the password loop function. I got it at one point to loop but it would not write host "Passwords do not match, please try again". I've played with it so much now I haven't been able to get it back to that so asking for help and hoping I have made a silly mistake that I have gone code blind to see.

  • I am asking for the technician to enter a users password twice to ensure there are no typos
  • I have the script working to check if PW1 matches PW2.
  • I can get it to check, and produce a screen prompt of 'OK' or 'Not OK'
  • I can not get it to advise 'Passwords don't match' and then loop back to the top to have the technician enter them again until it is correct. At the point of it being correct it will move on to the next bit of script

I am very new to PowerShell, self taught other than online webinars and books and I understand the premise of While, but not 100% understanding the catch. This script is prior to the AD account being created, the full script IS to create it once you get to the last question. Where after that it will run new-aduser etc etc.

while( $true){ 

$NewPassword = (Read-Host -Prompt 'Please enter User New Password' -AsSecureString)
$NewPasswordMatch = (Read-Host -Prompt 'Please re-enter User New Password' -AsSecureString)

$newp1 = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($NewPassword))
$newp2 = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($NewPasswordMatch))
try{
     if ($newp1 -CEQ $newp2){
     Write-Host "Password Accepted" -ForegroundColor Green
        }
    Break
    }
catch{
    Write-Host "Passwords do not match, please try again" -ForegroundColor Red
    }
}

I am creating a script with basic variables to create users in AD via PS7 rather than remoting in to AD and manually creating. It runs through several questions and then my script will run at the end to create the AD user.

This is the very original starting block when I didn't know of while loop

 $NewPassword = (Read-Host -Prompt 'Please enter User New Password' -AsSecureString)
 $NewPasswordMatch = (Read-Host -Prompt 'Please re-enter User New Password' -AsSecureString)

 $newp1 = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($NewPassword))
 $newp2 = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($NewPasswordMatch))

 if ($newp1 -CEQ $newp2) {
 Write-Host "Passwords matched" -ForegroundColor Green
 } else {
 Write-Host "Passwords incorrect - please reset in AD once user is 
 created" -ForegroundColor Red
 }

I have everything pretty much in place for basics but I am struggling with the password loop function. I got it at one point to loop but it would not write host "Passwords do not match, please try again". I've played with it so much now I haven't been able to get it back to that so asking for help and hoping I have made a silly mistake that I have gone code blind to see.

  • I am asking for the technician to enter a users password twice to ensure there are no typos
  • I have the script working to check if PW1 matches PW2.
  • I can get it to check, and produce a screen prompt of 'OK' or 'Not OK'
  • I can not get it to advise 'Passwords don't match' and then loop back to the top to have the technician enter them again until it is correct. At the point of it being correct it will move on to the next bit of script

I am very new to PowerShell, self taught other than online webinars and books and I understand the premise of While, but not 100% understanding the catch. This script is prior to the AD account being created, the full script IS to create it once you get to the last question. Where after that it will run new-aduser etc etc.

while( $true){ 

$NewPassword = (Read-Host -Prompt 'Please enter User New Password' -AsSecureString)
$NewPasswordMatch = (Read-Host -Prompt 'Please re-enter User New Password' -AsSecureString)

$newp1 = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($NewPassword))
$newp2 = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($NewPasswordMatch))
try{
     if ($newp1 -CEQ $newp2){
     Write-Host "Password Accepted" -ForegroundColor Green
        }
    Break
    }
catch{
    Write-Host "Passwords do not match, please try again" -ForegroundColor Red
    }
}
Source Link

While Function - Loop on failure to match input password for new AD user creation script

I am creating a script with basic variables to create users in AD via PS7 rather than remoting in to AD and manually creating. It runs through several questions and then my script will run at the end to create the AD user.

I have everything pretty much in place for basics but I am struggling with the password loop function. I got it at one point to loop but it would not write host "Passwords do not match, please try again". I've played with it so much now I haven't been able to get it back to that so asking for help and hoping I have made a silly mistake that I have gone code blind to see.

  • I am asking for the technician to enter a users password twice to ensure there are no typos
  • I have the script working to check if PW1 matches PW2.
  • I can get it to check, and produce a screen prompt of 'OK' or 'Not OK'
  • I can not get it to advise 'Passwords don't match' and then loop back to the top to have the technician enter them again until it is correct. At the point of it being correct it will move on to the next bit of script

I am very new to PowerShell, self taught other than online webinars and books and I understand the premise of While, but not 100% understanding the catch. This script is prior to the AD account being created, the full script IS to create it once you get to the last question. Where after that it will run new-aduser etc etc.

while( $true){ 

$NewPassword = (Read-Host -Prompt 'Please enter User New Password' -AsSecureString)
$NewPasswordMatch = (Read-Host -Prompt 'Please re-enter User New Password' -AsSecureString)

$newp1 = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($NewPassword))
$newp2 = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($NewPasswordMatch))
try{
     if ($newp1 -CEQ $newp2){
     Write-Host "Password Accepted" -ForegroundColor Green
        }
    Break
    }
catch{
    Write-Host "Passwords do not match, please try again" -ForegroundColor Red
    }
}