【问题标题】:What is the command for Enabling diagnostic logs on Azure AD with PowerShell?使用 PowerShell 在 Azure AD 上启用诊断日志的命令是什么?
【发布时间】:2018-08-15 21:23:41
【问题描述】:

我正在准备一个脚本,该脚本启用诊断日志将它们发送到存储帐户。

Get-AzureRmResource | foreach {  
    #For now adding all registered resources to Diagnostics Logs. Should narrow to specific resource types?
    #Categories "Execution", "Request" only, the "AllMetrics" category intended to log all categories fail. Can add specific categories to each resource type.
    Write-Output "Adding resource $_.ResourceId to the storage"
    Set-AzureRmDiagnosticSetting -ResourceId $_.ResourceId -StorageAccountId $storageid -Enabled $true -RetentionEnabled $true -RetentionInDays 90 -Categories “Execution”,“Request”
}

此 PowerShell 命令匹配以启用对订阅中创建的资源的诊断日志。

如何在 Azure Active Directory 中启用诊断日志?它们包括审核和登录日志?

有人可以请教吗?

非常感谢!

塞尔吉奥

更新:
我需要使用 PowerShell 自动执行以下操作:
1. 转到 Azure 门户
2. 在左侧边栏选项卡上,选择 Azure Active Directory
3. 选择审核日志或登录日志 4. 在顶部菜单中,选择导出数据设置
5.单击添加诊断设置
6. 选中存档到存储帐户并设置保留天数。
视频中描述的过程:
Video discussing Azure AD reports shows how to enable the Logs, I am required to automate enabling the logs, not getting the report

【问题讨论】:

    标签: azure powershell azure-active-directory azure-diagnostics


    【解决方案1】:

    MSOL 提供这些日志/审计资源。

    从 Azure 资源收集和使用日志数据

    Azure Monitor 诊断日志是由 Azure 服务发出的日志 提供有关该服务操作的丰富、频繁的数据。 Azure Monitor 提供两种类型的诊断日志:

    • 租户日志 - 这些日志来自现有的租户级服务 Azure 订阅之外,例如 Azure Active Directory 日志。

    • 资源日志 - 这些日志来自部署的 Azure 服务 Azure 订阅中的资源,例如网络安全 组或存储帐户。

    https://docs.microsoft.com/en-us/azure/monitoring-and-diagnostics/monitoring-overview-of-diagnostic-logs

    Azure Active Directory 审核日志

    目前从管理门户提供的审计事件也 可在 Azure Active Directory 审核报告中按文档下载 事件。现在可以方便组织的管理员收集 Azure Active Directory 中发生的关键更改 租户。

    https://blogs.msdn.microsoft.com/azuresecurity/2015/06/11/azure-active-directory-audit-logs

    除了上述之外,您还尝试启用哪些其他日志?

    你追求什么?

    注意:从诊断方法的角度来看,AAD 不是 ADDS。

    OP 更新

    Azure 事件的审核日志
    https://blogs.msdn.microsoft.com/cloud_solution_architect/2015/03/10/audit-logs-for-azure-events/

    通过 Azure 检索资源指标和创建警报规则 PowerShell

    指标定义

    Get-AzureRmMetric cmdlet 下载 Azure 的定义 洞察力指标。例如,以下检索定义 对于名为 myRG 的资源组中名为 myVM 的 VM:

    $resourceId = '/subscriptions/SUBSCRIPTION_guid/resourceGroups/myRG/providers/Microsoft.Compute/virtualMachines/myVM'
    
    Get-AzureRmMetricDefinition –ResourceId $resourceId `
     -DetailedOutput
    

    https://blogs.msdn.microsoft.com/cloud_solution_architect/2016/02/26/retrieving-resource-metrics-and-creating-alert-rules-via-azure-powershell

    Example 4: Get all resources with a given name
    
    PowerShell = Copy  ( Try It 
    PS C:\> Get-AzureRmResource -Name testVM | fl
    
    Name              : testVM
    ResourceGroupName : testRG
    ResourceType      : Microsoft.Compute/virtualMachines
    Location          : westus
    ResourceId        : /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/testRG/providers/Microsoft.Compute/virtualMachines/testVM
    
    
    Example 7: Get a resource by resource id
    
    PowerShell = Copy  ( Try It 
    PS C:\> Get-AzureRmResource -ResourceId /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/testRG/providers/Microsoft.Compute/virtualMachines/testVM
    
    Name              : testVM
    ResourceGroupName : testRG
    ResourceType      : Microsoft.Compute/virtualMachines
    Location          : westus
    ResourceId        : /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/testRG/providers/Microsoft.Compute/virtualMachines/testVM
    

    https://docs.microsoft.com/en-us/powershell/module/azurerm.resources/get-azurermresource?view=azurermps-6.7.0

    【讨论】:

    • 感谢您的回答@postanote。我在 AAD 审核和登录日志之后。它们是此处插入的视频中显示的 docs.microsoft.com/en-us/azure/active-directory/… 如所见,在 Azure 门户中,它们进入 AAD 节点,然后启用审核和登录日志。我想自动化这个。在您的第一个链接上,在“通过 PowerShell 启用资源诊断日志收集”部分的 PowerShell 语法中,他们使用 ResourceId 参数。当我查询RM资源时,我没有得到AD resourceIds,所以应该有另一种方式。
    • 查看我的更新。您是说,您已经查看了这些内容,但它们并没有为您提供您所追求的东西?
    • 感谢更新。确切地说,我在来这里之前已经对此进行了审查。我了解 Get-AzureRmResource 和 Set-AzureRmDiagnosticSetting cmdlet 的使用。问题是如何获取 Azure AD 资源 ID 以调用 Set-AzureRmDiagnosticSetting cmdlet,或者是否有不同的 PowerShell cmdlet 来启用 Azure AD 的诊断日志记录。在上下文设置为我的 Azure AD 租户的 exec Get-AzureRmResource 上,我没有获得 Azure AD 资源。在诊断日志概述的描述中存在租户日志和资源日志之间的区别,它们没有在文章中涉及租户日志。
    【解决方案2】:

    目前,似乎没有 powershell 命令可以直接获取 Azure AD 审核和登录日志。

    如果 Micorsoft Graph Rest API 可接受,您可以使用以下 Microsoft graph Rest API 来执行此操作。

    GET tenant user activities  https://graph.microsoft.com/beta/auditLogs/directoryAudits 
    

    GET tenant user sign-ins    https://graph.microsoft.com/beta/auditLogs/signIns
    

    我们也可以从link 获取演示代码。 如果我们想运行代码。我们需要做先决条件才能访问 Azure Active Directory 报告 API,更多信息请参考document

    $URIfilter = "?`$filter=activityDateTime gt $PastPeriod"  
    $url = "https://graph.microsoft.com/beta/auditLogs/directoryAudits" + $URIfilter 
    GetReport $url "DirectoryAudits" $Tenantdomain
    

    有关 Azure AD 报告的更多信息,请参阅此tutorial

    更新:

    我们可以使用以下 Rest API 来启用/更新 Azure 审核日志或登录日志。

    Put https://management.azure.com/providers/microsoft.aadiam/diagnosticSettings/{name}?api-version=2017-04-01-preview
    

    身体

    {
      "properties": {
        "logs": [
          {
            "category": "AuditLogs",
            "enabled": true,
            "retentionPolicy": {
              "days": 0,
              "enabled": false
            }
          },
          {
            "category": "SignInLogs",
            "enabled": true,
            "retentionPolicy": {
              "days": 0,
              "enabled": false
            }
          }
        ],
        "metrics": [],
        "storageAccountId": "/subscriptions/{subscriptionId}/resourceGroups/{resourgroupName}/providers/Microsoft.Storage/storageAccounts/{storageAccountName}"
      }
    

    我用邮递员测试它。

    【讨论】:

    • 感谢您的回答@Tom Sun。问题不是如何获取/获取/提取 Azure AD 审核和登录日志。问题是关于如何使用 PowerShell 启用这些日志,所以他们可用,即在存储帐户中。 Azure 门户术语中的自动化步骤如下: 1. 转到 Azure 门户 2. 在左侧边栏选项卡上,选择 Azure Active Directory 3. 选择审核日志或登录日志 4. 在顶部菜单上,选择导出数据设置,5.单击添加诊断设置 6. 选中存档到存储帐户并设置保留天数。视频中描述的过程:youtube.com/embed/syT-9KNfug8?start=84
    • 我们可以使用以下其余 API https://management.azure.com/providers/microsoft.aadiam/diagnosticSettings/{name}?api-version=2017-04-01-preview 创建或更新日志我还使用详细信息更新了答案
    • 嗨,汤姆,如果我用服务主体调用 apihttps://management.azure.com/providers/microsoft.aadiam/diagnosticSettings/write,sp 应该有什么权限?一个类似的问题需要你的帮助,stackoverflow.com/questions/53666223/… ;-)
    • @JoyWang 如果应用程序权限不起作用。您是否尝试过使用用户权限?也请尝试使用全局管理员用户
    • @TomSun 刚刚在这里遇到了这个问题,我是另一篇文章的 OP。我还收到了 Microsoft 的支持票,他们也无法使显然对您有用的 API 调用正常工作。我的 SP 对 AAD 租户具有全局管理员权限,对 Log Analytics 工作区所在的订阅具有所有者权限。我收到另一篇文章中所述的“未经授权”错误。
    猜你喜欢
    • 1970-01-01
    • 2018-02-10
    • 1970-01-01
    • 2017-06-04
    • 1970-01-01
    • 2014-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多