¿Cómo agregar SAMAccount a los resultados?

¿Cómo agregar SAMAccount a los resultados?

Atascado en cómo agregar SAMAccounts (nombres de usuario de AD) a los resultados, ¿alguien puede ayudarme?

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

Respuesta1

Publicaste esto enintercambio de pilay aquí estaba mi respuesta para usted también y para aquellos que no desean saltar al enlace.

Puede obtener SamAccountName mediante el cmdlet Get-Mailbox.

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

Simplemente no se transmite por el proceso como se indica aquí... Ejemplo:

(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

...
#>

Así que inténtalo de esta manera...

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

Actualización para el OP

En cuanto a tu comentario...

Hola, esto produce un error: Invoke-Command: No se puede vincular el parámetro 'Filtro' al objetivo. Configuración de excepción "Filtro": "Sintaxis de filtro no válida. Para obtener una descripción de la sintaxis del parámetro de filtro, consulte la ayuda del comando. "*" en la posición 1".

… como se indica en mi comentario antes de esta actualización.

El ejemplo es todo PowerShell normal y sin formato, y debería funcionar de forma local o remota (siempre que tenga PSRemoting configurado correctamente y sea administrador local en la caja remota y esté ejecutando esto como ese administrador)

Si ejecutas esto...

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

o esto...

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

O esto...

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

... por sí solo en su entorno durante una sesión de PSRemoting, ¿qué sucede?

Si estás haciendo tu sesión de PSRemoting, así...

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

Import-PSSession $ExpSession

Entonces no necesita Invoke-Command en absoluto, ya que los cmdlets ya están enviados por proxy a su estación de trabajo. Simplemente ejecute el código tal como está.

Ejemplo: sesión implícita de PSRemoting, aprovechando -Prefix

Este prefijo -no es absolutamente necesario, es solo un hábito que he estandarizado para todas mis sesiones remotas implícitas. Se recomienda el prefijo -si está utilizando, por ejemplo, los cmdlets de Exchange Online y los cmdlets locales de Exchange en el mismo cuadro para evitar confusiones y errores):

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

información relacionada