简体   繁体   English

从 Python 获取 Office 365 邮箱和组

[英]Get Office 365 Mailboxes and Groups from Python

A coworker made a working PowerShell script to retrieve Mailboxes and Groups from Office 365. We have both admin rights on Office 365 = I can get all of these information with a internet browser on EAC.一位同事制作了一个有效的 PowerShell 脚本来从 Office 365 中检索邮箱和组。我们在 Office 365 上拥有管理员权限 = 我可以使用 EAC 上的 Internet 浏览器获取所有这些信息。 When I'm executing the PowerShell script with my Office 365 credentials, I get the excepted results.当我使用我的 Office 365 凭据执行 PowerShell 脚本时,我得到了异常结果。 It means that I have the rights access and permissions on Exchange.这意味着我在 Exchange 上具有访问权限和权限。

My need is to create a Python script to do almost the same thing then to create a human-readable Excel Workbook (probably using openpyxl) and send email later.我需要创建一个 Python 脚本来做几乎相同的事情然后创建一个人类可读的 Excel 工作簿(可能使用 openpyxl)并稍后发送 Z0C83F57C786A0B4A39EFAB23731CEBCZ。 Many of you will ask to me why I don't complete the PowerShell script, the simple answer is that this script will be a little part of a biggest project, written in Python.你们中的许多人会问我为什么不完成 PowerShell 脚本,简单的回答是这个脚本将是一个最大项目的一小部分,用 Python 编写。

Here, the PowerShell script:在这里,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

I don't want an application to have access to Azure AD.我不希望应用程序有权访问 Azure AD。 The same way the Powershell script ask for user Credentials, I would like my python script to prompt for Office 365 credentials.与 Powershell 脚本要求用户凭据的方式相同,我希望我的 python 脚本提示输入 Office 365 凭据。 I would like my python script to work this way to be sure only office 365 users with right rights can use this python script.我希望我的 python 脚本以这种方式工作,以确保只有具有正确权限的 office 365 用户才能使用此 python 脚本。

I have tested 4 python libraries, for all of them, I succeed the authentication but I don't find the right way to list all groups and mailboxes我已经测试了 4 个 python 库,对于所有这些库,我都成功通过了身份验证,但我找不到列出所有组和邮箱的正确方法

Using ExchangeLib ( https://github.com/ecederstrand/exchangelib ):使用 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.

I'm certainly not using this library correctly because I don't get why I need more permissions than the ones I already have?我当然没有正确使用这个库,因为我不明白为什么我需要比我已经拥有的权限更多的权限? I've followed this page, but I've got the same error: https://documentation.arcserve.com/Arcserve-UDP/Available/V6.5/ENU/Bookshelf_Files/HTML/Solutions%20Guide/UDPSolnGuide/udp_add_role_group_exchgonline.htm我已经关注了这个页面,但我遇到了同样的错误: https://documentation.arcserve.com/Arcserve-UDP/Available/V6.5/ENU/Bookshelf_Files/HTML/Solutions%20Guide/UDPSolnGuide/udp_add_role_group_exchgonline。 htm

I tried Azure SDK for Python ( https://github.com/Azure/azure-sdk-for-python ), O365 ( https://github.com/O365/python-o365 ) and Office365-REST-Python-Client ( https://github.com/vgrem/Office365-REST-Python-Client ). I tried Azure SDK for Python ( https://github.com/Azure/azure-sdk-for-python ), O365 ( https://github.com/O365/python-o365 ) and Office365-REST-Python-Client ( https://github.com/vgrem/Office365-REST-Python-Client )。 I succeed to (seems to) authenticate with all of them, but failed to list all mailboxes or groups.我成功(似乎)向所有人进行了身份验证,但未能列出所有邮箱或组。

Anyone could help?任何人都可以帮忙吗?

I don't know about the Exchange Graph API, but EWS simply does not provide this information.我不知道 Exchange Graph API,但 EWS 根本不提供此信息。 Your best bet is the GetSearchableMailboxes service that you tried in exchangelib.您最好的选择是您在 exchangelib 中尝试过的GetSearchableMailboxes服务。 EWS requires users to have the Discovery Management RBAC role for this to succeed. EWS 要求用户具有发现管理 RBAC 角色才能成功。

Since you already have PowerShell commands that work for you, I would probably just call these commands from your Python script using a subprocess.由于您已经拥有适合您的 PowerShell 命令,我可能会使用子进程从您的 Python 脚本中调用这些命令。 Here's a blog post with some examples: https://www.phillipsj.net/posts/executing-powershell-from-python/这是一个包含一些示例的博客文章: https://www.phillipsj.net/posts/executing-powershell-from-python/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM