【发布时间】:2021-05-26 04:38:19
【问题描述】:
一位同事制作了一个有效的 PowerShell 脚本来从 Office 365 中检索邮箱和组。 我们在 Office 365 上拥有管理员权限 = 我可以使用 EAC 上的 Internet 浏览器获取所有这些信息。当我使用我的 Office 365 凭据执行 PowerShell 脚本时,我得到了异常结果。这意味着我在 Exchange 上具有访问权限和权限。
我需要创建一个 Python 脚本来做几乎相同的事情,然后创建一个人类可读的 Excel 工作簿(可能使用 openpyxl)并稍后发送电子邮件。很多人会问我为什么不完成 PowerShell 脚本,简单的回答是这个脚本将是一个用 Python 编写的最大项目的一小部分。
这里是 PowerShell 脚本:
Install-Module -Name AzureAD
Install-Module -Name MSOnline
$CSV_Groups = "C:\tmp\Groups.csv"
#Get Credentials to connect
$Credential = Get-Credential
#Create the session
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Credential -Authentication Basic -AllowRedirection
#Import the session
Import-PSSession $Session -DisableNameChecking
$O365Groups = Get-UnifiedGroup
ForEach ($Group in $O365Groups) {
Get-UnifiedGroupLinks –Identity $Group.Id –LinkType Members | Select-Object @{Name="Group Name";Expression={$Group.DisplayName}}, @{Name="Group Address";Expression={$Group.PrimarySmtpAddress}}, @{Name="User Name";Expression={$_.DisplayName}}, PrimarySmtpAddress | Export-CSV $CSV_Groups -NoTypeInformation -Append -encoding UTF8
}
$O365Groups = Get-DistributionGroup
ForEach ($Group in $O365Groups) {
Get-DistributionGroupMember –Identity $Group.Id | Select-Object @{Name="Group Name";Expression={$Group.DisplayName}}, @{Name="Group Address";Expression={$Group.PrimarySmtpAddress}}, @{Name="User Name";Expression={$_.DisplayName}}, PrimarySmtpAddress | Export-CSV $CSV_Groups -NoTypeInformation -Append -encoding UTF8
}
$CSV_Mailboxes = "C:\tmp\Mailboxes.csv"
Get-Mailbox -RecipientTypeDetails SharedMailbox -ResultSize:Unlimited | Get-MailboxPermission | Select-Object Identity,User,AccessRights | Export-Csv $CSV_Mailboxes -NoTypeInformation
Remove-PSSession $Session
我不希望应用程序有权访问 Azure AD。与 Powershell 脚本要求用户凭据的方式相同,我希望我的 python 脚本提示输入 Office 365 凭据。我希望我的 python 脚本以这种方式工作,以确保只有具有正确权限的 office 365 用户才能使用这个 python 脚本。
我已经测试了 4 个 python 库,对于所有这些库,我都通过了身份验证,但我找不到列出所有组和邮箱的正确方法
使用 ExchangeLib (https://github.com/ecederstrand/exchangelib):
import exchangelib
exchangelib_credentials = exchangelib.Credentials(
username=self.username,
password=self.password
)
exchangelib_account = exchangelib.Account(
primary_smtp_address="my_email@address.com",
credentials=exchangelib_credentials,
autodiscover=True
)
for mailbox in account.protocol.get_searchable_mailboxes(expand_group_membership=True):
print(mailbox)
# ==> exchangelib.errors.ErrorAccessDenied:
# The caller has not assigned any of the RBAC roles requested in the management role header.
我肯定没有正确使用这个库,因为我不明白为什么我需要比我已经拥有的权限更多的权限? 我已经关注了这个页面,但我遇到了同样的错误: https://documentation.arcserve.com/Arcserve-UDP/Available/V6.5/ENU/Bookshelf_Files/HTML/Solutions%20Guide/UDPSolnGuide/udp_add_role_group_exchgonline.htm
我尝试了 Azure SDK for Python (https://github.com/Azure/azure-sdk-for-python)、O365 (https://github.com/O365/python-o365) 和 Office365-REST-Python-Client (https://github.com/vgrem/Office365-REST-Python-Client)。 我成功(似乎)向所有这些人进行身份验证,但未能列出所有邮箱或组。
有人可以帮忙吗?
【问题讨论】:
标签: python exchangewebservices office365api azure-sdk-python exchangelib