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

[玩转系统] Exchange Online:使用 PowerShell 查看邮箱大小和添加的移动设备

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

Exchange Online:使用 PowerShell 查看邮箱大小和添加的移动设备


很多客户问我是否可以统计Exchange Online中的邮箱大小。我说,用 PowerShell 当然可以。不久前,我创建了一个简短的脚本来获取尺寸作为 shell 的输出。没那么好用。最近几天,我花了一些时间创建包含邮箱大小和 ActiveSync 设备信息的 CSV 输出。使用以下脚本可以查看活动同步设备及其操作系统以及添加时间。

CSV 看起来像这样:

[玩转系统] Exchange Online:使用 PowerShell 查看邮箱大小和添加的移动设备

您可以将此 CSV 导入 Excel 并排序,或者可以执行任何您想要的操作。完整脚本位于底部

在开始分析邮箱统计数据之前,您需要一个 powershell 连接来进行在线交换。
如果您不知道,请参阅我的 Exchange Online Powershell 模块教程
有关 PowerShell 模块 Exchange Online 的其他信息
这里是给懒人的简短摘要 ?

#install module for all users on pc (once)
Install-Module ExchangeOnlineManagement # -Verbose -Scope AllUsers -Force
#import module 
Import-Module ExchangeOnlineManagement 
#connecto to exchange online (admin credentials needed)
Connect-ExchangeOnline 

[玩转系统] Exchange Online:使用 PowerShell 查看邮箱大小和添加的移动设备

成功连接到 Exchange Online 后,您应该能够在 Exchange Online 中执行 powershell cmdlet。

当然,您需要定义导出 csv 文件的路径,请参见第 3 行

[string]$timestamp = (Get-Date -Format yyyy-MM-dd-HH-mm) 
###### insert path to csv export
$csvpath = "C:\temp\exon-stats-$timestamp.csv"
######

四个输出原因必须有一个变量 mbstats 的数组,并且您需要获取变量 $mbs 中的所有邮箱

#see statistics of mailboxes
$mbs = Get-Mailbox -ResultSize Unlimited | Sort-Object name
#create an array for all statistics
$mbstats = @()

从 Exchange Online 获取所有邮箱后,我们使用 foreach 循环遍历它们以查找信息

foreach($mb in $mbs){
  $dn = (($mb).identity)
  $activesync = (Get-CASMailbox -Identity "$dn").hasactivesyncdevicepartnership
  if ($activesync -eq $true){
    #fetching active sync device information
    $activesyndevice = Get-MobileDevice -Mailbox "$dn" | Select-Object DeviceType,DeviceOS,DeviceUserAgent,WhenCreated
    $devicemodel = @()
    $devicemodel = ($activesyndevice) | Select-Object DeviceType -ExpandProperty DeviceType 
    $devicemodel = $devicemodel -join '||' 
    $deviceos = @()
    $deviceos = ($activesyndevice) | Select-Object DeviceOS -ExpandProperty DeviceOS 
    $deviceos = $deviceos -join '||'
    $deviceuagent = @()
    $deviceuagent =  ($activesyndevice) | Select-Object DeviceUserAgent -ExpandProperty DeviceUserAgent
    $deviceuagent = $deviceuagent -join '||'
    $devicecreated = @()
    $devicecreated = ($activesyndevice) | Select-Object WhenCreated -ExpandProperty WhenCreated
    $devicecreated = $devicecreated -join '||'
    #putting everything together (active sync devices and mailbox statistics)
    $mbstat = Get-Mailbox -Identity "$dn" | Get-MailboxStatistics | Select-Object DisplayName,MailboxGuid,`
    @{name="TotalItemSize (MB)"; expression={[math]::Round(($_.TotalItemSize.ToString().Split("(")[1].Split(" ")[0].Replace(",","")/1MB),2)}},`
    ItemCount,@{name="ActiveSync";expression={"True"}}, @{name="DeviceType";expression={"$devicemodel"}},@{name="DeviceOS";expression={"$deviceos"}},`
    @{name="DeviceAgent";expression={"$deviceuagent"}},@{name="DeviceFirstConnected";expression={"$devicecreated"}}`
    #adding to array 
    $mbstats += $mbstat
  }
  else{
    #get statistics for non active sync users
    $mbstat = Get-Mailbox -Identity "$dn" | Get-MailboxStatistics | Select-Object DisplayName,MailboxGuid, @{name="TotalItemSize (MB)"; expression={[math]::Round(($_.TotalItemSize.ToString().Split("(")[1].Split(" ")[0].Replace(",","")/1MB),2)}},ItemCount,@{name="ActiveSync";expression={"False"}} 
    #adding to array
    $mbstats += $mbstat
  }
} 

变量 $mbstats 填充了来自邮箱的信息,因此最后一件事是将这些信息导出到 CSV

#export information to csv with delimiter 
$mbstats | Export-Csv -Path "$csvpath" -Encoding UTF8 -Delimiter ";"

将 CSV 导入 Excel 非常简单,您可以根据需要过滤和分析内容。

[玩转系统] Exchange Online:使用 PowerShell 查看邮箱大小和添加的移动设备

带有交换在线连接的完整脚本

[string]$timestamp = (Get-Date -Format yyyy-MM-dd-HH-mm) 
###### insert path to csv export
$csvpath = "C:\temp\exon-stats-$timestamp.csv"
######

#install module for all users on pc (once)
Install-Module ExchangeOnlineManagement # -Verbose -Scope AllUsers -Force
#import module 
Import-Module ExchangeOnlineManagement 
#connecto to exchange online (admin credentials needed)
Connect-ExchangeOnline 
#see statistics of mailboxes
$mbs = Get-Mailbox -ResultSize Unlimited | Sort-Object name
#create an array for all statistics
$mbstats = @()
foreach($mb in $mbs){
  $dn = (($mb).identity)
  $activesync = (Get-CASMailbox -Identity "$dn").hasactivesyncdevicepartnership
  if ($activesync -eq $true){
    #fetching active sync device information
    $activesyndevice = Get-MobileDevice -Mailbox "$dn" | Select-Object DeviceType,DeviceOS,DeviceUserAgent,WhenCreated
    $devicemodel = @()
    $devicemodel = ($activesyndevice) | Select-Object DeviceType -ExpandProperty DeviceType 
    $devicemodel = $devicemodel -join '||' 
    $deviceos = @()
    $deviceos = ($activesyndevice) | Select-Object DeviceOS -ExpandProperty DeviceOS 
    $deviceos = $deviceos -join '||'
    $deviceuagent = @()
    $deviceuagent =  ($activesyndevice) | Select-Object DeviceUserAgent -ExpandProperty DeviceUserAgent
    $deviceuagent = $deviceuagent -join '||'
    $devicecreated = @()
    $devicecreated = ($activesyndevice) | Select-Object WhenCreated -ExpandProperty WhenCreated
    $devicecreated = $devicecreated -join '||'
    #putting everything together (active sync devices and mailbox statistics)
    $mbstat = Get-Mailbox -Identity "$dn" | Get-MailboxStatistics | Select-Object DisplayName,MailboxGuid,`
    @{name="TotalItemSize (MB)"; expression={[math]::Round(($_.TotalItemSize.ToString().Split("(")[1].Split(" ")[0].Replace(",","")/1MB),2)}},`
    ItemCount,@{name="ActiveSync";expression={"True"}}, @{name="DeviceType";expression={"$devicemodel"}},@{name="DeviceOS";expression={"$deviceos"}},`
    @{name="DeviceAgent";expression={"$deviceuagent"}},@{name="DeviceFirstConnected";expression={"$devicecreated"}}`
    #adding to array 
    $mbstats += $mbstat
  }
  else{
    #get statistics for non active sync users
    $mbstat = Get-Mailbox -Identity "$dn" | Get-MailboxStatistics | Select-Object DisplayName,MailboxGuid, @{name="TotalItemSize (MB)"; expression={[math]::Round(($_.TotalItemSize.ToString().Split("(")[1].Split(" ")[0].Replace(",","")/1MB),2)}},ItemCount,@{name="ActiveSync";expression={"False"}} 
    #adding to array
    $mbstats += $mbstat
  }
} 
#export information to csv with delimiter 
$mbstats | Export-Csv -Path "$csvpath" -Encoding UTF8 -Delimiter ";"

享受这个小脚本分析您的交换在线环境的乐趣。

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

取消回复欢迎 发表评论:

关灯