1
votes

Good Day

I have been tasked to gather a list of all the VMs in our different subscriptions, and I have a script that gathers: Mode, Name, ResourceGroupName, location, VMSize, and Status. There are two empty columns: subscription and availability set.

A bit of a backstory, the company I am contracted to hired a vendor to deploy the resources and now they don't know if the vendor did what they were suppose to.

What I am trying to figure out:

  1. How to list the ASG the resource is in
  2. The NSG the resource is in
  3. The vCPUs and RAM
  4. Storage account

I have been scouring google to find the PSObjects I need to define. Any assistance would be greatly appreciated

1
Hi Ryan, I have written a PowerShell script to generate an Azure Inventory, let me know if it helps - manjunathrao.com/2016/12/30/powershell-generate-azure-inventoryManjunath Rao

1 Answers

0
votes

1.How to list the ASG the resource is in

If you want to list all the ASGs in the VM, you need to list them via all the NICs, you could refer to the command below, the $asgnames is the list of all ASGs, note if different NICs have the same ASG, it will output several times.

$nics = (Get-AzureRmVM -ResourceGroupName "<ResourceGroupName>" -Name "<VM Name>").NetworkProfile.NetworkInterfaces
$nicnames = @()
$asgnames = @()
foreach($nic in $nics){
    $a = $nic.Id -split"/"
    $nicname = $a[8]
    $nicnames += $nicname
    $ipconfig = Get-AzureRmResource -ResourceGroupName <ResourceGroupName> -ResourceType Microsoft.Network/networkInterfaces/ipConfigurations -ResourceName "$nicname/ipconfig1" -ApiVersion 2018-07-01
    $asgids = $ipconfig.Properties.applicationSecurityGroups.id 

    foreach ($asgid in $asgids){
        $a = $asgid -split"/"
        $asganme = $a[8]
        $asgnames += $asganme
    }
}

enter image description here

2.The NSG the resource is in

If you want to get NSGs in the VM, you need to get them via all NICs and VNET Subnet.

Get the NSG of the NIC, the $nsgnic is the NSG:

$nic = Get-AzureRmResource -ResourceGroupName <ResourceGroupName> -ResourceType Microsoft.Network/networkInterfaces -ResourceName "<NIC name>" -ApiVersion 2018-07-01
$a = $nic.properties.networkSecurityGroup.id -split"/"
$nsgnic = $a[8]

Get the NSG of subnet, the $nsgsub is the NSG:

$subnet = Get-AzureRmResource -ResourceGroupName <ResourceGroupName> -ResourceType Microsoft.Network/virtualNetworks/subnets -ResourceName "<Vnet name>/<Subnet name>" -ApiVersion 2018-07-01
$b = $subnet.properties.networkSecurityGroup.id -split"/"
$nsgsub = $a[8]

Then the NSGs of the VM will be the sum of $nsgnic and $nsgsub.

3.The vCPUs and RAM

Try the command below, NumberOfCores is the vCPUs , MemoryInMB is the RAM, note the RAM in result is in MB, you could convert it to GB if necessary.

Get-AzureRmVMSize -ResourceGroupName "<ResourceGroupName>" -VMName "<VMName>" 

enter image description here

4.Storage account

If I do not misunderstand, the storage account here you mean the Diagnostics storage account, try the command below, $storage is the storage account uri.

$a = Get-AzureRmResource -ResourceGroupName <ResourceGroupName> -ResourceType Microsoft.Compute/virtualMachines -ResourceName "<VM Name>" -ApiVersion 2018-06-01
$storage = $a.properties.diagnosticsProfile.bootDiagnostics.storageUri

enter image description here