Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to query Azure virtual machine creation records

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Recently received a demand, want to take a look at the Azure virtual machine creation records, detailed understanding of the recent cloud what new resources, this is actually a relatively normal demand, as the cloud is more and more widely used, many enterprises are no longer satisfied with the simple use of the cloud, but more focused on how to make good use of the cloud, a more core point is that more and more enterprises begin to pay attention to the cloud cost problem Therefore, the rationality of the use of resources is more and more a focus of enterprises.

To return to the topic, then how to achieve this requirement in Azure? in fact, the creation record of VM can be found in the deployment record of the resource group, but the information collected in this way is very fragmented, and it is impossible for us to check and sort out this information one by one, so what is the good way?

In fact, we can solve this problem directly through Azure's PowerShell. We only need to write a simple script. First, run the following command to get all the log of Azure in the past three months.

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

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 ResourceGroup $($log.ResourceGroupName)"}}

So you can see the record created by VM!

What if you want to aggregate this information into Excel? You can use the following code!

[pscustomobject []] $VMObjects = $nullforeach ($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 ResourceGroup $($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

Finally, this method can only collect logs within 90 days, because the longest log open to users on the Azure platform is 90 days.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report