【问题标题】:Powershell Array output somes values with multiple colorsPowershell Array 输出一些具有多种颜色的值
【发布时间】:2021-05-02 18:26:31
【问题描述】:

我有两个包含电子邮件地址的数组:

  • $from
  • $newuser

我想将$from 的值输出给用户,但我想为变量$newuser 中也存在的值着色。

因此输出将以白色显示$from 中的所有电子邮件地址,但以绿色显示$newuser 中存在的电子邮件地址。

我不想附加两个变量的值,而是比较两个数组并以绿色显示两个数组中都存在的值。

我正在尝试在 Foreach 中使用 IF,但输出始终为白色。

foreach ($element in $from) {
    if($element -contains $newuser) {
        write-host $element `n -ForegroundColor Green
    } else {
        write-host $element `n -ForegroundColor White
    }
}

需要一些帮助来找出原因。

谢谢!

【问题讨论】:

  • 您的 if 子句中有 $elements 拼写错误。不应该只是 $element 吗?

标签: arrays powershell variables colors output


【解决方案1】:

除了道格提到的拼写错误之外,您的 if 子句被颠倒了。试试这样:

if ($newuser -contains $element) { ... }

【讨论】:

    猜你喜欢
    • 2013-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-14
    相关资源
    最近更新 更多