PowerShell检查Outlook日历权限报错:指定邮箱不存在,如何修正?
解决PowerShell获取Outlook日历权限时的身份识别错误
问题场景
尝试编写以下PowerShell代码检查Outlook日历权限:
$Usermailbox = Read-Host "Please enter the mailbox whose permissions you want to see" Get-MailboxFolderPermission -Identity "$Usermailbox :\calendar"
执行时出现错误:
Get-MailboxFolderPermission: xxxx|Microsoft.Exchange.Configuration.Tasks.ManagementObjectNotFoundException|The specified mailbox Identity:“xxx ” doesn’t exist.
问题根源是字符串拼接错误,代码将$Usermailbox :\calendar错误识别为包含多余空格的身份字符串,导致Exchange无法找到对应邮箱。
修正方案
正确的拼接方式需要明确变量边界,同时移除多余空格,有以下几种写法:
- 使用花括号包裹变量,明确变量范围:
$Usermailbox = Read-Host "Please enter the mailbox whose permissions you want to see" Get-MailboxFolderPermission -Identity "${Usermailbox}:\calendar"
- 直接移除变量后的空格,PowerShell会自动解析变量:
$Usermailbox = Read-Host "Please enter the mailbox whose permissions you want to see" Get-MailboxFolderPermission -Identity "$Usermailbox:\calendar"
- 使用字符串连接运算符
+拼接:
$Usermailbox = Read-Host "Please enter the mailbox whose permissions you want to see" Get-MailboxFolderPermission -Identity ($Usermailbox + ":\calendar")
说明
原代码中"$Usermailbox :\calendar"的问题在于变量后多了一个空格,导致生成的身份字符串格式为"邮箱名 :\calendar",Exchange会把包含空格的部分当成邮箱名称去查找,自然找不到对应对象。通过明确变量边界或移除多余空格,就能生成正确的身份格式"邮箱名:\calendar",让Exchange正确识别邮箱和日历文件夹。
内容的提问来源于stack exchange,提问作者Wiktor
相关产品推荐
相关产品推荐

