Powershell使用一个对象到另一个O365

时间:2018-10-01 18:25:53

标签: powershell

我正在尝试清理Poweshell scritp输出。到目前为止,我有

$both = my group I am pulling the members from
$both | foreach {get-MailboxPermission -identity $_.displayname} | Where-Object {$_.AccessRights -like 'FullAccess' -and $_.User -like'*@myemaildomain'} | Select-Object User, Identity | Sort-Object user `   

它输出以下内容:

User                             Identity
----                          --------
user@myemaildomain       User's Full Name(First and last name)          
user@myemaildomain       User's Full Name(First and last name)
user@myemaildomain       User's Full Name(First and last name)
user@myemaildomain       User's Full Name(First and last name)                         

我需要获取“用户”对象(user @ myemaildomain)并将其转换为名称(第一个和最后一个)

我知道我可以使用Get-mailbox user @ myemaildomain | select-object Name获取用户的全名。如何将其合并到脚本中?

1 个答案:

答案 0 :(得分:0)

我个人可以通过以下方式做到这一点:

Class O365User{
    $EmailAddress = [String]
    $Group = [String]
    $UserName = [String]
}

$both = my group I am pulling the members from
$UserData = $both | foreach {get-MailboxPermission -identity $_.displayname} | Where-Object {$_.AccessRights -like 'FullAccess' -and $_.User -like'*@myemaildomain'} | Select-Object User, Identity | Sort-Object user
$Mailboxes = Get-Mailbox

$O365Users = @()

Foreach($User in $UserData){
    $SingleUser = [O365User]::New()
    $SingleUser.EmailAddress = $User.User
    $SingleUser.Group = $User.Identity
    $SingleUser.UserName = ($Mailboxes | Where {$_.UserPrincipalName -eq $User.User}).Name
    $O365Users += $SingleUser
}

$O365Users

如果需要,您可以使用更多属性扩展类,并通过foreach循环分配值。