Created
July 13, 2019 17:24
-
-
Save artisticcheese/89421f49819cecc153e3b33a9c388d6e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Retrieve all Public IPs | |
$AllPublicIPs = Get-AzureRMPublicIPaddress | |
# Retrieve all Virtual Networks | |
$vnetall = Get-AzureRMVirtualNetwork | |
# Retrieve all Network Interfaces | |
$nicall = Get-AzureRmNetworkInterface | |
# Retrieve all ARM Virtual Machines | |
$AllVMs = Get-AzureRMVM | Sort-object location, resourcegroupname, name | |
$StatusForAllVms = Get-azureRmVM -Status | |
#TESTComment2 | |
# Get VM sizes supported by Azure in the given RG. Call this only once. | |
if ($AllVMs -ne $null) { | |
# Retrieve all available Virtual Machine Sizes | |
Write-Verbose "5- Retrieving all available Virtual Machines Sizes..." | |
$AllVMsSize = Get-AzureRmVMSize -Location "West US" # Using West US and South Central US and East US 2 as those 2 locations are usually the ones with all and newer VM sizes | |
$AllVMsSizeSCU = Get-AzureRmVMSize -Location "South Central US" | |
foreach ($VMsSizeSCU in $AllVMsSizeSCU) { | |
if ($AllVMsSize.Name -notcontains $VMsSizeSCU.Name) { $AllVMsSize += $VMsSizeSCU } | |
} | |
$AllVMsSizeEU2s = Get-AzureRmVMSize -Location "East US 2" | |
foreach ($VMsSizeEU2 in $AllVMsSizeEU2s) { | |
if ($AllVMsSize.Name -notcontains $VMsSizeEU2.Name) { $AllVMsSize += $VMsSizeEU2 } | |
} | |
} | |
$AzureVMs = @() | |
foreach ($CurrentVM in $AllVMs) { | |
$azureVM = [PSCustomObject]@{} | |
#$azureVM | add-Member -MemberType NoteProperty -Name "Name" -Value $currentVM | |
$azureVM | Add-Member -MemberType NoteProperty -Name "VMName" -Value $CurrentVM.Name | |
$azureVM | Add-Member -MemberType NoteProperty -Name "Location" -Value $CurrentVM.Location | |
$azureVM | Add-Member -MemberType NoteProperty -Name "ResourceGroup" -Value $CurrentVM.ResourceGroupName | |
$AzureVM | Add-Member -MemberType NoteProperty -Name "OSType" -Value $CurrentVM.StorageProfile.OsDisk.OsType.ToString() | |
$azureVM | add-Member -MemberType NoteProperty -Name "PowerState" -Value $StatusForAllVms.Where{$_.Name -eq $CurrentVM.Name}.PowerState | |
$azureVM | Add-Member -MemberType NoteProperty -Name "BootDiagnostics" -Value $CurrentVM.DiagnosticsProfile.BootDiagnostics.Enabled | |
$azureVM | Add-Member -MemberType NoteProperty -Name "OSDiskSizeGB" -Value $CurrentVM.StorageProfile.OsDisk.DiskSizeGB | |
$azureVM | Add-Member -MemberType NoteProperty -Name "NumberOfDataDisks" -Value $CurrentVM.StorageProfile.DataDisks.Count | |
$azureVM | Add-Member -MemberType NoteProperty -Name "Offer" -Value $CurrentVM.StorageProfile.ImageReference.Offer | |
$azureVM | Add-Member -MemberType NoteProperty -Name "Publisher" -Value $CurrentVM.StorageProfile.ImageReference.Publisher | |
$azureVM | Add-Member -MemberType NoteProperty -Name "VMSize" -Value $CurrentVM.HardwareProfile.VmSize | |
if ($azureVM.PowerState -eq "VM running") { | |
$azureVM | Add-Member -MemberType NoteProperty -Name "DataDisksSize" -Value (($currentVM.StorageProfile.DataDisks | ForEach-Object {$_.DiskSizeGB}) -join "|") | |
} | |
#region NICs | |
$privateIPs = @() | |
$publicIPs = @() | |
$vnetName = "" | |
foreach ($currentNic in $CurrentVM.NetworkProfile.networkInterfaces) { | |
$nicinfo = $nicall.Where{$_.ID -eq $currentNic.Id} | |
$vnetName = $vnetall.where{$_.Subnets.ID -eq $nicInfo.IpConfigurations.Subnet.ID}.Name | |
$subnet = ($nicInfo.IpConfigurations.Subnet.ID -split "/")[-1] | |
$nicInfo.IpConfigurations.Foreach{ | |
$privateIPs += $_.PrivateIPAddress; | |
if ($null -ne $_.PublicIpAddress) { | |
$ipID = $_.PublicIpAddressText | convertFrom-Json | Select-Object ID | |
$publicIPs += $AllPublicIPs.Where{$_.ID -eq $ipID.ID}.IpAddress | |
} | |
} | |
} | |
#endregion | |
$azureVM | Add-Member -MemberType NoteProperty -Name "VnetName" -Value $vnetName | |
$azureVM | Add-Member -MemberType NoteProperty -Name "Subnet" -Value $subnet | |
$azureVM | Add-Member -MemberType NoteProperty -Name "privateIPs" -Value ($privateIPs -join ",") | |
$azureVM | Add-Member -MemberType NoteProperty -Name "publicIPs" -Value ($publicIPs -join ",") | |
if ($null -ne $CurrentVM.OsDisk.vhd ) {$azureVM | add-Member -MemberType NoteProperty -Name "unmanagedDisk" -Value $CurrentVM.OsDisk.vhd.Uri.Split("/")[2].Split(".")[0]} | |
if ($null -ne $CurrentVM.AvailabilitySetReference) {$azureVM | add-Member -MemberType NoteProperty -Name "AvailabilitySet" -Value $CurrentVM.AvailabilitySetReference.Id.split("/")[-1]} | |
$tags = $CurrentVM.tags | |
$tagkeys = $tags.Keys | |
if ($tagkeys -match "Name") { | |
$azureVM | Add-Member -MemberType NoteProperty -Name "NameTag" -Value $CurrentVM.tags["Name"] | |
} | |
if ($tagkeys -match "Owner") { | |
$azureVM | Add-Member -MemberType NoteProperty -Name "OwnerTag" -Value $CurrentVM.tags["Owner"] | |
} | |
if ($tagkeys -match "Environment") { | |
$azureVM | Add-Member -MemberType NoteProperty -Name "EnvironmentTag" -Value $CurrentVM.tags["Environment"] | |
} | |
if ($tagkeys -match "Function") { | |
$azureVM | Add-Member -MemberType NoteProperty -Name "FunctionTag" -Value $CurrentVM.tags["Function"] | |
} | |
$azureVM | Add-Member -MemberType NoteProperty -Name "VMCores" -Value $AllVMsSize.Where{$_.Name -eq $CurrentVM.HardwareProfile.VmSize}.NumberOfCores | |
$azureVM | Add-Member -MemberType NoteProperty -Name "VMmemory" -Value (($AllVMsSize.Where{$_.Name -eq $CurrentVM.HardwareProfile.VmSize}.MemoryInMB) / 1024) | |
$AzureVMs += $azureVM | |
} | |
$AzureVMs | ConvertTo-Json -Depth 50 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment