Why does my loop only happen once? I want to enter 5 users and add it to a hashtable, but the code runs once then stops. How Do I fix it?
$userID=""
$firstname=""
$lastname="
$personCount = 1
$personHash = @{}
while ($personCount -le 5){
$personCount++
While([string]::IsNullOrWhiteSpace($userID)){
$userID = Read-Host "Enter ID"
}
While([string]::IsNullOrWhiteSpace($firstname)){
$firstname = Read-Host "Enter First Name"
}
While([string]::IsNullOrWhiteSpace($lastname)){
$lastname = Read-Host "Enter Last Name"
}
$user = New-Object PSCustomObject -Property @{
ID = $userID
FirstName = $firstname
LastName = $lastname
}
$personHash.Add($user.ID,$user)
}
}
Output:
Enter ID: 1001
Enter First Name: Charles
Enter Last Name: Whitfield
Name Value
---- -----
1001 @{ID=1001; FirstName=Charles; LastName=Whitfield}
1001 @{ID=1001; FirstName=Charles; LastName=Whitfield}
1001 @{ID=1001; FirstName=Charles; LastName=Whitfield}
1001 @{ID=1001; FirstName=Charles; LastName=Whitfield}
$lastname="is missing a". I think the rest of your code is fine.