我的同事使用下面的powerhell查询来检索过去4天的日志数据,不包括今天,它匹配资源的操作,并收集功能,如EventTimeStamp,调用者,SubscriptionId等。
Get-AzureRmLog -StartTime (Get-Date).AddDays(-4) -EndTime (Get-Date).AddDays(-1) | Where-Object {$_.OperationName.LocalizedValue -match "Start|Stop|Restart|Create|Update|Delete"} |
Select-Object EventTimeStamp, Caller, SubscriptionId, @{name="Operation"; Expression = {$_.operationname.LocalizedValue}},
我是一个新手,想生成一份报告,在这份报告中,我还可以获取过去90天的资源的 "标签 "名称& 值。这将是什么powerhell查询?我也可以使用python来查询这些数据吗?我试着搜索文档,但无法深入了解,所以如果有人能把我转到正确的地方,那将会很有帮助。
首先,你应该知道,并不是所有的 azure 资源都可以指定标签,所以你应该在你的代码中考虑这一点。请参考 Azure资源的标签支持 来检查哪些 azure 资源支持标签。
对于powerhell查询,我建议使用 新的 azure powershell az 模块 代替以往 azureRM模块.
下面是一个简单的powerhell代码 氮气模块. 而为了测试的目的,我只是介绍如何获取和添加标签到输出。请根据你的要求随意修改。
#for testing purpose, I just get the azure activity logs from a specified resource group
$mylogs = Get-AzLog -ResourceGroupName "a resource group name"
foreach($log in $mylogs)
{
if(($log.Properties.Content.Values -ne $null))
{
#the tags is contains in the Properties of the log entry.
$s = $log.Properties.Content.Values -as [string]
if($s.startswith("{"))
{
$log | Select-Object EventTimeStamp, Caller, SubscriptionId,@{name="Operation"; Expression = {$_.operationname.LocalizedValue}}, @{name="tags"; Expression = {($s | ConvertFrom-Json).tags}}
}
#if it does not contains tags.
else
{
$log | Select-Object EventTimeStamp, Caller, SubscriptionId,@{name="Operation"; Expression = {$_.operationname.LocalizedValue}}, @{name="tags"; Expression = {""}}
}
}
#if it does not contains tags.
else
{
$log | Select-Object EventTimeStamp, Caller, SubscriptionId,@{name="Operation"; Expression = {$_.operationname.LocalizedValue}}, @{name="tags"; Expression = {""}}
}
Write-Output "************************"
}
测试结果。
对于python来说,你可以看一下: 这个github问题 其中介绍了如何从azure活动日志中获取日志,但如何在输出中添加标签,你需要做一些研究。
希望对大家有所帮助。