温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么查询Azure虚拟机的创建记录

发布时间:2020-06-04 17:26:32 来源:亿速云 阅读:327 作者:Leah 栏目:云计算

这篇文章给大家分享的是查询Azure虚拟机的创建记录的方法,相信大部分人都还没学会这个技能,为了让大家学会,给大家总结了以下内容,话不多说,一起往下看吧。

怎么查询Azure虚拟机的创建记录




    其实我们可以直接通过Azure的PowerShell解决这个问题,只需要编写一个简单的脚本就可以了,首先运行以下命令,获取到Azure近三个月的所有log

    

$logs = Get-AzureRmLog -ResourceProvider Microsoft.Compute -StartTime (Get-Date).AddDays(-90) -Maxrecord 100000


怎么查询Azure虚拟机的创建记录

foreach($log in $logs)
{
    if(($log.OperationName.Value -eq 'Microsoft.Compute/virtualMachines/write') -and ($log.SubStatus.Value -eq 'Created'))
    {
        Write-Output "$($log.caller) created vm $($log.Id.split("/")[8])  at $($log.EventTimestamp)  in Resource Group $($log.ResourceGroupName)"
    }

}

    



这样就能看到VM创建的记录了!

怎么查询Azure虚拟机的创建记录



那么如果想把这些信息汇总到Excel里呢?可以通过以下的代码即可!

[pscustomobject[]]$VMObjects = $null
foreach ($log in $logs) {
        if (($log.OperationName.Value -eq 'Microsoft.Compute/virtualMachines/write') -and ($log.SubStatus.Value -eq 'Created')) {
            Write-Output "$($log.caller) created vm $($log.Id.split("/")[8])  at $($log.EventTimestamp)  in Resource Group $($log.ResourceGroupName)"

            $VMObject = New-Object -TypeName psobject
            $VMObject | Add-Member -MemberType NoteProperty -Name SubscriptionName -Value $SubscriptionName
            $VMObject | Add-Member -MemberType NoteProperty -Name SubscriptionID -Value $SubscriptionID
            $VMObject | Add-Member -MemberType NoteProperty -Name ResourceGroup -Value $log.ResourceGroupName
            $VMObject | Add-Member -MemberType NoteProperty -Name VMName -Value $log.Id.split("/")[8]
            $VMObject | Add-Member -MemberType NoteProperty -Name Time -Value $log.EventTimestamp
            $VMObjects += $VMObject

        }

    }
    
    $OutputPath="C:\vm.csv"
    $VMObjects | Export-Csv -NoTypeInformation -LiteralPath $OutputPath


最后要说的是,这种方法只能收集到90天以内的日志,因为Azure平台开放给用户的最长时间的log就是90天

关于查询Azure虚拟机的创建记录的方法就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI