Skip to content

NKCODE TECH GEEK ZONE

  • RSS - Posts
Menu
  • Home
  • Cloud
    • Azure
    • Alibaba
    • AWS
  • Hardware
  • Linux
  • Network
  • Security
  • Windows Client / Servers
    • SQL
    • Windows Client OS
      • Windows 10
    • Windows Servers
      • Windows 2008R2
      • Windows Server 2012R2
      • Windows Server 2016
      • Windows Server 2019
  • VMWARE
  • Free Tools
  • About Me
    • Disclaimer
Menu

Export Azure IaaS VM properties, including NIC IP address to CSV using PowerShell

Posted on July 15, 2019

The Script below provided to export the Azure IaaS VM properties, including NIC IP address to CSV format using Powershell.

To explain this PowerShell script, it promptly works as follows:

  • viewing the script in most Windows Powershell IDEs
  • # CORE #
    • This is the default set-up of the script
    • I then define a working directory, I used C:\TEMP
    • I then define some parameters, like the date format and the file details for a transcript file
    • Lastly, the transcript is initiated
  • # SETUP #
    • This section I define the parameters to be used in the FOREACH loop
    • This is mainly the file export properties, like the filename
    • The report setup
    • Grabbing the key Azure IaaS VM instance details
  • # FOREEACH LOOP#
    • I now loop through the various info that I need from # SETUP #
    • There’s a lot more properties that can be extracted, but in this script example I’m after just a few key bits of information like resource group, hostname and IP address of each Azure IaaS VM in a particular subscription
  • # OUTPUT#
    • You can output everything on screen via just executing $Report
    • However, you can’t pipe that with an Export-CSV as you’ll potentially get System.Object[] issues in the exported file if you’ve got multiple IP addresses associated to VMs
    • Therefore, I Select-Object and the -Property the properties I want to output to a CSV
    • Note that the IpAddress uses a custom expression as some servers have multiple IP’s and I need to join them all into a single CSV cell and delineate with “;”
    • Lastly, Export-CSV is run
  • # CORE END #
    • End the transcript and echo that the script has ended in a nice GREEN background text so its visually easy to know when the script has ended
      • I know this is very trivial, but it’s a handy bit of PowerShell I copy/paste between scripts and works very well for really long scripts, or time-consuming scripts
###### CORE START #####
$WorkingDirectory = "C:\Temp\"
if ((Test-Path $WorkingDirectory) -Eq $False) {New-Item -ItemType Directory -Path $WorkingDirectory | Out-Null}
$Date = Get-Date -Format yyyy-MM-dd_HH.mm.ss
$Root = $WorkingDirectory + $Date
$Log = $LogRoot + "-Transcript.txt"
Start-transcript $Log | Out-Null
#end

    ###### SETUP #####
    $Export = $Root + "-AzureIaaS-Export.csv"
    $Report = @()
    $VMs = Get-AzVM
    $INTERFACEs = Get-AzNetworkInterface
    #end

    ###### FOREACH LOOP #####
    FOREACH($Server in $INTERFACEs)
    {
    $info = "" | Select VmName, ServerHostName, ResourceGroupName, IpAddress, OSType
    $VM = $VMs | ? -Property Id -eq $Server.VirtualMachine.id
    $info.VMName = $VM.Name
    $info.ServerHostName = $VM.OSProfile.ComputerName
    $info.ResourceGroupName = $VM.ResourceGroupName
    $info.IpAddress = $Server.IpConfigurations | Select-Object -ExpandProperty PrivateIpAddress
    $info.OSType = $VM.StorageProfile.osDisk.osType
    $report+=$info
    }
    #end

    ###### OUTPUT #####
    $Report | Select-Object -Property VMName,ServerHostName,ResourceGroupName,@{Name=’IpAddress’;Expression={[string]::join(“;”, ($_.IpAddress))}},OSType | Export-csv -Path $Export -Append -NoTypeInformation  
    #end

###### CORE END #####
Stop-Transcript
#end

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pinterest (Opens in new window) Pinterest
  • Click to share on Telegram (Opens in new window) Telegram
  • Click to share on WhatsApp (Opens in new window) WhatsApp
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

Welcome to Teck Geek Zone

Alibaba & Azure Cloud with a free trial worth $200-1200 USD Click below Cloud Providers

  • Securing Your Cloud Environment with Alibaba Cloud Firewall
  • 🚢 Sailing into the Data Age: How Cloud and IoT are Revolutionizing the Marine Industry
  • What is Azure Grafana? A Comprehensive Guide to Monitoring and Visualization
  • 🔐 How to Enable Virtualization-Based Security (VBS) for Windows Workloads in VMware Cloud Foundation and vSphere
  • Microsoft’s Azure SRE Agent: AI-Powered Reliability Engineering

Categories

  • Cloud (186)
    • Alibaba (39)
    • AWS (39)
    • Azure (114)
  • Free Tools (5)
  • Hardware (17)
  • Linux (13)
  • Network (28)
  • Security (21)
  • VMWARE (54)
  • Windows OS (44)
    • Windows 10 (7)
  • Windows Servers (69)
    • SQL (3)
    • Windows 2008R2 (7)
    • Windows Server 2012R2 (15)
    • Windows Server 2016 (20)
    • Windows Server 2019 (10)

Subscribe to our newsletter

©2025 NKCODE TECH GEEK ZONE | Design: Newspaperly WordPress Theme
%d