question

RisingFlight-7863 avatar image
0 Votes"
RisingFlight-7863 asked PranathiPanyam-MSFT commented

Export VM information

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
windows-server-powershellazure-virtual-machinesazure-sql-virtual-machines
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered PranathiPanyam-MSFT commented

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.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@RisingFlight-7863,

Just checking in to see if the above answer helped. If this answers your query, do click “Mark as Answer” and Up-Vote for the same, which might be beneficial to other community members reading this thread. And, if you have any further query do let us know.

0 Votes 0 ·