vendredi 17 janvier 2020

Powershell foreach loop with if statements only evaluates first statement

So I have a snippet of my code below. Basically it loops from 1 to a user input. Each loop it generates a random number from 1 to 4 (inclusive), then based on that number it prints a specific word document to the default printer.

If I just run the code without all the if / elseif statements, only printing the value of $ran, then it works perfectly.

However once I put the if / elseif statements in, it only prints file number 1.docx.

Any ideas why the value of $ran seems to remain 1 when im using the if / elseif statements?

Code -

1..$count | foreach { $ran = random -Minimum 1 -Maximum 5
    if ($ran = 1) {
    Start-Process -FilePath "file1" -Verb Print
    Wait-Process "WINWORD"
    }
Elseif ($ran = 2){

    Start-Process -FilePath "file2" -Verb Print
    Wait-Process "WINWORD"
    }
elseif ($ran = 3){

    Start-Process -FilePath "file3" -Verb Print
    Wait-Process "WINWORD"
    }
elseif ($ran = 4){

    Start-Process -FilePath "file4" -Verb Print
    Wait-Process "WINWORD"
    }
else {

    Write-Output "Something went wrong!"
    pause
    }
    }



Aucun commentaire:

Enregistrer un commentaire