Powershell:将获取adpermission对象转换为获取aduser对象

rryofs0p  于 2022-12-18  发布在  Shell
关注(0)|答案(2)|浏览(130)

我正在尝试运行对邮箱具有“代理发送”权限的用户的报告。我正在使用以下代码,该代码工作正常:

$sa_user = Get-Mailbox -id <mailbox> | Get-ADPermission | where { ($_.ExtendedRights -like "*Send-As*") -and ($_.IsInherited -eq $false) }

但${sa_user.User}数据可能“毫无帮助”,我被要求提供更有用的数据(具体来说就是:部门、规范名称和显示名称)。
我想将$sa_user adpermission对象绑定到aduser对象,因为它具有报告所需的所有属性。

$ad_user = Get-ADUser -id $sa_user.identity

但是$sa_user.identity是用于邮箱的,而不是用户的。所以我尝试:

$ad_user = Get-ADUser -id $sa_user.User


但我只得到Get-ADUser cmdlet Identify找不到$sa_user. User给定的值的错误。
所以我的问题是我如何基于adpermission对象的可用属性得到aduser对象?...或者任何其他方法、快捷方式或中介来实现?

svgewumm

svgewumm1#

$ad_user = Get-ADUser -filter * -Properties * | where-object {$_.CanonicalName -eq $sa_user.Identity}
d4so4syb

d4so4syb2#

试试看

$ad_user = Get-ADUser -Filter * -Properties msDS-PrincipalName | where-object {$_.'msDS-PrincipalName' -eq $sa_user.User}

$ad_user = Get-ADUser -Filter * -Properties sAMAccountName | where-object {$_.sAMAccountName -eq ($sa_user.User -replace ".+\\")}

.

编辑2022年12月1日

在我注意到这种过滤方式可能会非常慢之后,我尝试了一些不同的方法。下面是循环到send-as授权用户/对象的完整代码。你只需要定义一个searchbase:

function Convert-CN2DN([string]$cn) {
    $obj = $cn.Split('/')
    if(-not $obj[0].Contains('.')) {
        $cn
    } else {
        [string]$DN = 'CN=' + $obj[$obj.count - 1]
        for ($i = $obj.count - 2; $i -ge 1; $i--) { $DN += ',OU=' + $obj[$i] }
        $obj[0].split('.') | ForEach-Object { $DN += ',DC=' + $_ }
        $DN
    }
}

$user_NTAUTHORITY = "NT AUTHORITY" # This is language specific. For example in german: "NT-AUTORITÄT"
$Searchbase = "" # <--- define a searchbase

$MailBoxes = Get-Mailbox -OrganizationalUnit $Searchbase
foreach($Mailbox in $MailBoxes) { 
    $RecipientPermissions = $Mailbox | Get-RecipientPermission | ? { -not ($_.Trustee -match $user_NTAUTHORITY) -and ($_.IsInherited -eq $false)} 
    foreach($RecipientPermission in $RecipientPermissions) { 
        if($RecipientPermission.Trustee) {
        
            $MailboxUser = Get-ADUser -Identity $Mailbox.DistinguishedName -Properties description,msExchResourceDisplay | Select ObjectClass,msExchResourceDisplay,description,Name
                            
            $SendAsObject_DN = Convert-CN2DN $RecipientPermission.Trustee
            if($SendAsObject_DN -ne $RecipientPermission.Trustee) { # When converting was successful. (Just in case of deleted users.)
                $SendAsObject = Get-ADObject -Identity $SendAsObject_DN -properties mail | Select Name,ObjectClass,distinguishedName,mail
            } else { 
                $SendAsObject = ""
            }
            
            # Now you could work with
            # $MailboxUser
            # $SendAsObject    
        } 
    }
}

相关问题