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

#PromptPowerShell

The other day I needed to export some data from Azure. I needed an output of all the IaaS VM instances high level configuration for a customer. Namely I needed the resource group, the hostname and the IP address of the instances to forward across for some cross reference analysis.

Now, I’ve had the unfortunate mishap of losing my PowerShell script repo during the change over / migration from my Macbook to my current Surface Pro. As such, I’m moving everything into Github and this is a good use case to not only do some housekeeping, but also share some info at the same time.

Below is the end result of the script I used. I came to this final version after testing out some PowerShell of my own and doing a bit of research online. In this final version, I also added part of my default transcript # CORE # functionality that I use a lot to cover my backside in the event of any issues or faults. If you want to grab the script from my Github public repo, you can find that shared here (again on Github) for you to either download or fork as you see fit.

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

It is assumed you have the Azure Az module installed and imported; Import-Module Az. For more info on this new Az module, that replaces AzureRM, check out the following docs.microsoft.com page.

Sample PowerShell script

Again, you can find the bellow script available in my ‘public-AZURE’ GitHub repo available here >

#region ##### 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
#endregion

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

    #region ##### 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
    }
    #endregion

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

#region ##### CORE END #####
Stop-Transcript
#endregion

Questions?

Have a question about this post? Ask away on Twitter or in my AMA repo.