Originally posted on Lucian.Blog. Follow Lucian on Twitter.

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:

  • I used regions to keep sections together, which comes in handy when I’m viewing the script in most 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

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

#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

Originally posted on Lucian.Blog. Follow Lucian on Twitter.

Category:
Azure Infrastructure, PowerShell
Tags:
, , ,

Join the conversation! 1 Comment

  1. An alternative : use the CLI
    az vm list -d

Comments are closed.