You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 04:12:47