当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 使用 PowerShell 在 Office 365 中查找电子邮件地址

作者:精品下载站 日期:2024-12-14 03:40:21 浏览:13 分类:玩电脑

使用 PowerShell 在 Office 365 中查找电子邮件地址


有时您需要查找使用特定电子邮件地址的用户或邮箱。特别是别名有时很难找到,您不想打开每个邮箱来检查它是否使用您需要的电子邮件地址。

您可以向不同位置的用户或组添加电子邮件地址,但它们都集中在您的 Exchange Online 服务器中。这就是我们要搜索的地方。

我们将为此使用新的 Exchange Online PowerShell 模块,如果您以前没有使用过它,那么您可以按照这个简短指南安装它并连接到 Exchange Online。新模块的优点是可以轻松连接到不同的租户,并且支持MFA。

使用 PowerShell 查找电子邮件地址

邮箱的所有电子邮件地址都列在 EXOMailbox cmdlet 的 EmailAddresses 属性中。我们可以通过对电子邮件地址应用过滤器来搜索任何电子邮件地址。过滤器的优点是我们还可以使用通配符。

[玩转系统] 使用 PowerShell 在 Office 365 中查找电子邮件地址

让我们从简单的开始,搜索特定的电子邮件地址。在示例中,我们将查找谁在使用 [email protected] 电子邮件地址。

# Make sure you are connected to Exchange Online
Connect-ExchangeOnline

# Search for the emailaddress
# -eq stands for equels, so the emailaddress must be exactly the same as the given string
Get-EXOMailbox -filter {EmailAddresses -eq "[email protected]"}

结果是一个包含我们正在查找的电子邮件地址的邮箱。

[玩转系统] 使用 PowerShell 在 Office 365 中查找电子邮件地址

使用通配符

通配符允许您搜索电子邮件地址的一部分。在 PowerShell 中,您可以使用 * 作为通配符标记,它可以放置在字符串中的任何位置。

假设我们想要查找电子邮件地址包含“支持”一词的所有邮箱。我们将在支持一词前后使用通配符。请注意,我们没有使用 -eq 运算符,而是使用 -like 运算符。

当您期望多个结果时,使用 ft cmdlet 在表中格式化输出是最有用的

# Get all mailboxes that contain the word support
Get-EXOMailbox -Filter {EmailAddresses -like "*support*"} | ft

[玩转系统] 使用 PowerShell 在 Office 365 中查找电子邮件地址

格式表输出 (ft) 的一个缺点是并非所有属性都适合您的屏幕。要解决此问题,您可以仅选择所需的字段,自动调整表格大小并换行。

Get-EXOMailbox -Filter {EmailAddresses -like "*support*"} | 
ft -Property DisplayName,RecipientType,Identity,EmailAddresses -AutoSize -Wrap

[玩转系统] 使用 PowerShell 在 Office 365 中查找电子邮件地址

在通讯组列表中搜索

上面的 PowerShell 命令会在所有邮箱中搜索邮件地址。但电子邮件地址也可以在通讯组列表或 Office 365 组中使用。

要搜索分发列表,我们需要使用 Exchange Online 模块中的 Get-DistributionGroup cmdlet。

通讯组列表中有两个电子邮件字段:PrimarySMTPAddressEmailAddresses。我们将使用后者,因为它还将包含分发列表中的所有别名。

Get-DistributionGroup -Filter {EmailAddresses -like "*support*"} | 
ft -Property DisplayName,RecipientType,Identity,EmailAddresses -AutoSize -Wrap

在 Office 365 组中搜索电子邮件地址

您可能想要搜索电子邮件地址的最后一个位置是 Office 365 组。这些组与通讯组具有相同的结构,因此我们将在 emailaddresses 字段中进行搜索。

Get-UnifiedGroup -Filter {EmailAddresses -like "*support*"} | 
ft -Property DisplayName,RecipientType,Identity,EmailAddresses -AutoSize -Wrap

Powershell 列出所有电子邮件地址和别名

利用这一原理,我们还可以获得包含所有电子邮件地址和别名的列表,并将其导出到 CSV 文件等。

获取包含电子邮件地址的列表有点困难,因为 EmailAddresses 字段还包含 SPO(用于 SharePoint Online 功能)和 SIP 地址。所以我们需要过滤掉这些。

通过使用表达式,我们可以过滤 EmailAddresses 字段并仅选择 SMTP 地址。运算符-clike 是区分大小写的like 运算符。

Get-EXOMailbox | 
Select-Object DisplayName,RecipientType,PrimarySmtpAddress, @{Name="Aliases";Expression={$_.EmailAddresses | Where-Object {$_ -clike "smtp:*"}}}

[玩转系统] 使用 PowerShell 在 Office 365 中查找电子邮件地址

正如您在结果中看到的,别名包含前缀 smtp:。在将结果导出到 csv 文件之前,我们也希望删除这些内容。 -join 运算符将多个别名连接在一起,并在它们之间使用逗号。

Get-EXOMailbox | 
Select-Object DisplayName,RecipientType,PrimarySmtpAddress, @{Name="Aliases";Expression={($_.EmailAddresses | Where-Object {$_ -clike "smtp:*"} | 
ForEach-Object {$_ -replace "smtp:",""}) -join "," }}

[玩转系统] 使用 PowerShell 在 Office 365 中查找电子邮件地址

如果你想将结果导出到excel,那么只需要在后面添加Export-Csv c:\path\filename.csv即可:

Get-EXOMailbox |
Select-Object DisplayName,RecipientType,PrimarySmtpAddress, @{Name="Aliases";Expression={($_.EmailAddresses | Where-Object {$_ -clike "smtp:*"} |
ForEach-Object {$_ -replace "smtp:",""}) -join "," }} | 
Export-Csv c:\temp\emailaddresses.csv

返回超过 1000 个结果

默认情况下,结果限制为 1000 条记录。如果您的邮箱超过 1000 个,请在 Get-ExoMailbox cmdlet 之后添加 -ResultSize unlimited:

Get-EXOMailbox -ResultSize unlimited

在所有 Office 365 位置搜索电子邮件地址

我创建了一个小脚本,它将在所有三个位置中搜索电子邮件地址的使用情况。您还可以搜索电子邮件地址的一部分。结果将被输出,或者如果您愿意,可以使用 Export-CSV cmdlet 将结果导出为 CSV。

您可以从我的 Github 存储库下载完整的脚本。

param(
  [Parameter(
    Mandatory = $true,
    HelpMessage = "Enter the Exchange Online or Global admin username"
  )]
  [string]$adminUPN,

  [Parameter(
    Mandatory = $true,
    HelpMessage = "Emailaddress or part of it to find"
  )]
  [string]$emailAddress
)

Function ConnectTo-EXO {
  <#
    .SYNOPSIS
        Connects to EXO when no connection exists. Checks for EXO v2 module
  #>
  
  process {
    # Check if EXO is installed and connect if no connection exists
    if ((Get-Module -ListAvailable -Name ExchangeOnlineManagement) -eq $null)
    {
      Write-Host "Exchange Online PowerShell v2 module is requied, do you want to install it?" -ForegroundColor Yellow
      
      $install = Read-Host Do you want to install module? [Y] Yes [N] No 
      if($install -match "[yY]") 
      { 
        Write-Host "Installing Exchange Online PowerShell v2 module" -ForegroundColor Cyan
        Install-Module ExchangeOnlineManagement -Repository PSGallery -AllowClobber -Force
      } 
      else
      {
	      Write-Error "Please install EXO v2 module."
      }
    }


    if ((Get-Module -ListAvailable -Name ExchangeOnlineManagement) -ne $null) 
    {
	    # Check if there is a active EXO sessions
	    $psSessions = Get-PSSession | Select-Object -Property State, Name
	    If (((@($psSessions) -like '@{State=Opened; Name=ExchangeOnlineInternalSession*').Count -gt 0) -ne $true) {
		    Connect-ExchangeOnline -UserPrincipalName $adminUPN
	    }
    }
    else{
      Write-Error "Please install EXO v2 module."
    }
  }
}

Function Search-Mailboxes {
  <#
  .SYNOPSIS
    Search for email address in the mailboxes
  #>
  process {
    Write-Host "Searching in mailboxes for $emailAddress" -ForegroundColor Cyan
    Get-EXOMailbox -filter "EmailAddresses -like '*$emailAddress*'"
  }
}

Function Search-Distributionlists {
  <#
  .SYNOPSIS
    Search for email address in the distributionlists
  #>
  process {
    Write-Host "Searching in distributionlists for $emailAddress" -ForegroundColor Cyan
    Get-DistributionGroup -Filter "EmailAddresses -like '*$emailAddress*'"
  }
}

Function Search-Groups {
  <#
  .SYNOPSIS
    Search for email address in the mailboxes
  #>
  process {
    Write-Host "Searching in groups for $emailAddress" -ForegroundColor Cyan
    Get-UnifiedGroup -Filter "EmailAddresses -like '*$emailAddress*'"
  }
}

Function Search-DynamicDistributionlists {
  <#
  .SYNOPSIS
    Search for email address in the distributionlists
  #>
  process {
    Write-Host "Searching in dynamic distributionlists for $emailAddress" -ForegroundColor Cyan
    Get-DynamicDistributionGroup -Filter "EmailAddresses -like '*$emailAddress*'"
  }
}

Function Find-EmailAddress{
  <#
    .SYNOPSIS
      Get all AD users
  #>
  process {
    $result = @()

    $result += Search-Mailboxes
    $result += Search-Distributionlists
    $result += Search-Groups
    $result += Search-DynamicDistributionlists

    $result | ForEach {
        [pscustomobject]@{
          "DisplayName" = $_.DisplayName
          "RecipientType" = $_.RecipientType
          "Identity" = $_.identity
          "EmailAddresses" = $_.EmailAddresses
        }
    }
  }
}

# Connect to Exchange Online
ConnectTo-EXO

Find-EmailAddress | Sort-Object DisplayName 

总结

我希望您觉得这篇文章有用。如果您有任何疑问,请在下面发表评论。您可能还喜欢以下相关文章:

  • 使用 PowerShell 连接到 Exchange Online
  • 使用 PowerShell 从 CSV 文件更新用户

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯