Call Invoke-RestMethod from Azure Function results in Unknown Host

Schrocke, Joerg 1 Reputation point
2024-08-14T15:01:08.2266667+00:00

Hi,

I have a PowerShell Azure function (Timer triggered) which calls an external API via Invoke-RestMethod.

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"

$headers.Add("Authorization", "Basic $secret")

$headers.Add("Content-Type", "application/json; charset=utf-8")

$headers.Add("Cookie", "......")

$roleurl = "https://myurl.domain.com/rest/api//$($project.key)/role"

$roles = Invoke-RestMethod $roleurl -Method 'GET' -Headers $headers

Always when I trigger this to run in the Azure Portal, I am getting

2024-08-14T14:43:48Z [Error] ERROR: No such host is known. (myurl.domain.com:443) Exception : Type : System.Net.Http.HttpRequestException TargetSite : Name : MoveNext DeclaringType : System.Net.Http.HttpConnectionPool+<ConnectToTcpHostAsync>d__98, System.Net.Http, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a MemberType : Method Module : System.Net.Http.dll Message : No such host is known. (myurl.domain.com:443) InnerException : Type : System.Net.Sockets.SocketException Message : No such host is known. SocketErrorCode : HostNotFound ErrorCode : 11001 NativeErrorCode : 11001 TargetSite : Name : ThrowException DeclaringType : System.Net.Sockets.Socket+AwaitableSocketAsyncEventArgs, System.Net.Sockets, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a MemberType : Method Module : System.Net.Sockets.dll Source : System.Net.Sockets HResult : -2147467259 StackTrace : at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken) at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token) at System.Net.Sockets.Socket.<ConnectAsync>g__WaitForConnectWithCancellation|277_0(AwaitableSocketAsyncEventArgs saea, ValueTask connectTask, CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.ConnectToTcpHostAsync(String host, Int32 port, HttpRequestMessage initialRequest, Boolean async, CancellationToken cancellationToken) Source : System.Net.Http HResult : -2147467259 StackTrace : at System.Net.Http.HttpConnectionPool.ConnectToTcpHostAsync(String host, Int32 port, HttpRequestMessage initialRequest, Boolean async, CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.AddHttp11ConnectionAsync(HttpRequestMessage request) at System.Threading.Tasks.TaskCompletionSourceWithCancellation`1.WaitWithCancellationAsync(CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.GetHttp11ConnectionAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.SendWithVersionDetectionAndRetryAsync(HttpRequestMessage request, Boolean async, Boolean doRequestAuth, CancellationToken cancellationToken) at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken) at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken) at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.GetResponse(HttpClient client, HttpRequestMessage request, Boolean keepAuthorization) at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.ProcessRecord()

I am able to run the same e.g. on my local machine succesfully.

Did I missed anything to configure or import?

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,029 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,552 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Rich Matheisen 46,796 Reputation points
    2024-08-14T19:38:49.46+00:00

    Add this at the beginning of your code:

    
    Try{
        Resolve-DNSName myurl.domain.com -EA STOP
    }
    Catch{
    	"This is a DNS problem!"
        Throw
    }
    

    You can add your own DNS servers to the Resolve-DNSName: -Server 'MyDNS1','192.168.1.1', etc.

    By default only the DNS servers present on the interfaces are queried. The one(s) on your local machine are probably different.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.