Export VM information

Rising Flight 6,276 Reputation points
2021-06-06T19:06:48.383+00:00

Hi All

i have 2 subscriptions and i want to export all the VMS information to csv file.
i want to export the below information to a csv file.
Subscription
Name
Location
Status
Resource Group
VMType
Core
Mem (MB)
OS
IP
Zone
Created

Expert can anyone validate if the below script works as i don't want to test in production tenant and i don't have a any test tenant.

$report = @()
$subs = Get-AzSubscription
Foreach ($sub in $subs)
{
select-AzSubscription $sub | Out-Null
$subName = $sub.Name
$vms = Get-AzVM
$publicIps = Get-AzPublicIpAddress 
$nics = Get-AzNetworkInterface | ?{ $_.VirtualMachine -NE $null} 
foreach ($nic in $nics) { 
$info = "" | Select VmName, ResourceGroupName, Region, VmSize, VirtualNetwork, PrivateIpAddress, OsType, PublicIPAddress, Subscription, Cores, Memory, CreatedDate
$vm = $vms | ? -Property Id -eq $nic.VirtualMachine.id 
foreach($publicIp in $publicIps) { 
if($nic.IpConfigurations.id -eq $publicIp.ipconfiguration.Id) {
$info.PublicIPAddress = $publicIp.ipaddress
} 
} 
[string]$sku = $vm.StorageProfile.ImageReference.Sku
[string]$os = $vm.StorageProfile.ImageReference.Offer
$osDiskName = $vm.StorageProfile.OsDisk.Name
$info.VMName = $vm.Name 
$info.OsType = $os + " " + $sku
$info.ResourceGroupName = $vm.ResourceGroupName 
$info.Region = $vm.Location
 $vmLocation = $vm.location 
$info.VmSize = $vm.HardwareProfile.VmSize
$info.VirtualNetwork = $nic.IpConfigurations.subnet.Id.Split("/")[-3] 
$info.PrivateIpAddress = $nic.IpConfigurations.PrivateIpAddress 
$info.Subscription = $subName
if ($vmLocation)
{
$sizeDetails = Get-AzVMSize -Location $vmLocation | where {$_.Name -eq $vm.HardwareProfile.VmSize}
}
$info.Cores = $sizeDetails.NumberOfCores
$info.Memory = $sizeDetails.MemoryInMB
$osDisk = Get-AzDisk -ResourceGroupName $vm.ResourceGroupName -DiskName $osDiskName
$info.CreatedDate = $osDisk.TimeCreated
$report+=$info
} 
}
$report | ft VmName, ResourceGroupName, Region, VmSize, VirtualNetwork, PrivateIpAddress, OsType, PublicIPAddress, Subscription, Cores, Memory, CreatedDate| export-csv -path "c:\temp\output.csv" -Notypeinformation
SQL Server on Azure Virtual Machines
Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Answer accepted by question author
  1. Anonymous
    2021-06-07T01:59:36.26+00:00

    Hi,

    The formatted output of the ft cmdlet cannot be exported to a CSV file. You may use Select-Object to specify the properties to be exported.

    $report | Select-Object VmName, ResourceGroupName, Region, VmSize, VirtualNetwork, PrivateIpAddress, OsType, PublicIPAddress, Subscription, Cores, Memory, CreatedDate| export-csv -path "c:\temp\output.csv" -Notypeinformation  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.