Karthi Keyan
Karthi Keyan

Reputation: 47

How to call PowerShell switch case from CSV input

I have PowerShell script with a switch statement, I am trying to import CSV input to the script along with the required variables that will be used with in the switch case statement. One of the row will have the switch case no to be called.

enter image description here

Import-Csv $path
switch ($_.caseid) {
    1 {
        # task goes here
    }
    2 {
        # task goes here
    }
    3 {
        # task goes here
    }
}

If the number matches it should execute the respective block and delete the executed row from the CSV, if the field is empty or incorrect values it should ignore the row and go to other row.

Upvotes: 1

Views: 1133

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200463

Connect the Import-Csv and switch statements with a ForEach-Object:

Import-Csv $path | ForEach-Object {
    switch ($_.caseid) {
        1 {
            # task goes here
        }
        2 {
            # task goes here
        }
        3 {
            # task goes here
        }
    }
}

Upvotes: 2

Related Questions