目前不支持使用 Az PowerShell cmdlet Get-AzLog 或 Get-AzureRmLog 获取分配了 RBAC 角色的用户名的要求。
但是,我们可以利用 Azure REST API for Activity Logs - List 和 Az PowerShell cmdlet Get-AzureADUser 来满足您的要求。
以这种方式,因为我们依赖于 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 代码以从 Azure REST API 的输出中获取“PrincipalId”(位于“properties”下)以进行 Activity Logs - List 调用。获取到的“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 新方法,但我没有机会对此进行测试。如果有兴趣,您也可以试试这个或采用上述方法。
更新 2:
$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
#...
#...
}
}