如何將 SAMAccount 加入結果?

如何將 SAMAccount 加入結果?

不知道如何將 SAMAccounts(AD 使用者名稱)加入結果中,有人可以幫忙嗎?

Get-Mailbox -ResultSize Unlimited | Get-MailboxPermission | where {$_.user.tostring() -ne "NT AUTHORITY\SELF" -and $_.IsInherited -eq $false} | Select Identity,User,Username,@{Name='Access Rights';Expression= {[string]::join(', ', $_.AccessRights)}} | Export-Csv -NoTypeInformation C:\temp\mailboxpermissions1.csv

答案1

您將此交叉發佈到堆疊交換這也是我在這裡為您以及那些傾向於不跳到該連結的人的回答。

您可以使用 Get-Mailbox cmdlet 取得 SamAccountName。

((Get-Mailbox -Filter '*')[0] | Get-Member).Name

# Results
<#
PS C:\Scripts> ((Get-Mailbox -Filter '*')[0] | Get-Member).Name
...
RoomMailboxAccountEnabled
RulesQuota
SamAccountName
SCLDeleteEnabled
...
#>

Get-Mailbox -Filter '*' | ForEach {$PSItem.SamAccountName}

# Results
<#
 Get-Mailbox -Filter '*' | ForEach {$PSItem.SamAccountName}
Administrator
...
#>

它只是沒有按照此處所述的方式沿著管道傳遞...示例:

(Get-Mailbox -Filter '*' -ResultSize Unlimited).SamAccountName | 
ForEach{Get-MailboxPermission -Identity $PSItem} | 
Where-Object {
    $PSItem -ne 'NT AUTHORITY\SELF' -and 
    $PSItem.IsInherited -eq $false
} | Select-Object -Property '*'

# Results
<#
AccessRights    : {FullAccess, ReadPermission}
Deny            : False
InheritanceType : All
User            : NT AUTHORITY\SELF
Identity        : contoso.com/Users/Administrator
IsInherited     : False
IsValid         : True
ObjectState     : Unchanged

...
#>

那麼,試試這個方法...

(Get-Mailbox -Filter '*' -ResultSize Unlimited).SamAccountName | 
ForEach{Get-MailboxPermission -Identity $PSItem} | 
Where-Object {
    $PSItem -ne 'NT AUTHORITY\SELF' -and 
    $PSItem.IsInherited -eq $false
} | Select-Object -Property Identity,User,
@{Name = 'SamAccountName';Expression = {(Get-ADUser -Identity $($PSitem.Identity -split '/')[-1]).SamAccountName}},
@{Name = 'Access Rights';Expression = {[string]::join(', ', $PSItem.AccessRights)}}

# Results
<#
Identity                              User                SamAccountName   Access Rights                       
--------                              ----                --------------   -------------                       
contoso.com/Users/Administrator     NT AUTHORITY\SELF   Administrator    FullAccess, ReadPermission          
... 
#>

OP 更新

至於你評論...

您好,出現錯誤:Invoke-Command:無法將參數「Filter」綁定到目標。異常設定“過濾器”:“過濾器語法無效。有關過濾器參數語法的說明,請參閱命令說明。“*”位於位置 1。”

…正如我在此更新之前的評論所述。

此範例都是原始的普通 PowerShell,並且應該在本機或遠端工作(只要您正確設定了 PSRemoting 並且您是遠端機器上的本機管理員並且您以管理員身分執行此範例)

如果你運行這個...

Invoke-Command -ComputerName ex01 -ScriptBlock {Get-Mailbox -Filter '*' -ResultSize Unlimited}

或這個...

Invoke-Command -ComputerName ex01 -ScriptBlock {(Get-Mailbox -Filter '*' -ResultSize Unlimited).SamAccountName}

或這個...

Invoke-Command -ComputerName ex01 -ScriptBlock {Get-Mailbox -Filter '*' -ResultSize Unlimited | Select-Object -Property SamAccountName}

……在您的環境中透過 PSRemoting 會話本身,會發生什麼?

如果您正在進行 PSRemoting 會話,就像這樣...

$ExpSession = New-PSSession -ConfigurationName 'Microsoft.Exchange' -ConnectionUri ("http://$Ex01Fqdn/PowerShell") -Authentication Kerberos -Credential $Creds

Import-PSSession $ExpSession

那麼您根本不需要 Invoke-Command,因為 cmdlet 已經代理到您的工作站。只需按原樣運行程式碼即可。

範例 - 隱式 PSRemoting 會話,利用 -Prefix

這個前綴並不是絕對必要的,這只是我在所有隱式遠端會話中標準化使用的習慣。如果您在同一台機器上同時使用 Exchange Online cmdlet 和 Exchange on-prem cmdlet,建議使用 - 前綴以避免混淆和錯誤。

($ExpSession = New-PSSession -ConfigurationName 'Microsoft.Exchange' -ConnectionUri ("http://$Ex01Fqdn/PowerShell") -Authentication Default)

<#
 Id Name            ComputerName    State         ConfigurationName     Availability
 -- ----            ------------    -----         -----------------     ------------
  8 Session8        ex01.contoso... Opened        Microsoft.Exchange       Available
#>


Import-PSSession $ExpSession -Prefix 'EXP'

<#
WARNING: The names of some imported commands from the module 'tmp_zucxz5zd.0ee' include unapproved verbs that might make them less discoverable. To find the commands wi
th unapproved verbs, run the Import-Module command again with the Verbose parameter. For a list of approved verbs, type Get-Verb.

ModuleType Version    Name                                ExportedCommands                                                                                             
---------- -------    ----                                ----------------                                                                                             
Script     1.0        tmp_zucxz5zd.0ee                    {Add-EXPADPermission, Add-EXPAvai...
#>

(Get-ExpMailbox -Filter '*' -ResultSize Unlimited).SamAccountName | 
ForEach{Get-ExpMailboxPermission -Identity $PSItem} | 
Where-Object {
    $PSItem -ne 'NT AUTHORITY\SELF' -and 
    $PSItem.IsInherited -eq $false
} | Select-Object -Property Identity,User,
@{Name = 'SamAccountName';Expression = {(Get-ADUser -Identity $($PSitem.Identity -split '/')[-1]).SamAccountName}},
@{Name = 'Access Rights';Expression = {[string]::join(', ', $PSItem.AccessRights)}}

# The results would be the same as my original response.

相關內容