如何在PowerShell中提取Exchange Online主邮箱位置作为变量
提取Exchange Online主邮箱位置的PowerShell实现
要获取MailboxLocations中标记为Primary的主邮箱位置,你需要直接操作Get-Mailbox返回的对象(不要用Format-List,它仅用于格式化输出,会破坏对象结构无法后续处理),具体实现如下:
修改后的完整脚本
#### Connect to exchange online Connect-ExchangeOnline #### Set Username $Username = "Username@domain.com" #### Get the recoverable items size in GB Get-MailboxFolderStatistics $Username -FolderScope RecoverableItems | Format-List Name,FolderAndSubfolderSize #### Enable Archive Enable-Mailbox -Identity $Username -Archive #### 获取邮箱对象并提取主邮箱位置 $mailbox = Get-Mailbox -Identity $Username $primary = $mailbox.MailboxLocations | Where-Object { $_.IsPrimary -eq $true } | Select-Object -ExpandProperty Value #### Start managed folder assistant Start-ManagedFolderAssistant -Identity $primary
关键逻辑说明
$mailbox = Get-Mailbox -Identity $Username:将邮箱的完整对象存储到变量中,保留所有属性和结构,避免格式化命令破坏对象$mailbox.MailboxLocations:这是一个包含邮箱所有位置的集合,每个元素包含IsPrimary(是否为主邮箱)和Value(位置标识字符串)两个核心属性Where-Object { $_.IsPrimary -eq $true }:筛选出集合中标记为主邮箱的项Select-Object -ExpandProperty Value:直接提取主邮箱对应的位置标识字符串(即你需要的1;45af8ec4--8c8b-80290cdf4d格式内容),赋值给$primary变量
内容的提问来源于stack exchange,提问作者Sjaaktrekhaak
相关产品推荐
相关产品推荐

