You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Get-AzureADAuditDirectoryLogs命令中自动使用Get-Date实现每日日志下载

使用Get-Date自动生成日期下载Azure AD审计日志

当然可以用Get-Date替代手动输入的日期,这也是实现每日自动下载审计日志的核心操作。

  • 如果需要获取前一天的全部审计日志,可以先把Get-Date生成的日期转换成Azure AD API要求的格式,再嵌入到Filter参数中:
# 生成昨天的日期字符串(格式:yyyy-MM-dd)
$targetDate = (Get-Date).AddDays(-1).ToString("yyyy-MM-dd")
# 拉取日志并导出,文件名包含日期避免覆盖
Get-AzureADAuditDirectoryLogs -Filter "activityDateTime gt $targetDate" | Export-Csv -Path "C:\auditlogs_$targetDate.csv" -NoTypeInformation
  • 如果你的自动任务在当天凌晨执行,想要精准获取前一天0点到24点的日志,可以进一步限定时间范围:
# 生成前一天0点的起始时间和当天0点的结束时间(符合ISO 8601格式)
$startTime = (Get-Date).AddDays(-1).Date.ToString("yyyy-MM-ddTHH:mm:ssZ")
$endTime = (Get-Date).Date.ToString("yyyy-MM-ddTHH:mm:ssZ")
# 拉取指定时间段的日志并导出
Get-AzureADAuditDirectoryLogs -Filter "activityDateTime ge $startTime and activityDateTime lt $endTime" | Export-Csv -Path "C:\auditlogs_$((Get-Date).AddDays(-1).ToString("yyyy-MM-dd")).csv" -NoTypeInformation

把这段脚本加到Windows任务计划中,设置每日固定时间执行,就能实现自动下载的需求了。

内容的提问来源于stack exchange,提问作者The Freelancer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 13:05:45