You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell中如何移除匹配另一数组值的数组元素?

问题解决方法

你的问题出在两个关键地方:

  • $script:UnwantedUsers是逗号分隔的单个字符串,不是PowerShell可识别的数组,-notin运算符无法和单个字符串完成正确的列表匹配
  • Where-Object语法错误,缺少包裹判断条件的大括号{}

修正步骤:

  1. 先把字符串格式的排除名单转换成PowerShell数组:
# 按", "分割字符串,生成真正的用户名称数组
$script:UnwantedUsers = "ABM User, WCSNCservice, Afaria Service Account, APC, etc" -split ', '
  1. 使用正确的Where-Object语法过滤用户列表:
$FilteredUsers = $AllU365Users | Where-Object { $_.DisplayName -notin $script:UnwantedUsers }

补充说明:

  • 转换成数组后,-notin才能逐个校验用户的DisplayName是否属于排除名单
  • Where-Object的判断条件必须放在大括号内,这是PowerShell的强制语法要求,之前的写法会导致条件解析失败,最终返回空结果

内容的提问来源于stack exchange,提问作者user1675228

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 11:42:37