- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get Azure VM activity logs using PowerShell?
To get the Azure VM activity logs with PowerShell, we need to use the Get-AzLog command. Before running, AZ commands make sure that you are connected to the Azure Account using (ConnectAzAccount) and the subscription (Set-AzContext).
We have the below TestVM, we need to retrieve activity logs and we need its resource id. We will get the resource ID using,
PS C:\> $vm = Get-AzVM -VMName TestVM
PS C:\> $vm.Id
We need to use this ID in the Get-AzLog command to retrieve the activity logs.
PS C:\> Get-AzLog -ResourceId $vm.Id
It will provide all the azure events for that specific resource group and you can see the number of properties. If we need to retrieve the only properties which are shown in the azure activity log on the portal, you can use the below command.
(Get-AzLog -ResourceId $vm.Id) | Select @{N='VMName';E={$vm.Name}},ResourceGroupName, @{N='Message';E={$_.OperationName.LocalizedValue}}, Level, Caller, EventTimestamp | ft -AutoSize
You can filter the event with a specific level. For example, to filter only warning logs,
(Get-AzLog -ResourceId $vm.Id) | where{$_.Level -eq "Warning"} | Select @{N='VMName';E={$vm.Name}},ResourceGroupName, @{N='Message';E={$_.OperationName.LocalizedValue}},Level, Caller, EventTimestamp
You can add the start and end times as shown below.
Get-AzLog -ResourceId $vm.Id -StartTime 2021-03-01 -EndTime 2021-05-25 -MaxRecord 20 | ` Select @{N='VMName';E={$vm.Name}},ResourceGroupName, @{N='Message';E={$_.OperationName.LocalizedValue}},Level, Caller, EventTimestamp
- Related Articles
- How to get the Azure VM Size using Azure CLI in PowerShell?
- How to get the Azure VM username using Azure CLI in PowerShell?
- How to get the Azure VM available sizes using PowerShell?
- How to get the installed Azure VM extension using PowerShell?
- How to get the Azure VM DNS name using PowerShell?
- How to get the available azure VM size using Azure CLI in PowerShell?
- How to get the Azure VM disk encryption settings using PowerShell?
- How to get the Azure VM disk caching settings using PowerShell?
- How to get the Azure VM storage account type using PowerShell?
- How to get the location of the Azure VM using Azure CLI in PowerShell?
- How to get all the available azure VM images using Azure CLI in PowerShell?
- How to get the Accelerated networking status of Azure VM using PowerShell?
- How to get the Power state of the Azure VM using PowerShell?
- How to deallocate the Azure VM using Azure CLI in PowerShell?
- How to start the Azure VM using Azure CLI in PowerShell?
