Finding the last date of the vm using azure cli command - azure

I have an account with Azure with different Resource Group and different virtual machine. I would like to know how I can determine which ones are unused. For example check the last date where the virtual machine was started or used by the user using azure cli command.
Please help me out with this...

Easiest would probably to look at the powerstate of a vm.
First list all the vm's and then run a query where you filter out those that are deallocated and belong to a specific resource group:
az vm list -d --query '[?powerState == `VM deallocated` && resourceGroup==`resource_group`]'
For more information on the queries look up 'JMESPath-query' on the Microsoft docs page. Hopefully this helps.

I would like to know how I can determine which ones are unused
Currently, there is no way to do that using cli.
check the last date where the virtual machine was started or used
We can get this information using PowerShell. which follows.
Get data information of deallocated VM using Get-AzVM -VMName xxxx - RgName xxx -Status
# To retrieve the date of VM was Deallocated.
$vmDeallocatedDate = Get-AzVM -VMName <Your VM name> -ResourceGroupName <Your ResourceGroup Name> -Status
$vmDeallocatedDate.Statuses[0].Time
List all the VM's and the timestamp of the action that triggered the Deallocation
Get-AzLog -Status Accepted -DetailedOutput | ?{$_.Authorization.Action -eq "Microsoft.Compute/virtualMachines/deallocate/action"} | fl ResourceId,EventTimestamp
References
Get-AzLog to get VM Deallocate status
Get-AzVM -status to get VM Deallocate status

Related

Cannot check the size of virtual machines in one region

I am trying to check the availability size of virtual machines in one region.
The command: Get-AzVMSize -ResourceGroupName RG3 -AvailabilitySetName availabilitysets
However, it appears this error :-
Get-AzVMSize is not recognized as an internal and external command.
Kindly please help. Thanks
We have tried to Getvmsize using below steps:
Make sure to Connected with Azure using Connect-AzAccount.
Then run the following command to list the vm sizes using availability sets name
Get-AzVMSize -ResourceGroupName "myrgname" -AvailabilitySetName "myavsetname"
&
Get-AzVMSize -ResourceGroupName "myrgname" -VMName "myvmname"
OUTPUT SCREENSHOT FOR REFERENCE:-
For more information please refer this MICROSOFT DOCUMENTATION|Get-AzVMSize

List available VM SKUs for the azure policy "Allowed virtual machine SKUs"

$definition = Get-AzPolicyDefinition | Where-Object {$_.Properties.DisplayName -eq "Allowed virtual machine SKUs"}
New-AzPolicyAssignment -Name 'Test' -DisplayName 'Test' -Scope $ResourceGroup.ResourceID -PolicyDefinition $definition
Running this command will ask for 'listOfAllowedSKUs' for which SKUs i want to allow to be created in the specified resource group. I can find a list of them by going into the azure portal, finding policy and clicking assign policy, then selecting "Allowed virtual machine skus" and then going into parameters and looking at the names.
I can type in those names manually one by one when i get asked for it after running the command, but instead of opening the azure portal every time, i want to be able to list the available SKUs in powershell instead. I can't find anything online about getting a list of available SKUs in the console.
Is is possible to get a list of available SKUs to create?
Hello and welcome to Stack Overflow!
If you want to get the SKUs corresponding to a specific Location, Publisher and Offer, you may use the Get-AzVMImageSku cmdlet to get the VMImage SKUs by providing the Location, PublisherName and Offer as input parameters, and then construct the AllowedSkus object. For example:
$sku = Get-AzVMImageSku -Location "Central US" -PublisherName "Fabrikam" -Offer "LinuxServer"
$AllowedSkus = #{'listOfAllowedSKUs'=($sku.Skus)}
Else, to mimic the list of available SKUs as appearing in the portal, you may use the Get-AzVMSize cmdlet to get the available virtual machine sizes as follows:
$allsizes = Get-AzLocation | Get-AzVmSize | Select-Object -ExpandProperty Name | Sort-Object | Get-Unique
$AllowedSkus = #{'listOfAllowedSKUs'=($allsizes)}
This can finally be passed to the New-AzPolicyAssignment cmdlet using the -PolicyParameterObject option:
New-AzPolicyAssignment -Name '<policy assignment name>' -DisplayName '<display name>' -Scope $ResourceGroup.ResourceID -PolicyDefinition $definition -PolicyParameterObject $AllowedSkus
Other examples here. Hope this helps.
According to my test, if you want to know the available virtual machine SKUs in the subscription, you can use the following PowerShell command
Get-AzComputeResourceSku | where{$_.ResourceType.Equals('virtualMachines')}| fl
Its outputs contain parameter Restrictions. The parameter will tell us which SKU cannot be used.
For example
Giving one type of restriction : Zone Type for size Standard_A0 which means that under a particular subscription, Standard_A0 is not available to be created in West Europe region if placed in Availability Zones
Giving no Restrictions at the end, which means that VM Size : Standard_F8s can be created under this particular subscription in this region : West Europe without any restrcitions ( i.e : it can be created with Availaibility Zone and without Availaibility zone as well )
Giving two types of restrictions : Zone Type and Location Type, which means that VM Size of Standard_DS2_V2_Promo cannot be created in this particular subscription in this region : WestEurope at all.

How to delete all the public ip addresses in a Resource group in Azure

I want to delete all the public ip addresses in a resource group in Azure using single command rather than doing it one by one
You can use following Powershell scripts to achieve your requirement:
Get-AzureRmPublicIpAddress -ResourceGroupName <yourresourcegroupname> | Remove-AzureRmPublicIpAddress
Also, you can use the command Force to run without asking for user confirmation:
Get-AzureRmPublicIpAddress -ResourceGroupName <yourresourcegroupname> | Remove-AzureRmPublicIpAddress -Force
I test in my lab and succeed.

Start / Shutdown specific VMs in Azure through an input file

Need to shutdown specific VMs in my Azure subscription. Is there a way, I can make the ‘Get-AzureVM’ PowerShell cmdlet to read from an input file? We have too many VMs in our subscription. So specifying individual machine by name is tedious in our case.
Please advice.
I am assuming in the answer below that you are using ASM Azure portal, i.e. the older portal as you are using the ASM cmdlets.
If you want to start all VMs in your subscription then you can use:
Get-AzureVM | Start-AzureVM
If you want to start only specific VMs then you can use below script:
#Declaring the Dictionary object to store VM Names along with their Service names
$VMList = #{}
#Adding VMs to the VM List
#You can replace this section with reading from file and then populating your dictionary object
#Replace the VM Name and Service name with your environment data
$VMList.Add("VMName01", "ServiceName")
$VMList.Add("VMName02","ServiceName")
#Getting the VM object using Get-AzureVM and then starting the VMs
$VMList.Keys | % {Get-AzureVM -Name $_ -ServiceName $VMList.Item($_)} | Start-AzureVM

How to start a azure deallocated vm?

I needed to do a vm backup and I followed this article:
https://azure.microsoft.com/en-us/documentation/articles/virtual-machines-linux-capture-image/
So, I executed:
azure vm shutdown vm-1
But now I really need to start the deallocated vm but I don't know how to do it.
When I try to execute this command:
Start-AzureVM -ServiceName "vm-1" -Name "vm-1"
I'm getting this message:
No deployment found in service: 'vm-1'.
And when I try to list all my vm, I don't see vm-1
Any idea of how to start a deallocated vm?
Thanks
Understood your problem. Your VM has been deleted by you and you want it back. Now in order to back your vm you need to make sure you have the vhds of the vm in place.
a. Find out the vhd and convert it to disk(OS Disk and data disk).
b. Use the OS disk's diskname to create a new vm using this powershell-
Set-AzureSubscription -SubscriptionId "xyz" -CurrentStorageAccountName "lmn"
$vm=New-AzureVMConfig -DiskName "OSDiskDiskName" -InstanceSize "InstanceSizeofvm" -Name "VMName"
New-AzureVM -Location "LocationName" -ServiceName "abc" -VNetName "vNetName" -VM $vm -verbose
c. Find out the data disk and attach to the newly created VM.
In case you have deleted the vhds as well, raise a ticket with MS, only they can help you in that case.

Resources