Chendrayan Venkatesan

2 minute read

Introduction

I got stumbled with an issue, “Failed SNAT connections detected”. Luck favored! Yes, it’s a very minimal impact on the environment. However, I took remediation action as earliest as possible. The functions I have developed invokes Azure management API for a business reason. So, I ended up with the error due to the outbound connection. Microsoft recommends below

For C# applications, ensure that the HttpClient object is created once and reused again. For ADO.NET connections, make sure database connection pooling is used. Restart the web app to reset this limit. However, it is recommended to pursue a more long-term and stable fix by following connection pooling best practices.

I have to replace Invoke-RestMethod and Invoke-WebRequest cmdlet from the code. Wait! What? Yes, the answer is here. Now, the code using HTTPClient is as shown below. (PowerShell Core 6 or above)

#region - Virtual Machine List
[System.Collections.ArrayList]$VirtualMachineList = @()
$Client = [System.Net.Http.HttpClient]::new()
$Client.DefaultRequestHeaders.Authorization = [System.Net.Http.Headers.AuthenticationHeaderValue]::new("Bearer" , $AccessToken)
[System.Collections.ArrayList]$virtualMachineList = @()
$Uri = "https://management.azure.com/subscriptions/{SUBSCRIPTIONID}/providers/Microsoft.Compute/virtualMachines?api-version=2019-03-01"
do {
    $Request = [System.Net.Http.HttpRequestMessage]::new("Get", $Uri)
    $Response = $Client.SendAsync($Request).GetAwaiter().GetResult()
    if (($Response.Content.ReadAsStringAsync().Result | ConvertFrom-Json).value) {
        $VirtualMachineList.AddRange(($Response.Content.ReadAsStringAsync().Result | ConvertFrom-Json).value)
    }
    $Uri = ($Response.Content.ReadAsStringAsync().Result | ConvertFrom-Json).nextLink
} until ($null -eq $Uri)
#endregion

The preceding code is to retrieve all VM’s from the given subscription. Oh well, we can use the Resource Graph Query to list it out. But, consider using it if the table is not available in the graph query. Example: Diagnostic Settings.

#region - Virtual Machine List
(Measure-Command -Expression {
    [System.Collections.ArrayList]$VirtualMachineList = @()
    $Client = [System.Net.Http.HttpClient]::new()
    $Client.DefaultRequestHeaders.Authorization = [System.Net.Http.Headers.AuthenticationHeaderValue]::new("Bearer" , $AccessToken)
    [System.Collections.ArrayList]$virtualMachineList = @()
    $Uri = "https://management.azure.com/subscriptions/{SUBSCRIPTIONID}/providers/Microsoft.Compute/virtualMachines?api-version=2019-03-01"
    do {
        $Request = [System.Net.Http.HttpRequestMessage]::new("Get",$Uri)
        $Response = $Client.SendAsync($Request).GetAwaiter().GetResult()
        if(($Response.Content.ReadAsStringAsync().Result | ConvertFrom-Json).value) {
            $VirtualMachineList.AddRange(($Response.Content.ReadAsStringAsync().Result | ConvertFrom-Json).value)
        }
        $Uri = ($Response.Content.ReadAsStringAsync().Result | ConvertFrom-Json).nextLink
    } until ($null -eq $Uri)
})
#endregion
Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 9
Milliseconds      : 135
Ticks             : 91355065
TotalDays         : 0.000105735028935185
TotalHours        : 0.00253764069444444
TotalMinutes      : 0.152258441666667
TotalSeconds      : 9.1355065
TotalMilliseconds : 9135.5065    

9 seconds to pull 948 VM

Invoke-RestMethod and it’s performance in PowerShell Core (Azure Functions with Windows Stack)

#region - Subscription
(Measure-Command -Expression {
    [System.Collections.ArrayList]$virtualMachineList = @()
    $Uri = "https://management.azure.com/subscriptions/{SUBSCRIPTIONID}/providers/Microsoft.Compute/virtualMachines?api-version=2020-12-01"
    do {
        $virtualMachines = Invoke-RestMethod -Method Get -Uri $($uri) -Headers @{
            Authorization = "Bearer {0}" -f ($AccessToken)
        } -Verbose
        $virtualMachineList.AddRange($virtualMachines.value)
        $uri = $virtualMachines.nextLink
    } until ($null -eq $Uri)
})
#endregion - Subscription
Days              : 0
Hours             : 0
Minutes           : 2
Seconds           : 11
Milliseconds      : 135
Ticks             : 9134565
TotalDays         : 0.000105735028935185
TotalHours        : 0.00253764069444444
TotalMinutes      : 0.152258441666667
TotalSeconds      : 9.1355065
TotalMilliseconds : 9135.5065    
comments powered by Disqus