- 1 year ago
Get Azure VM size inside on a windows machine
To retrieve the Azure VM size from within a Windows machine, you can use the Azure Instance Metadata service. The Instance Metadata service provides information about the running Azure VM, including its size.
Here's an example of how you can fetch the VM size using PowerShell:
powershell# Invoke the Instance Metadata service to get the VM size
$vmSize = Invoke-RestMethod -Uri "http://169.254.169.254/metadata/instance/compute/vmSize?api-version=2021-01-01&format=text" -Headers @{"Metadata"="true"}
# Print the VM size
Write-Output "Azure VM Size: $vmSize"
The script above uses Invoke-RestMethod
to make a GET request to the Instance Metadata service at the URL http://169.254.169.254/metadata/instance/compute/vmSize?api-version=2021-01-01&format=text
. The response will contain the VM size, which is then stored in the $vmSize
variable. Finally, the script outputs the VM size to the console.
By executing this PowerShell script on an Azure VM, you should be able to retrieve the VM size dynamically.
Please note that the Instance Metadata service is only available within Azure VMs and may not work on non-Azure environments or VMs running outside of Azure.