【问题标题】:Filtering inactive AD user from cmdlet result?从 cmdlet 结果中过滤非活动 AD 用户?
【发布时间】:2020-08-27 14:15:49
【问题描述】:

我需要验证下面的 cmdlet 结果:

Get-AntiPhishPolicy | Select-Object -ExpandProperty targeteduserstoprotect

返回如下:

System Admin; System.Admin@domain.com Display Name;
Display.Name@domain.net

如何与Get-ADUser结合,获取非活跃用户列表?

显示名称

Get-AntiPhishPolicy | Select-Object -ExpandProperty targeteduserstoprotect | Select @{ Name = 'User List'; Expression = { ($_ -split ';')[0] }}

代理地址

Get-AntiPhishPolicy | Select-Object -ExpandProperty targeteduserstoprotect | Select @{ Name = 'User List'; Expression = { ($_ -split ';')[1] }}

Inactive 表示 AD 用户帐户已禁用或找不到匹配的帐户。

Get-AntiPhishPolicy | Select-Object -ExpandProperty targeteduserstoprotect | Where-Object {((Get-ADUser -Filter "PrimarySMTPAddress -neq '$($_.targeteduserstoprotect -split ';' [1] )'" ) -or ("Enabled -eq $True")}

来源:https://docs.microsoft.com/en-us/powershell/module/exchange/get-antiphishpolicy?view=exchange-ps

Because the above is not working for me with the below error:
At line:2 char:178
+ ... ySMTPAddress -neq '$($_.targeteduserstoprotect -split ';' [1] )'" ) - ...
+                                                               ~~~
Unexpected token '[1]' in expression or statement.
At line:2 char:179
+ ... arySMTPAddress -neq '$($_.targeteduserstoprotect -split ';' [1] )'" ) ...
+                                                                  ~
Missing type name after '['.
At line:2 char:213
+ ... argeteduserstoprotect -split ';' [1] )'" ) -or ("Enabled -eq $True")}
+                                                                         ~
Missing closing ')' in expression.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedToken

【问题讨论】:

  • 乍一看,我会说一个好的代码编辑器,比如Visual Studio Code,会在这里帮助你很多。在你运行任何东西之前就存在语法错误。在你的Where-Object中,$_.targeteduserstoprotect已经不存在了,应该把Select-Object -ExpandProperty后面的$_替换成$_。拆分部分应该更像这样($_ -split ';')[1]。我不知道-or ("Enabled -eq $True") 的目的是什么。
  • 好的,它正在部分工作。如何根据电子邮件地址 ($_.ProxyAddresses) 将管道结果匹配到 Get-ADUser?我需要得到那些失败的。

标签: powershell


【解决方案1】:

根据我上面的 cmets,我会简化这一点,不要试图将它强加到一个班轮中。

$TargetedUser = ((Get-AntiPhishPolicy | Select-Object -ExpandProperty targeteduserstoprotect) -split ";")[1]
Get-ADUser -Filter "PrimarySMTPAddress -ne $TargetedUser -and Enabled -eq 'True'" 

注意关于 AD cmdlet,我相信 Enabled 字段实际上不是布尔值,应该是一个字符串,因此在上面将其设置为“True”。如果我的假设是错误的,或者我记错了,请将其设置为$true

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-09
    • 1970-01-01
    • 1970-01-01
    • 2018-10-03
    相关资源
    最近更新 更多