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