Forum Discussion

EricShahi's avatar
EricShahi
Helper IV
3 years ago
Solved

Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a receive

Hi everyone,

 

To Introduce myself : Working as a Power BI Developer with PBI Admin access. 

 

My Powershell script stoped working suddenly and prompting me an error saying the underlying connection was closed. This was all working fine few days back.

Here is a part of my script that calls REST API to export the reprot in pdf format. 

 

Login-PowerBI


$groupid = "Hidden"
$Reportid = "Hidden"
$Folder = "c:\temp\"
$Body = "{`”format`”:`”pdf`”}"
$filename = $Folder + "PowerBIMetrics.pdf"

$StatusCheck=0

# Get token
$token = Get-PowerBIAccessToken -AsString
##write-host $token


# Building Rest API header with authorization token
$authHeader = @{
"Authorization"= $token
"Content-Type" = "application/json"
}

 

$url1 = "https://api.powerbi.com/v1.0/myorg/groups/$groupid/reports/$Reportid/ExportTo"

Invoke-RestMethod -Method Post -uri $url1 -Headers $authHeader -body $Body

 

I have tried to look for solution and many of them (Power BI community/ DBA) is saying I need to add extra line  of code below before I execute the Invoke-ResMethod command line,

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

 

Unfortunately, I'm getting same error message.

 

I also tried to used otther method from link below but no luck either.
PowerShell - The underlying connection was closed: An unexpected error occurred on a send. - darrenjrobinson - Bespoke Identity and Access Management Solutions

Thank you.

Eric

 

  • EricShahi's avatar
    EricShahi
    3 years ago

    Hi, 

     

    This could be as I am using a proxy server / connected to the client network using VPN. 

    TBH, the only way to find out is to go to office and try to execute this API and see it works.

     

    I will keep an update on this.

     

    Thanks.

5 Replies

  • nitishsh91 

    Thanks for your reply , I added that TLS line on the top of the code and ran with same error.

     

    You mentioned the issue might be with TLS in my firewall which is not allowing the connection ? Do you know how we could confirm/check on this? 

     

    I hope we can get bottom of this.

     

    FYI - This was changed recently , could this be the reason for this issue ? 

    Enable TLS 1.2 support as Azure AD TLS 1.0/1.1 is deprecated - Active Directory | Microsoft Learn

     

    Regards,

    Eric

    • nitishsh91's avatar
      nitishsh91
      Solution Supplier

      Are you trying it in a browser within client network?

      If yes then please can try opening powerbi on your mobile or personal laptop browser incognito mode and try it to see if it gives any result or not.

       

      Yes it was changed recently and this was my guess that it's causing an issue for you.

       

      This notification has been going in for a while and changes are being rolled out to disabled old tls versions.

      • EricShahi's avatar
        EricShahi
        Helper IV

        Hi, 

         

        This could be as I am using a proxy server / connected to the client network using VPN. 

        TBH, the only way to find out is to go to office and try to execute this API and see it works.

         

        I will keep an update on this.

         

        Thanks.