据我通过查看其他帖子可知,使用 Python 中的子进程库是在 Python 中执行 PowerShell 命令的最流行方法。例如:
data = subprocess.check_output(["powershell.exe", "Connect-AzureAD -AccountId <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f79a92b79a92d994989a" rel="noreferrer noopener nofollow">[email protected]</a> \n Get-AzureADAuditSignInLogs"]).decode(sys.stdout.encoding)
但是,我最终尝试将此脚本添加到 Azure 中的自动化帐户。在脚本中指定可执行文件“powershell.exe”在 Azure 自动化帐户 Runbook 中不起作用(引发“FileNotFound”错误)。
是否有其他方法可以从 Python 中获取登录日志数据,并且更适合在 Azure 自动化帐户 Runbook 中使用?可以使用 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
关于python - 在 Python 中获取 Azure AD 登录日志的替代方法,无需使用子进程库和 powershell.exe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71005240/