Skip to content

Instantly share code, notes, and snippets.

@dabedin
Created July 5, 2018 16:17
Show Gist options
  • Save dabedin/88e24f55e7f0d4718a987597ac634ce2 to your computer and use it in GitHub Desktop.
Save dabedin/88e24f55e7f0d4718a987597ac634ce2 to your computer and use it in GitHub Desktop.
A simple (and highly improvable) PS script to retrieve VMs informations.
function Login
{
$Conn = Get-AutomationConnection -Name AzureRunAsConnection
$output = Add-AzureRmAccount -ServicePrincipal -Tenant $Conn.TenantID -ApplicationID $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint
}
function FlattenTags ($tags) {
if ($tags -ne $NULL) {
$result = ($tags.GetEnumerator() | ForEach-Object { "$($_.Key)=$($_.Value)" }) -join ';'
}
else {
$result = ""
}
return $result
}
#Login in Automation
Login
$Subscriptions = Get-AzureRmSubscription | ForEach-Object {"$($_.Name)"}
$allResources = @()
# Custom class to store the data (needs powershell 5)
Class AzureResource
{
[string]$Subscription
[string]$ResourceType
[string]$ResourceId
[string]$ResourceGroup
[string]$Location
[string]$Name
[string]$OSType
[string]$VNet
[string]$Subnet
[string]$Size
[string]$vCPU
[string]$RAM
[string]$Status
[string]$Tags
}
ForEach ($Subscription in $Subscriptions)
{
$output = Select-AzureRmSubscription -SubscriptionName $Subscription
Write-Verbose "Processing data for subscription $Subscription"
$armVMs = $null
$armVMs = Get-AzureRmVM
foreach($armVM in $armVMs)
{
# ARM VM
Write-Verbose "Processing data for ARM VM $($armVM.Name)"
$azureResource = New-Object AzureResource
$azureResource.Subscription = $Subscription
$azureResource.OSType = $armVM.StorageProfile.OsDisk.OsType
$azureResource.Location = $armVM.Location
$azureResource.ResourceId = $armVM.Id
$azureResource.ResourceGroup = $armVM.ResourceGroupName
$azureResource.Name = $armVM.Name
$sizeinfo = $null
$sizeinfo = Get-AzureRmVMSize -Location $azureResource.Location | where-object {$_.name -eq "$($armVM.hardwareprofile.vmsize)"}
$azureResource.Size = $sizeinfo.Name
$azureResource.vCPU = $sizeinfo.NumberOfCores
$azureResource.RAM = $sizeinfo.MemoryInMB
$vmstatus = ""
$vmstatus = Get-AzurermVM -Name $armVM.Name -ResourceGroupName $armVM.ResourceGroupName -Status
$azureResource.Status = (get-culture).TextInfo.ToTitleCase(($vmstatus.statuses)[1].code.split("/")[1])
$nic = Get-AzureRmNetworkInterface -Name ($armVM.NetworkProfile.NetworkInterfaces[0].Id -split '/')[-1] -ResourceGroupName ($armVM.NetworkProfile.NetworkInterfaces[0].Id -split '/')[-5]
$IpConfig = ConvertFrom-Json -InputObject $nic.IpConfigurationsText
$subnet = $IpConfig.Subnet.Id
$azureResource.VNet = ($subnet -split '/')[-3]
$azureResource.Subnet = ($subnet -split '/')[-1]
$azureResource.Tags = FlattenTags($armVM.Tags)
$allResources += $azureResource | Select-Object `
Subscription, `
Name, `
Size, `
vCPU, `
RAM, `
OSType, `
Status, `
Location, `
VNet, `
Subnet, `
ResourceGroup, `
ResourceId, `
Tags
}
}
# output the data to the csv file
$allResources = $allResources | Sort-Object Subscription, ResourceGroup
Write-Output ($allResources | ConvertTo-Json)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment