Get Virtual Machine ScaleSet private IPs
Sorry for not posting stuff recently i’m really busy working with customers on really big project.In this post i’ll show you some tools i built in order to help myself in my daily work.
We’re working on a project that use dozens of virtual machine scalesets and each time they scale new ip are assigned which is good because the scalability is key on our work, but when you want to troubleshoot problems sometimes you just don’t want to shoot a VM and go deep inside the VM to get it work again, we’re not talking about containers here. And in order to get access to your Instances (VM) in a Scaleset you have to find the private IP that is used.
This stuff is pretty hard to get, no information on the portail and obviously no easy information in PowerShell…
The solution is to use Get-AzureRmResource to trigger the ARM APIs in order to get everything we need which isn’t really easy to use… So, i built this little PowerShell script.
Example 1: You want to list all Instance PrivateIP in a Resource Group in a specific subscription
.\Get-AzureVMSSPrivateIp.ps1 -SubscriptionID <subscriptionId> -ResourceGroupName <RGName>
Example 2: You want to list all instances in a specific ScaleSet
.\Get-AzureVMSSPrivateIp.ps1 -SubscriptionID <subscriptionId> -ResourceGroupName <RGName> -VMSSName <VMSS>
Below the code used in this script, hope you’ll find it usefull.
param ( [ValidatePattern('(^([0-9A-Fa-f]{8}[-][0-9A-Fa-f]{4}[-][0-9A-Fa-f]{4}[-][0-9A-Fa-f]{4}[-][0-9A-Fa-f]{12})$)')] [String]$SubscriptionID, [String]$VMSSName, [Parameter(Mandatory=$true)] [String]$ResourceGroupName ) # First let's connect to your tenant try { $AvoidOutput = Login-AzureRmAccount -ErrorAction SilentlyContinue } Catch { Throw "Can't log into your tenant. Error: $($_.Exception.Message)" } if ($SubscriptionID) { # Check if Subscription exists and is enabled if ((Get-AzureRmSubscription -SubscriptionId $SubscriptionID).State -eq "Enabled") { Select-AzureRmSubscription -SubscriptionId $SubscriptionID | Out-Null } else { Throw "Subscription with id $($SubscriptionID) doesn't exist in this tenant" } } # Test if ResourceGroup exists $ResourceGroup = Get-AzureRmResourceGroup -Name $ResourceGroupName -ErrorAction SilentlyContinue # if RG exists go to next step, check is the VMSS exists if ($ResourceGroup) { if ($VMSSName) { try { # If VM Scaleset exists get VMs informations $VMScaleset = Get-AzureRmVmss -ResourceGroupName $ResourceGroupName -VMScaleSetName $VMSSName -ErrorAction SilentlyContinue } catch { Throw "VM ScaleSet: $VMSSName doesn't exist in $SubscriptionID" } } else { $VMScaleset = Get-AzureRmVmss -ResourceGroupName $ResourceGroupName -ErrorAction SilentlyContinue if (!($VMScaleset)) { Throw "No VM ScaleSet in $SubscriptionID" } } $Output = @() foreach ($ScaleSet in $VMScaleset) { Try { # Get All VM informations Get-AzureRmVmssVM -ResourceGroupName $ResourceGroupName -VMScaleSetName $($ScaleSet.Name) -ErrorAction SilentlyContinue | ForEach-Object { $NetworkCard = ($_[0].NetworkProfile.NetworkInterfaces[0].Id).Split('/')[-1] $Resource = $($ScaleSet.Name) + "/" + $_.InstanceId + "/" + $NetworkCard $Output += [PSCustomObject]@{ VMScaleSet = $($ScaleSet.Name) VMName = $_.Name PrivateIPAddress = (Get-AzureRmResource -ResourceGroupName $ResourceGroupName -ResourceType Microsoft.Compute/virtualMachineScaleSets/virtualMachines/networkInterfaces -ResourceName $Resource -ApiVersion 2017-03-30).Properties.ipConfigurations[0].properties.privateIPAddress } } } Catch { Throw "Error: $($_.Exception.Message)" } } $Output | Format-Table } else { Write-Error "RG: $ResourceGroupName doesn't exist in $SubscriptionID" }