【问题标题】:Powershell if statement into foreach loop from csvPowershell if 语句从 csv 进入 foreach 循环
【发布时间】:2021-11-09 20:30:39
【问题描述】:

我创建了一个脚本来遍历 csv 文件并获取每列的值。基于其中一个值,必须运行 if 语句并返回结果。到目前为止,它只通过 if,而不是 else。你能帮我解决这个问题吗?这是我的代码。

$members = import-csv membership.csv
select-object "id","group","operation" |


Foreach ($row in $members) {
    If ($row.operation = "add")

    {write-host "This is IF statement"}

    Else 

    {write-host "This is ELSE statement"}
}

【问题讨论】:

标签: powershell csv if-statement foreach


【解决方案1】:
$members = import-csv membership.csv
select-object "id","group","operation" |


Foreach ($row in $members) {
If ($row.operation -eq "add")

{write-host "This is IF statement"}

Else 

{write-host "This is ELSE statement"}

}

【讨论】:

  • 您忘记删除| ;)
  • 非常感谢大家,我删除了管道并将 = 更改为 -eq,现在它可以工作了。
猜你喜欢
  • 2021-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多