0

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}
2
  • 1
    The declaration of $lastname=" is missing a ". I think the rest of your code is fine. Commented Dec 13, 2014 at 21:50
  • You also have a spurious closing bracket. Commented Dec 14, 2014 at 11:45

3 Answers 3

1

Your loop runs 5 times.

The problem is that once the variables are populated in the first loop, they fail the While condition in the all the subsequent loops. It runs one time, doing a read-host and setting the variables. Then it runs 4 more times, but doesn't find anything to do.

Your updated code is closer. You save the data, but didn't clear the variables for the next loop.

$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)

         $UserID,$firstname,$lastname = ""
    }
Sign up to request clarification or add additional context in comments.

2 Comments

How do I fix so I can enter more than on name?
Come up with a different test for the while loops, or save the data in $UserID, $firstname, and $last name somewhere and then clear those variaibles so they're ready for the next loop.
1

Couple of issues here, the while loops need clear variables like mjolinor said. Once that is done, I suggest saving that data in an array, so the data of each user is not lost by the next iteration:

$users = @()
$personCount = 1

while ($personCount -le 5){
    $userID=""
    $firstname=""  
    $lastname=""
    $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"
    }
    $users += New-Object -TypeName PSCustomObject -Property @{
        userID    = $userID;
        firstName = $firstname;
        lastName  = $lastname
    }
}

Once the array $users has all the data, you can output all the values at once with $users or specific users with $users[0],$users[1], etc.

Comments

1

It's clearly not possible that the code you posted would create the output you posted. Even if I discount the two syntax errors you'd still be getting 4 key conflict exceptions, because you're trying to add the same key to the same hashtable 5 times (a key in a hashtable must be unique).

If you want to create 5 user objects in a hashtable and make sure that the user enters a value for all 3 properties you could do something like this if you want a duplicate ID to throw an error:

$personHash = @{}

1..5 | % {
  do {
    $userID    = Read-Host "Enter ID"
    $firstname = Read-Host "Enter First Name"
    $lastname  = Read-Host "Enter Last Name"
  } until ( $userID -and $firstname -and $lastname )

  $user = New-Object -Type PSCustomObject -Property @{
            'ID'        = $userID
            'FirstName' = $firstname
            'LastName'  = $lastname
          }

  $personHash.Add($userID, $user)
}

or like this if you want a duplicate ID to update the current values:

$personHash = @{}

1..5 | % {
  do {
    $userID    = Read-Host "Enter ID"
    $firstname = Read-Host "Enter First Name"
    $lastname  = Read-Host "Enter Last Name"
  } until ( $userID -and $firstname -and $lastname )

  $user = New-Object -Type PSCustomObject -Property @{
            'ID'        = $userID
            'FirstName' = $firstname
            'LastName'  = $lastname
          }

  $personHash[$userID] = $user
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.