我们有一个与 Office 365 中的 Exchange Online 同步的本地 AD 域。我正在尝试使用 PowerShell 获取所需的信息,但一直收到错误,并且似乎无法使命令一起正确工作(管道)。
我想要做的是:
1) Use GET-ADOBJECT to query my local AD for any user/person object in a specific
OU and return the UserPrincipalName
2) Pipe the returned UserPrincipalName to the GET-MAILBOX cmdlet (Exchange Online
in Office 365) and retrieve any mailbox where RecipientTypeDetails equals
“SharedMailbox”. The output will them return UserPrincipalName
3) Pipe the returned UserPrincipalName to GET-ADOBJECT and query my local AD again
for any object where msExchRemoteRecipientType isn’t equal to 100
组合的 PowerShell 命令如下所示:
Get-ADObject -Filter {(objectClass -eq "User") -And (objectCategory -eq "Person")} -SearchBase “OU=Test,DC=domain,DC=com” -Properties UserPrincipalName | Select-Object UserPrincipalName | ForEach-Object{Get-Mailbox -Identity $_.UserPrincipalName} | Where-Object {$_.RecipientTypeDetails -eq "SharedMailbox"} | Select-Object UserPrincipalName | ForEach-Object {Get-ADObject -Filter {(UserPrincipalName -eq $_.UserPrincipalName)} -SearchBase "OU=Test,DC=domain,DC=com" –Properties msExchRemoteRecipientType} | Where-Object {$_.msExchRemoteRecipientType -ne 100}
我尝试了不同的变化但最终出现以下错误:
Get-ADObject : Property: 'UserPrincipalName' not found in object of type:
'System.Management.Automation.PSCustomObject'.
At line:1 char:356
+ Get-ADObject -Filter {(objectClass -eq "User") -And (objectCategory -eq "Person")} -SearchBase “OU=Test,DC=domain,DC=com” -Properties UserPrincipalName | Select-Object UserPrincipalName | ForEach-Object{Get-Mailbox -Identity $_.UserPrincipalName} | Where-Object {$_.RecipientTypeDetails -eq "SharedMailbox"} | Select-Object UserPrincipalName | ForEach-Object {Get-ADObject -Filter {(UserPrincipalName -eq $_.UserPrincipalName)} -Search Base "OU=Test,DC=domain,DC=com" –Properties msExchRemoteRecipientType} | Where-Object {$_.msExchRemoteRecipientType -ne 100}
+ CategoryInfo : InvalidArgument: (:) [Get-ADObject], ArgumentException
+ FullyQualifiedErrorId : Property: 'UserPrincipalName' not found in object of type:
'System.Management.Automation.PSCustomObject'.,Microsoft.ActiveDirectory.Management.Commands.GetADObject
如果我删除最后一部分(最后的 Get-ADObject 部分):
ForEach-Object {Get-ADObject -Filter {(UserPrincipalName -eq $_.UserPrincipalName)} -Search Base "OU=Test,DC=domain,DC=com" –Properties msExchRemoteRecipientType} | Where-Object {$_.msExchRemoteRecipientType -ne 100}
我得到了所需的 UserPrincipalName 输出。
我的问题是将 UserPrincipalName 输出从 Get-Mailbox 传输到 Get-ADObject。我使用“ForEach-Object”的原因是,它解决了从 Get-ADObject 传输到 Get-Mailbox 时的问题,但将 Get-Mailbox 传输到 Get-ADObject 时却不起作用。
我尝试过很多不同的方法来解决这个问题,但都没有成功。有没有办法将 Get-Mailbox 传输到 Get-ADObject?或者有其他技巧可以用来获得所需的输出?我可以让第一个 Get-ADObject 查询返回的“属性”通过 Get-Mailbox cmdlet,然后将这些“属性”作为输出提供给第二个 Get-ADObject 查询吗?
谢谢
编辑:
我在运行以下示例时遇到同样的问题:
Get-Mailbox "[email protected]" | Select-Object UserPrincipalName | Get-ADObject {(UserPrincipalName -eq $_.UserPrincipalName)} -SearchBase "DC=domain,DC=com"
是否可以从获取邮箱到获取对象?
答案1
问题是您不能在过滤器中使用 $_ 变量。以下方法可以正常工作:
get-mailbox | foreach{$upn = $_.UserPrincipalName; Get-ADObject -Filter {UserPrincipalName -like $upn}}