0

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.

0

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.