python - 在 Python 中获取 Azure AD 登录日志的替代方法,无需使用子进程库和 powershell.exe

标签 python powershell azure-active-directory azure-automation

据我通过查看其他帖子可知,使用 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/

相关文章:

spring-boot - 如何使用 Spring Boot Starter oAuth2 客户端在从 Azure Active Directory (AAD) 返回的 OAuth2User 中包含更多属性

python - Pandas - 度假村柱位置

c# - 在服务器上执行 powershell .ps1 文件并查看结果 C# asp.net

shell - PowerShell 相当于 bash `exec >>(tee -a $logfile); exec 2>>(tee -a $logfile >&2)`

.net-core - 获取 401 : Unauthorized response when querying groups endpoint of Azure AD Graph API

sql-server - 使用 Azure Active Directory 凭据连接本地 SQL Server 数据库

python - 使用 Dask 读取时如何按日期时间列过滤 Parquet 文件?

当正则表达式匹配时,Python 的 Re.Sub 不改变任何内容

python - groupby 并根据另一列的值保留一列的信息

powershell - 内联函数调用,其中需要表达式