Array Count in ValidateScript

This topic contains 2 replies, has 3 voices, and was last updated by Profile photo of Curtis Smith Curtis Smith 1 year, 1 month ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
  • #28863
    Profile photo of Deadly-Bagel

    Perhaps this isn't the best way to do it but I'm just messing about right now. I'm writing a function that takes two string arrays as parameters, $username and $password.

    Obviously I want to make sure the number of users matches the number of passwords so I first looked at ValidateCount but that only lets me set strict numbers. As the number of elements may vary that won't work.

    So I instead moved to ValidateScript({ $_.Count -eq $UserName.Count }) just to see if it worked, but got an unexpected result. $_.Count returns 1 because the parameter is piped to the validation, so it only receives a single string at any one time.

    I mean I'm just messing about but it seems a bit odd they would just cut out any advanced means of testing the array length. Is there a reason for this or something I'm missing?

    Profile photo of Don Jones
    Don Jones

    I don't think they've cut out anything. Keep in mind that the Validate attributes run during parameter binding, before any of your code executes. That puts some practical limits on what they can do. Other validation would need to happen within your code, and you chuck an error if you don't like what you see.

    But as you note, in a pipeline situation you're only ever going to get one at a time from the pipeline. There's no way to "look back" along the pipeline and see how much data is there, because the previous command puts one object at a time into the pipeline.

    Really, want you probably want is a @{username='x';password='x'} hash table per user, on a single parameter. Or, an object having a UserName property and a Password property (like Import-CSV would produce), and you accept the entire object on a single parameter. That way you're getting that atomic data as a unit, not as two pieces.

    Profile photo of Curtis Smith
    Curtis Smith

    I have to say that I agree with Don, using a hash table does make the most since. You can get into trouble using two independent arrays for your username and password combinations. If you're not careful they can fall out of sync and position 0 in the password array might not contain the correct value for the username in position 0. Just checking to see if they have the same number of items does not validate the correlation.

    With that said, it sounds like this is what you are trying to do. Again I would advise not using this method and am only putting it here as a reference on how to do what I believe you are asking for.

    function ValidateScript {
        $username.count -eq $password.count
    $username = "user1","user2","user3"
    $password = "password1","password2","password3"
    ValidateScript -username $username -password $password
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.