Azure活动日志

时间:2019-08-30 11:54:18

标签: azure azure-devops azure-powershell azure-log-analytics

我想监视谁对rbac分配进行了更改,我创建了Powershell脚本来从Azure活动日志中收集数据。我用下面的代码。使用此解决方案,我可以获得以下项目: 呼叫者-进行角色分配更改的用户, 时间戳记 资源名称-在此资源分配更改中, 动作类型-写入或删除

在Azure门户的“活动日志”面板中的“摘要”门户中(消息:与“用户信息”共享),我可以看到已被授予资源权限/分配但使用Powershell脚本的用户名无法捕获此信息,是否有任何方法可以获取此信息?

Get-AzureRmLog -StartTime (Get-Date).AddDays(-7) | 
Where-Object {$_.Authorization.Action -like 
'Microsoft.Authorization/roleAssignments/*'} |
Select-Object @{N="Caller";E={$_.Caller}}, 
@{N="Resource";E={$_.Authorization.Scope}}, 
@{N="Action";E={Split-Path $_.Authorization.action -leaf}},
EventTimestamp

脚本输出:

Caller         : username@xxx.com
Resource   :/subscriptions/xxxx/resourceGroups/Powershell/providers/Microsoft.Compute/virtualMachines/xx/providers/Microsoft.Authorization/roleAssignments/xxxx
Action         : write
EventTimestamp : 8/29/2019 10:12:31 AM

3 个答案:

答案 0 :(得分:0)

这对您有用吗?

Get-AzureRmLog -StartTime (Get-Date).AddDays(-7) | 
Where-Object {$_.Authorization.Action -like 'Microsoft.Authorization/roleAssignments/*'} | 
Select-Object @{N="Caller";E={$_.Caller}}, 
@{N="Resource";E={$_.Authorization.Scope}}, 
@{N="Action";E={Split-Path $_.Authorization.action -leaf}},
@{N="Name";E={$_.Claims.Content.name}}, 
EventTimestamp

我的输出:

Caller         : username@domain.com
Resource       : /subscriptions/xxxx/resourceGroups/xxxx/providers/Microsoft.Authorization/roleAssignments/xxxx
Action         : write
Name           : John Doe
EventTimestamp : 30.08.2019 12.05.52

NB:我使用了Get-AzLog。不确定Get-AzLog和Get-AzureRmLog之间是否有任何区别。

答案 1 :(得分:0)

可以肯定的是,此cmdlet不会公开此内容。我什至在角色分配中都没有看到此信息。所以不确定您的确切意思。

答案 2 :(得分:0)

使用Az PowerShell cmdlet Get-AzLogGet-AzureRmLog当前不支持您获取分配了RBAC角色的用户名的要求。

但是,我们可以利用Activity Logs - List和Azure PowerShell cmdlet Get-AzureADUser的Azure REST API来满足您的要求。

通过这种方式,我们依赖于Activity Logs - List的Azure REST API(但是您似乎希望使用PowerShell的方式来满足要求),因此请在PowerShell中调用REST API,如下所示。

    $request = "https://management.azure.com/subscriptions/{subscriptionId}/providers/microsoft.insights/eventtypes/management/values?api-version=2015-04-01&`$filter={$filter}"
    $auth = "eyxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    $authHeader = @{
    'Content-Type'='application/json'
    'Accept'='application/json'
    'Authorization'= "Bearer $auth"
    }
    $Output = Invoke-RestMethod -Uri $request -Headers $authHeader -Method GET -Body $Body
    $ActivityLogsFinalOutput = $Output.Value

开发PowerShell代码,以从用于Activity Logs - List调用的Azure REST API的输出中获取“ PrincipalId”(位于“属性”下)。提取的“ PrincipalId”是您最终希望获得的用户的ObjectID。

现在利用Az PowerShell cmdlet Get-AzureADUser,使您的命令如下所示。

(Get-AzureADUser -ObjectID "<PrincipalID>").DisplayName

希望这会有所帮助!!干杯!

更新:

请找到PowerShell的方法,以获取需要在上述REST API调用中使用的身份验证令牌(即$ auth)。

$ClientID       = "<ClientID>" #ApplicationID
$ClientSecret   = "<ClientSecret>"  #key from Application
$tennantid      = "<TennantID>"

$TokenEndpoint = {https://login.windows.net/{0}/oauth2/token} -f $tennantid 
$ARMResource = "https://management.core.windows.net/";

$Body1 = @{
        'resource'= $ARMResource
        'client_id' = $ClientID
        'grant_type' = 'client_credentials'
        'client_secret' = $ClientSecret
}

$params = @{
    ContentType = 'application/x-www-form-urlencoded'
    Headers = @{'accept'='application/json'}
    Body = $Body1
    Method = 'Post'
    URI = $TokenEndpoint
}

$token = Invoke-RestMethod @params
$token | select access_token, @{L='Expires';E={[timezone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddSeconds($_.expires_on))}} | fl *

我也看到了this的新方法,但是我没有机会进行测试。如果有兴趣,您可以选择尝试此方法或采用上述方法。

UPDATE2:

$ActivityLogsFinalOutput| %{
    if(($_.properties.responseBody) -like "*principalId*"){
        $SplittedPrincipalID = $_.properties.responseBody -split "PrincipalID"
        $SplittedComma = $SplittedPrincipalID[1] -split ","
        $SplittedDoubleQuote = $SplittedComma[0] -split "`""
        $PrincipalID = $SplittedDoubleQuote[2]
        #Continue code for getting Azure AD User using above fetched $PrincipalID
        #...
        #...
    }
}

enter image description here