在不使用子进程库和 powershell.exe 的情况下获取 Azure AD 登录的替代方法 Python

Alternate way to get Azure AD sign in logs in Python without using subprocess library and powershell.exe

据我所知,通过查看其他帖子,使用 Python 中的子进程库是从 Python 中执行 PowerShell 命令的最流行方式。例如:

data = subprocess.check_output(["powershell.exe", "Connect-AzureAD -AccountId me@me.com \n Get-AzureADAuditSignInLogs"]).decode(sys.stdout.encoding)

但是,我最终尝试将此脚本添加到 Azure 中的自动化帐户。在脚本中指定可执行文件“powershell.exe”在 Azure 自动化帐户 Runbook 中不起作用(引发“FileNotFound”错误)。

是否有其他方法可以从 Python 中获取更适合在 Azure 自动化帐户运行手册中使用的登录日志数据?可以使用 Azure 库代替获取登录日志数据吗?

我已经在我的环境中测试过

您可以在 python 脚本中使用 MS Graph API 查询来获取用户的 Azure AD 登录日志

您可以使用下面的 python 代码:

import requests
import json

url = 'https://graph.microsoft.com/v1.0/auditLogs/signIns'
token = "access_token"

headers = {
 'Authorization': 'Bearer {}'.format(token)
}

user_response_data = json.loads(requests.get(url, headers=headers).text)
print(user_response_data)

参考Querying Microsoft Graph API with Python | by Ephraim Mwai | Towards Data Science