【问题标题】:How to call PowerShell switch case from CSV input如何从 CSV 输入调用 PowerShell 开关案例
【发布时间】:2017-11-06 18:09:11
【问题描述】:

我有带有 switch 语句的 PowerShell 脚本,我正在尝试将 CSV 输入以及将在 switch case 语句中使用的所需变量导入脚本。其中一排将有开关盒没有被调用。

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

如果数字匹配,则应执行相应的块并从 CSV 中删除已执行的行,如果字段为空或值不正确,则应忽略该行并转到其他行。

【问题讨论】:

    标签: powershell csv


    【解决方案1】:

    ForEach-Object 连接Import-Csvswitch 语句:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多