0

Hi I'm trying to get disk information alongside network information for azure virtual machines.

I'm probably doing something silly, but I thought it would be a case of adding the vm into the network foreach loop.

I've put the script below, can anyone see where I'm going wrong?

$reportName = "sample.csv"
$report = @()
$vms = Get-AzVM

$publicIps = Get-AzPublicIpAddress 
$nics = Get-AzNetworkInterface | ?{ $_.VirtualMachine -NE $null} 
foreach ($nic in $nics) { 
    $info = "" | Select VmName, ResourceGroupName, Region, VmSize, VirturalNetwork, Subnet, DnsServers, NicDns, PrivateIpAddress, OsType, OSDisk, DiagDisk, DataDisk, PublicIPAddress, vCPU, Memory
    $vm = $vms | ? -Property Id -eq $nic.VirtualMachine.id 
    foreach($publicIp in $publicIps) { 
        if($nic.IpConfigurations.id -eq $publicIp.ipconfiguration.Id) {
            $info.PublicIPAddress = $publicIp.ipaddress
            } 
        } 

        #$location = $vm.location
        $info.OsType = $vm.StorageProfile.OsDisk.OsType 
        $info.VMName = $vm.Name 
        $info.ResourceGroupName = $vm.ResourceGroupName 
        $info.Region = $vm.Location 
        $info.VmSize = $vm.HardwareProfile.VmSize
        $size = $info.VmSize = $vm.HardwareProfile.VmSize
        $info.vCPU = (Get-AzVMSize -Location $location | ? {$_.name -eq $size}).NumberOfCores
        $info.Memory = (Get-AzVMSize -Location $location | ? {$_.name -eq $size}).MemoryInMB
        $info.VirturalNetwork = $nic.IpConfigurations.subnet.Id.Split("/")[-3] 
        $info.Subnet = $nic.IpConfigurations.subnet.Id.Split("/")[-1] 
        $info.PrivateIpAddress = $nic.IpConfigurations.PrivateIpAddress 
        $vm = get-azvm | ? {$_.name -eq $vm}
        foreach ($vms in $vm) {
        $info.OSDiskSizeGB = $vm.StorageProfile.OsDisk.DiskSizeGB
        $info.OSDisk = $vm.StorageProfile.OsDisk.Vhd.uri
        $info.DiagDisk = $vm.DiagnosticsProfile.BootDiagnostics.StorageUri
        $info.DataDisk = $vm.StorageProfile.DataDisks.vhd.uri -join "**"
        }

        $report+=$info 
    } 
$report | ft VmName, ResourceGroupName, Region, VmSize, VirturalNetwork, Subnet, DnsServers, NicDns, PrivateIpAddress, OsType, OSDisk, DiagDisk, DataDisk, PublicIPAddress, vCPU, Memory 
$report | Export-CSV "c:\temp\$reportName"

Thanks in advance :)

EDIT:

This is the output I get, no disk infor for any VM

VmName    ResourceGroupName Region     VmSize         VirturalNetwork Subnet  DnsServers NicDns PrivateIpAddress  OsType OSDisk DiagDisk DataDisk PublicIPAddress vCPU Memory
------    ----------------- ------     ------         --------------- ------  ---------- ------ ----------------  ------ ------ -------- -------- --------------- ---- ------
vmr1-sec1 DEPLOYRG1         westeurope Standard_D2_v4 av-vnet         av-sub2                   172.0.2.4        Windows                                             2   8192
vmr2-sec1 DEPLOYRG1         westeurope Standard_D2_v4 av-vnet         av-sub2                   172.0.2.5        Windows                                             2   8192
cks-cp1   K8S               westeurope Standard_B2s   K8S-vnet        default                   10.0.0.4           Linux                          20.71.120.71       2   4096
Nick.Mc
  • 18,304
  • 6
  • 61
  • 91
Norrin Rad
  • 881
  • 2
  • 18
  • 42

1 Answers1

0

At the moment, Azure uses Azure managed disk in default. Azure managed disks are no longer stored into blob storage by default. For more details, please refer to here and here. if you want to get the vhd URL of Azure managed disk, you need to run the command Grant-AzDiskAccess to get it. But please note that the URL has a life cycle and will expire after a period of time

So I suggest you store your disk name or disk id in your CSV file

Jim Xu
  • 21,610
  • 2
  • 19
  • 39