【问题标题】:Query OnPremise AD for Specific AD attributes to check Office 365 or not查询 On Premise AD 的特定 AD 属性以检查 Office 365 与否
【发布时间】:2019-07-12 08:22:58
【问题描述】:

我需要确定哪个电子邮件地址收件人当前已在 Office 365 中或仍在内部部署。

以下简单的 PowerShell 脚本仅用于导出 AD 组特定成员中的用户列表:

Get-ADGroup -Filter {Name -like '*IT*'} | Select-Object @{ n='Group'; e={ $_.Name } }, @{ n='Members'; e={ (Get-ADGroup $_.DistinguishedName -Properties Members | Select-Object Members).Members } } |
    Get-ADGroupMember -Recursive |
        Get-ADUser -Properties Mail | Select-Object Name, sAMAccountName, Mail |
            Export-CSV -path "C:\Group_members.csv" -NoTypeInformation

我只是想要另一列显示用户是否已经在 Office 365 中或仍然在本地。

还有一个专家发帖:

Get-MsolUser -UsageLocation US -All |
    Where-Object isLicensed -eq $true |
    Select-Object -Property DisplayName, UserPrincipalName, isLicensed,
    @{label = 'MailboxLocation'; expression = {
            switch ($_.MSExchRecipientTypeDetails) {
                1 {'OnPremise'; break}
                2147483648 {'Office365'; break}
                default {'Unknown'}
            }
        }
    }

但是不知道上面的脚本怎么组合?

我也试过下面的脚本来查询具有特定属性的 OnPremise AD,但仍然失败没有返回结果?

Get-ADUser-Filter *-Properties *|
Where-Object {($_.msExchRemoteRecipientType-eq4) -and
  ($_.msExchRecipientDisplayType -eq '-2147483642') -and
  ($_.msExchRecipientTypeDetails -eq '2147483648') -and
  ($_.proxyAddresses -contains "*.onmicrosoft.com*")
} 

【问题讨论】:

  • 这是一个混合交换环境还是用户严格来说是 Office 365 或 OnPremise?我问的原因是因为混合用户仍然会将 onmicrosoft.com 作为本地 AD 中的代理地址。
  • 另外,您是否正在使用 Azure AD 连接同步用户?如果是这样,那么我们应该期望 office 365 和本地 AD 中的 userprincipalname 值相同,对吗?

标签: powershell active-directory office365 exchange-server


【解决方案1】:

用户没有通过 Get-ADUser 调用 targetAddress 的属性

虽然如果你把它放在你的选择中,它会在结果中显示为空,因为你可以在选择中添加你想要的任何属性名称,无论它是否存在。

这也是语法错误

$_.msExchRecipientDisplayType = '-2147483642')

您也没有正确使用比较运算,但您使用了赋值运算符。表示 -eq 与 '='。

$_.msExchRecipientDisplayType -eq ...


# Find all cmdlets / functions with a target parameter
Get-Command -CommandType Function | 
Where-Object { $_.parameters.keys -match 'targetAddress'} | 
Format-Table -Autosize


# No results

Get-Command -CommandType Cmdlet | 
Where-Object { $_.parameters.keys -match 'targetAddress'} | 
Format-Table -Autosize


# No results



Get-Command -CommandType Function | 
Where-Object { $_.parameters.keys -match 'Address'} | 
Format-Table -Autosize


# No results

Get-Command -CommandType Cmdlet | 
Where-Object { $_.parameters.keys -match 'Address'} | 
Format-Table -Autosize

< #
CommandType     Name                        ModuleName
-----------     ---- ----------
Cmdlet          New-ADDCCloneConfigFile     ActiveDirectory
Cmdlet          New-ADOrganizationalUnit    ActiveDirectory
Cmdlet          New-ADUser                  ActiveDirectory
Cmdlet          Set-ADOrganizationalUnit    ActiveDirectory
Cmdlet          Set-ADUser                  ActiveDirectory
#>


Get-Command -CommandType Function | 
Where-Object { $_.parameters.keys -match 'EmailAddress|proxyAddress'} | 
Format-Table -Autosize


# No results

Get-Command -CommandType Cmdlet | 
Where-Object { $_.parameters.keys -match 'EmailAddress|proxyAddress'} | 
Format-Table -Autosize
< #
CommandType Name       ModuleName     
----------- ----       ----------     
Cmdlet      New-ADUser ActiveDirectory
Cmdlet      Set-ADUser ActiveDirectory
#>


Clear-Host
(Get-ADUser -Filter * -Properties *)[0] | 
Get-Member -Force | 
Select Name, MemberType | 
Format-Table -AutoSize


Name                                            MemberType
----                                            ----------
...
EmailAddress                                      Property
...
mail                                              Property
mailNickname                                      Property
...
msExchArchiveQuota                                Property
msExchArchiveWarnQuota                            Property
msExchCalendarLoggingQuota                        Property
msExchCoManagedObjectsBL                          Property
msExchDumpsterQuota                               Property
msExchDumpsterWarningQuota                        Property
msExchELCMailboxFlags                             Property
msExchHomeServerName                              Property
msExchMailboxGuid                                 Property
msExchMailboxSecurityDescriptor                   Property
msExchPoliciesIncluded                            Property
msExchRBACPolicyLink                              Property
msExchRecipientDisplayType                        Property
msExchRecipientTypeDetails                        Property
msExchTextMessagingState                          Property
msExchUMDtmfMap                                   Property
msExchUserAccountControl                          Property
msExchUserCulture                                 Property
msExchVersion                                     Property
msExchWhenMailboxCreated                          Property
...
proxyAddresses                                    Property
...
#>

正如其他网站所指出的,这也是这个......

($_.proxyAddresses -contains "*.onmicrosoft.com*")

...真的应该是这个...

($_.proxyAddresses -match "onmicrosoft.com")

...或者这个...

($_.proxyAddresses -like "*.onmicrosoft.com*")

OP 更新

After getting back to my test environment, the below works for the use case.

Get-ADUser -Filter * -Properties msExchRemoteRecipientType,proxyAddresses,msExchRecipientDisplayType,msExchRecipientTypeDetails | 
Where-Object {($_.msExchRemoteRecipientType -eq 4) -and
  ($_.proxyAddresses -match "onmicrosoft.com") -and
  ($_.msExchRecipientDisplayType -eq '-2147483642') -and
  ($_.msExchRecipientTypeDetails -eq '2147483648')
} 

另外,更正,不是你需要它来满足你的需求,因为 proxyAddresses 返回相同的东西,当你点击那个远程 O365 邮箱时,你会得到一个 targetAddress 属性,它只是不在本地邮箱上,因此,proxyAddresses 可能更谨慎地使用以保持一致性。

Get-ADUser -Filter * -Properties msExchRemoteRecipientType,proxyAddresses,targetAddress,msExchRecipientDisplayType,msExchRecipientTypeDetails | 
Where-Object {($_.msExchRemoteRecipientType -eq 4) -and
  ($_.proxyAddresses -match "onmicrosoft.com") -and
  ($_.targetAddress -match 'onmicrosoft.com') -and 
  ($_.msExchRecipientDisplayType -eq '-2147483642') -and
  ($_.msExchRecipientTypeDetails -eq '2147483648')
}

【讨论】:

  • 不用担心......很高兴它有帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-08
  • 1970-01-01
相关资源
最近更新 更多