Powershell:获取邮箱权限 - 仅限具有给定名称+姓氏

时间:2015-10-16 07:36:34

标签: powershell csv exchange-server

我需要创建一个包含邮箱名称(mb标识),用户,访问权限和拒绝的CSV文件。但对于那些有姓名和姓氏而不仅仅是姓氏的ADU,我只想这样做。

我想到了类似的东西:

$File_Path = $args[0]
$File_Path = ((Get-Item -Path ".\" -Verbose).FullName) + "\" + $File_Path

$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding($False)
[System.IO.File]::WriteAllLines($File_Path, "Mailbox;RightHolder;Rights;Deny", $Utf8NoBomEncoding)

$Mailboxes = Get-Mailbox -ResultSize Unlimited

Foreach ($Mailbox in $Mailboxes) {
    $mbPermissions = get-mailboxpermission $Mailbox
    $str_DN = $Mailbox.DistinguishedName
    $ad_obj = [ADSI]"GC://$str_DN"

    Foreach ($mbPermission in $mbPermissions) {
        If ($mbPermission.IsInherited -eq $False -and $mbPermission.User -notlike "NT-AUTORITÄT\SELBST") {
            [System.IO.File]::AppendAllText($File_Path, "$($mbPermission.Identity);$($mbPermission.User);$($mbPermission.AccessRights);$($mbPermission.Deny)`r", $Utf8NoBomEncoding)
        }
    }
    $ADPermissions = get-ADPermission $Mailbox.Identity

    Foreach ($ADPermission in $ADPermissions) {
         If ($ADPermission.ExtendedRights -like "Send-As" -and $ADPermission.User -notlike "NT-AUTORITÄT\SELBST" -and $ADPermission.Deny -eq $false)    {
             [System.IO.File]::AppendAllText($File_Path, "$($ADPermission.Identity);$($ADPermission.User);$($ADPermission.ExtendedRights);$($ADPermission.Deny)`r", $Utf8NoBomEncoding)
         }
    }
}

这适用于使用我需要的所有凭据创建csv文件,但它不会排除没有给定名称的用户的邮箱。我有点卡在这里。

感谢您的帮助!

编辑: 解决了!只需将这些行代替$ Mailboxes部分:

$Mailboxes = get-mailbox -ResultSize Unlimited | select -ExpandProperty samaccountname
$Filter = foreach ($Obj in $Mailboxes) { get-aduser $Obj | select -property givenname,samaccountname }
$NoGivenName = $Filter | where { $_.givenname -ne $null } | select -ExpandProperty samaccountname

$ BoxesFiltered = foreach($ NoGivenName中的$){get-mailbox $ Box}

1 个答案:

答案 0 :(得分:0)

您可以尝试以下操作,可能需要一段时间,因为必须使用Get-ADUser过滤掉以重新生成列表。

Import-Module ActiveDirectory
$File_Path = $args[0]
$File_Path = ((Get-Item -Path ".\" -Verbose).FullName) + "\" + $File_Path

$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding($False)
[System.IO.File]::WriteAllLines($File_Path, "Mailbox;RightHolder;Rights;Deny", $Utf8NoBomEncoding)

$Mailboxes = Get-Mailbox -ResultSize Unlimited

$Mailboxes = Foreach ($Mailbox in $Mailbox){ (get-aduser $mailbox | where { ([string]::IsNullOrEmpty($_.givenname) -eq $false) }).samaccountname }

Foreach ($Mailbox in $Mailboxes)
{
    $mbPermissions = get-mailboxpermission $Mailbox
    $str_DN = $Mailbox.DistinguishedName
    $ad_obj = [ADSI]"GC://$str_DN"

    Foreach ($mbPermission in $mbPermissions)
    {
        If ($mbPermission.IsInherited -eq $False -and $mbPermission.User -notlike "NT-AUTORITÄT\SELBST")
        {
            [System.IO.File]::AppendAllText($File_Path, "$($mbPermission.Identity);$($mbPermission.User);$($mbPermission.AccessRights);$($mbPermission.Deny)`r", $Utf8NoBomEncoding)
        }
    }
    $ADPermissions = get-ADPermission $Mailbox.Identity

    Foreach ($ADPermission in $ADPermissions)
    {
        If ($ADPermission.ExtendedRights -like "Send-As" -and $ADPermission.User -notlike "NT-AUTORITÄT\SELBST" -and $ADPermission.Deny -eq $false)
        {
            [System.IO.File]::AppendAllText($File_Path, "$($ADPermission.Identity);$($ADPermission.User);$($ADPermission.ExtendedRights);$($ADPermission.Deny)`r", $Utf8NoBomEncoding)
        }
    }
}