# Azure VM Size Information from Inside the VM
# This script retrieves VM size and metadata from within an Azure VM
# No Azure PowerShell module required - uses Azure Instance Metadata Service
# Function to get Azure VM metadata
function Get-AzureVMMetadata {
try {
# Azure Instance Metadata Service endpoint
$metadataUri = "http://169.254.169.254/metadata/instance?api-version=2021-02-01"
# Create headers for the request
$headers = @{
'Metadata' = 'true'
}
Write-Host "Retrieving Azure VM metadata..." -ForegroundColor Yellow
# Make the REST API call to get metadata
$response = Invoke-RestMethod -Uri $metadataUri -Headers $headers -Method Get -TimeoutSec 10
return $response
}
catch {
Write-Error "Failed to retrieve Azure metadata. This script must run inside an Azure VM: $($_.Exception.Message)"
return $null
}
}
# Function to display VM size and specifications
function Show-VMSizeInfo {
param(
[Parameter(Mandatory=$true)]
$Metadata
)
Write-Host "============================================================" -ForegroundColor Green
Write-Host "AZURE VM INFORMATION" -ForegroundColor Green
Write-Host "============================================================" -ForegroundColor Green
# Basic VM Information
Write-Host "VM Name: " -NoNewline -ForegroundColor Cyan
Write-Host $Metadata.compute.name -ForegroundColor White
Write-Host "VM Size: " -NoNewline -ForegroundColor Cyan
Write-Host $Metadata.compute.vmSize -ForegroundColor Yellow
Write-Host "Resource Group: " -NoNewline -ForegroundColor Cyan
Write-Host $Metadata.compute.resourceGroupName -ForegroundColor White
Write-Host "Subscription ID: " -NoNewline -ForegroundColor Cyan
Write-Host $Metadata.compute.subscriptionId -ForegroundColor White
Write-Host "Location: " -NoNewline -ForegroundColor Cyan
Write-Host $Metadata.compute.location -ForegroundColor White
Write-Host "Zone: " -NoNewline -ForegroundColor Cyan
Write-Host $(if ($Metadata.compute.zone) { $Metadata.compute.zone } else { "N/A" }) -ForegroundColor White
Write-Host "Platform Update Domain: " -NoNewline -ForegroundColor Cyan
Write-Host $Metadata.compute.platformUpdateDomain -ForegroundColor White
Write-Host "Platform Fault Domain: " -NoNewline -ForegroundColor Cyan
Write-Host $Metadata.compute.platformFaultDomain -ForegroundColor White
Write-Host ""
}
# Function to get local system specifications
function Get-LocalSystemSpecs {
Write-Host "============================================================" -ForegroundColor Green
Write-Host "LOCAL SYSTEM SPECIFICATIONS" -ForegroundColor Green
Write-Host "============================================================" -ForegroundColor Green
# Get CPU information
$cpu = Get-CimInstance -ClassName Win32_Processor | Select-Object -First 1
Write-Host "CPU: " -NoNewline -ForegroundColor Cyan
Write-Host "$($cpu.Name)" -ForegroundColor White
Write-Host "CPU Cores: " -NoNewline -ForegroundColor Cyan
Write-Host "$($cpu.NumberOfCores)" -ForegroundColor White
Write-Host "Logical Processors: " -NoNewline -ForegroundColor Cyan
Write-Host "$($cpu.NumberOfLogicalProcessors)" -ForegroundColor White
# Get memory information
$memory = Get-CimInstance -ClassName Win32_ComputerSystem
$totalMemoryGB = [math]::Round($memory.TotalPhysicalMemory / 1GB, 2)
Write-Host "Total RAM: " -NoNewline -ForegroundColor Cyan
Write-Host "$totalMemoryGB GB" -ForegroundColor White
# Get OS information
$os = Get-CimInstance -ClassName Win32_OperatingSystem
Write-Host "Operating System: " -NoNewline -ForegroundColor Cyan
Write-Host "$($os.Caption)" -ForegroundColor White
Write-Host "OS Architecture: " -NoNewline -ForegroundColor Cyan
Write-Host "$($os.OSArchitecture)" -ForegroundColor White
Write-Host ""
}
# Function to get network information
function Get-NetworkInfo {
param(
[Parameter(Mandatory=$true)]
$Metadata
)
Write-Host "============================================================" -ForegroundColor Green
Write-Host "NETWORK INFORMATION" -ForegroundColor Green
Write-Host "============================================================" -ForegroundColor Green
# Network interfaces from metadata
if ($Metadata.network -and $Metadata.network.interface) {
foreach ($interface in $Metadata.network.interface) {
Write-Host "Network Interface:" -ForegroundColor Cyan
Write-Host " MAC Address: $($interface.macAddress)" -ForegroundColor White
if ($interface.ipv4 -and $interface.ipv4.ipAddress) {
foreach ($ipConfig in $interface.ipv4.ipAddress) {
Write-Host " Private IP: $($ipConfig.privateIpAddress)" -ForegroundColor White
Write-Host " Public IP: $($ipConfig.publicIpAddress)" -ForegroundColor White
}
}
if ($interface.ipv4 -and $interface.ipv4.subnet) {
foreach ($subnet in $interface.ipv4.subnet) {
Write-Host " Subnet: $($subnet.address)/$($subnet.prefix)" -ForegroundColor White
}
}
}
}
Write-Host ""
}
# Function to get storage information
function Get-StorageInfo {
Write-Host "============================================================" -ForegroundColor Green
Write-Host "STORAGE INFORMATION" -ForegroundColor Green
Write-Host "============================================================" -ForegroundColor Green
# Get disk information
$disks = Get-CimInstance -ClassName Win32_LogicalDisk | Where-Object { $_.DriveType -eq 3 }
foreach ($disk in $disks) {
$sizeGB = [math]::Round($disk.Size / 1GB, 2)
$freeSpaceGB = [math]::Round($disk.FreeSpace / 1GB, 2)
$usedSpaceGB = $sizeGB - $freeSpaceGB
$percentUsed = [math]::Round(($usedSpaceGB / $sizeGB) * 100, 1)
Write-Host "Drive $($disk.DeviceID)" -ForegroundColor Cyan
Write-Host " Total Size: $sizeGB GB" -ForegroundColor White
Write-Host " Used Space: $usedSpaceGB GB ($percentUsed%)" -ForegroundColor White
Write-Host " Free Space: $freeSpaceGB GB" -ForegroundColor White
Write-Host " File System: $($disk.FileSystem)" -ForegroundColor White
Write-Host ""
}
}
# Main execution
try {
Write-Host "Azure VM Size and Information Checker" -ForegroundColor Magenta
Write-Host "Running from inside Azure VM..." -ForegroundColor Magenta
Write-Host ""
# Get Azure VM metadata
$metadata = Get-AzureVMMetadata
if ($metadata) {
# Display VM information
Show-VMSizeInfo -Metadata $metadata
# Display local system specs
Get-LocalSystemSpecs
# Display network information
Get-NetworkInfo -Metadata $metadata
# Display storage information
Get-StorageInfo
}
else {
Write-Error "Unable to retrieve Azure VM metadata. Ensure this script is running inside an Azure VM."
}
}
catch {
Write-Error "An error occurred: $($_.Exception.Message)"
}
Write-Host ""
Write-Host "Script completed." -ForegroundColor Green
Comments
Post a Comment