PowerShell – 从 proxyAddresses 数组中删除特定的 SMTP 地址

PowerShell – 从 proxyAddresses 数组中删除特定的 SMTP 地址

我一直在尝试弄清楚如何使用 PowerShell 修改 AD 中某些用户的代理地址。我的基本目标是使用 Get-ADObject 查询 AD 中的一组特定用户,并从用户的代理地址(如果存在)中删除特定域的所有 SMTP 地址。

$ADobjects = @(Get-ADObject -Filter 'objectClass -eq "User"' -Properties mailNickname, ProxyAddresses) | Where-Object {$_.ProxyAddresses -Match "@BADdomain.com"}

ProxyAddresses 可能看起来像:

SMTP:[email protected], smtp:[email protected], smtp:[email protected], X500:/o=info/ou=test/cn=john.doe, x400:/o=info/ou=test/cn=john.doe, smtp:[email protected]

这没有特定的顺序,X500 地址可能是第一个,也可能是最后一个,SMTP、X400 等。大写 SMTP 表示主 SMTP 地址,小写表示辅助地址。有可能[电子邮件保护]可能是主 SMTP 地址(拆分这些地址时我会忽略大小写 - 如果需要,我可以在稍后的过程中测试大小写以更改主 SMTP)。而且(我可能错了)但似乎 proxyAddresses 不是一个普通数组(也许它是一个多维数组)?但是当我尝试在“,”上拆分数组时,我不断收到错误:

ForEach ($Object in $ADobjects)
{
$TempInfo = $Object.ProxyAddresses.Split(",")
write-host $TempInfo
}

它产生的错误是:

Method invocation failed because [Microsoft.ActiveDirectory.Management.ADPropertyValueCollection] doesn't contain a method named 'Split'.

我可以将 proxyAddresses “拆分” 为一个单独的数组,以便我可以处理每个地址,如果域名是 @BADdomain.com,则可以将其删除吗?然后我可以将其从数组中删除。或者是否可以使用“.Replace”并将以 SMTP 开头的所有内容替换为 BADdomain.com(如果在下一个 SMTP 之前找到 BADdomain.com)?我不知道,我尝试过很多方法,但都没有成功。

非常感谢!!

答案1

Split 用于分割字符串;用户对象的 ProxyAddresses 属性不是字符串,因此不支持“Split”。

尝试这样的事情(完全未经测试:)):

ForEach ($userObject in $ADobjects)
{
  ForEach ($proxyAddress in $userObject.ProxyAddresses)
  {
    write-host $proxyAddress
  }
}

相关内容