我有一个带有switch
语句的PowerShell脚本,我正在尝试将CSV输入导入脚本以及将在switch case语句中使用的必需变量。其中一行将不会调用开关盒。
Import-Csv $path
switch ($_.caseid) {
1 {
# task goes here
}
2 {
# task goes here
}
3 {
# task goes here
}
}
如果数字匹配,它应执行相应的块并从CSV中删除执行的行,如果该字段为空或值不正确,则应忽略该行并转到其他行。
答案 0 :(得分:1)
将Import-Csv
和switch
语句与ForEach-Object
:
Import-Csv $path | ForEach-Object {
switch ($_.caseid) {
1 {
# task goes here
}
2 {
# task goes here
}
3 {
# task goes here
}
}
}