Powershell : Cannot compare array.Length with int32

1.2k views Asked by At

I am creating a ps script that will handle different user commands. Since every function the user can call has a different number of parameters I just wanted to pass the rest of the usrInput[] array starting from index 1 as a parameter of the function for usrInput[0].

This is what I did:

$function
while($function -ne "backup" -or $function -ne "restore") { #Wait for user to make a valid input
    $usrInput = Read-Host "Enter -backup args[] or -restore args[] to backup or restore vms" -split " "
    $args
    for ($i = 1, $i -le $usrInput.Length, $i++) {
        $args[$i -1] = $usrInput[$i]
    }
    if ($usrInput[0] -eq "-backup") {
        backup($args)
}
    elseif ($usrInput[0] -eq "-restore") {
        restore($args)
    }
}

Now what I get is the following (English equivalent to the german shell output):

Enter -backup args[] or -restore args[] to back
"last comment in my code"
"1" couln't be compared to "96 62".
couldn't be converted to "System.Int32"
in line :5 letter:6
+ for ($i = 1, $i -le $inputLength, $i++) {
+      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation:
    + FullyQualifiedErrorId : ComparisonFailure

Enter -backup args[] or -restore args[] to back

Why is that?

I thought array.Lengths type was int!?

(Note: I also tried putting and [int] before it, but it didn't work)

Thank you very much in advance for your help.

1

There are 1 answers

0
Micky Balladelli On BEST ANSWER

The problem is the for statement you should write it like this:

for ($i = 1; $i -le $input.Length; $i++)

with ";" and not ",".

I didn't check the rest of the code as your question was about $input.length and int32.