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 コマンドレットを使用して 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' をターゲットにバインドできません。例外設定 "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 はまったく必要ありません。コードをそのまま実行するだけです。

例 - プレフィックスを活用した暗黙的な PSRemoting セッション

この -prefix は絶対に必要なわけではなく、すべての暗黙的なリモート セッションで標準的に使用する習慣です。混乱やエラーを避けるために、同じボックスで Exchange Online コマンドレットと Exchange オンプレミス コマンドレットの両方を使用している場合は、-prefix を使用することをお勧めします。):

($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.

関連情報