How to move the script forward?

Hi all
I have this script:

while ($EmployeeNumber -ne [int])
{
    


try
{
[int]$EmployeeNumber = Read-Host -Prompt "Enter Employee Number"
}
catch [System.Management.Automation.PSInvalidCastException]
{
  Write-Host -ForegroundColor Red "You can only use numbers!!!"

}

}

$password = Read-Host -Prompt "Enter password"

If I run this script and will enter laters the script will ask me again to enter numbers
but even if enter numbers it’s just showing me a number and not moving to ask password
is anyone know why?

thanks

I’d try it this way:

while($EmployeeNumber.GetType().Name -ne 'Int32'){
    try {
        [int]$EmployeeNumber = Read-Host -Prompt "Enter Employee Number" -ErrorAction Stop
    }
    catch [System.Management.Automation.PSInvalidCastException] {
        Write-Host -ForegroundColor Red "You can only use numbers!!!"
    }
}

Regardless of that - wouldn’t it be a much better option to generate an Employee Number automatically from specific rules? If the user of this script enters a number already used it might cause conflicts. :wink:

The -ne operator checks for equality. Your statement doesn’t make sense (and will always return false) because what you’re trying to do is compare a value with a type.
What you mean to do is check if $EmployeeNumber is an instance of type [int]. To check if an object is an instance of a particular type, you need to use the -is -isnot operators:

while ($EmployeeNumber -isnot [int]) {
   try {
        [int]$EmployeeNumber = Read-Host -Prompt "Enter Employee Number"
    }
   catch [System.Management.Automation.PSInvalidCastException] {
       Write-Host -ForegroundColor Red "You can only use numbers!!!"
   }
}

$password = Read-Host -Prompt "Enter password"

That said, the above is a pretty horrible way of doing things. I recommend learning about functions and parameters:

function getEmployeeNumber {
    param (
        [Parameter(Mandatory = $true, HelpMessage="Enter the employee number")]
        [int]$employeeNumber
    )
    $employeeNumber
}

getEmployeeNumber
1 Like

Hi.
I tried to run this script

while ($EmployeeNumber -isnot [int]) {
   try {
        [int]$EmployeeNumber = Read-Host -Prompt "Enter Employee Number"
    }
   catch [System.Management.Automation.PSInvalidCastException] {
       Write-Host -ForegroundColor Red "You can only use numbers!!!"
   }
}

$password = Read-Host -Prompt "Enter password"

but now its not even asking me to enter employee number why?

Probably you run the code before and there is still a “$EmployeeNumber” from an earlier run. :wink:

2 Likes

no its skiping this stage

How do you know? :wink:

It runs for me just as expected if the variable is either not existing or of another type than [int].

Try to run this before:

Remove-Variable -Name 'EmployeeNumber'

BTW:
If you want to make sure that our loop runs at least once you should use a tail controlled loop instead of a head controlled one.

do {
    try {
        [int]$EmployeeNumber = Read-Host -Prompt "Enter Employee Number"
    }
    catch [System.Management.Automation.PSInvalidCastException] {
        Write-Host -ForegroundColor Red "You can only use numbers!!!"
    }
} while ($EmployeeNumber -isnot [int])
1 Like

its not working
i just want if i will type number the script will move to next step and will ask for password
and if i will type laters its will give the warnning message and will ask again numbers

how can i do it ?

I don’t know what to answer. The code does exactly this. You probably do samething different/wrong. Did you try to restart your PowerShell console?

yes i did





# Grab Variables from User
$ADPath = "OU=Users,OU=Alex,DC=alex,DC=local"

    

# Grab Variables from User
$firstname = Read-Host -Prompt "Enter First Name"

# Stop by empty first name
while (!($firstname -eq "")){

$lastname = Read-Host  -Prompt  "Enter Last Name"

[int]$EmployeeNumber = Read-Host -Prompt "Enter Employee Number"

while ($EmployeeNumber -isnot [int]) {
   try {
        [int]$EmployeeNumber = Read-Host -Prompt "Enter Employee Number"
    }
   catch [System.Management.Automation.PSInvalidCastException] {
       Write-Host -ForegroundColor Red "You can only use numbers!!!"
   }
}


$password = Read-Host -Prompt "Enter password"


# Set username
$i = 1
$username = $firstName + $lastName.Substring(0,$i)
$username = $username.ToLower()
   
while ((Get-ADUser -filter {SamAccountName -eq $username}).SamAccountName -eq $username)
{

        $username = $firstName + $lastName.Substring(0,$i++)
        $username = $username.ToLower()
}

$email = $username + "@alex.local" 
if (Get-ADUser -Filter "surname -eq '$lastname' -and givenname -eq '$firstname'")

{
  
# Create the AD User
New-ADUser `
-Name "$firstname $lastname ($EmployeeNumber)" `
-GivenName $firstname `
-Surname $lastname `
-EmployeeNumber $EmployeeNumber `
-Displayname "$FirstName $lastname" `
-UserPrincipalName $email `
-SamAccountName $username  `
-AccountPassword (ConvertTo-SecureString $password -AsPlainText -Force) `
-Path $ADPath `
-Enabled 1   
}
else
{
   # Create the AD User
New-ADUser `
-Name "$firstname $lastname" `
-GivenName $firstname `
-Surname $lastname `
-EmployeeNumber $EmployeeNumber `
-Displayname "$FirstName $lastname" `
-UserPrincipalName $email `
-SamAccountName $username  `
-AccountPassword (ConvertTo-SecureString $password -AsPlainText -Force) `
-Path $ADPath `
-Enabled 1   
}






Write-Host  -ForegroundColor Green "The user"$username" created successfully."

$firstname = Read-Host -Prompt "Enter First Name"

}

Write-Host -ForegroundColor Red "Done, Thank You"

this is the script

Remove this code line in front of the loop:

[int]$EmployeeNumber = Read-Host -Prompt "Enter Employee Number"

Once inside the loop is enough. You don’t need it twice. :wink:

$EmployeeNumber = Read-Host -Prompt “Enter Employee Number”

while ($EmployeeNumber -isnot [int]) {
try
{
[int]$EmployeeNumber = Read-Host -Prompt “Enter Employee Number”
}
catch [System.Management.Automation.PSInvalidCastException]
{
Write-Host -ForegroundColor Red “You can only use numbers!!!”

}

}

$password = Read-Host -Prompt “Enter password”

now its asking twise the employee number

Remove the first line of code you just posted.

but if remove it its not asking for the employee number at all

Please don’t get me wrong - this will be my last reply on this topic. :man_shrugging:t4:

It will prompt for the employee number from inside the loop when the variable does not exist yet or is of another type than [int]. I just tested it several times. It works.

Good luck. :+1:t4: :slightly_smiling_face:

thank you for your hlep
but for me its not working

sorry now its working