Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
EricShahi
Helper IV
Helper IV

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.

EricShahi_1-1665227264165.png

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. - darrenj...

Thank you.

Eric

 

1 ACCEPTED SOLUTION

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.

View solution in original post

5 REPLIES 5
EricShahi
Helper IV
Helper IV

@nitishsh91   To add in more...

I passed in parameters groupId/reportid/body and click "run" but nothing happens, it doesn't give me any result, this was working fine before.
Reports - Export To File - REST API (Power BI Power BI REST APIs) | Microsoft Learn

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.

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.

EricShahi
Helper IV
Helper IV

@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
Solution Supplier
Solution Supplier

Hi @EricShahi 

 

I also suspect issue with tls in your firewall from where you are trying to run this command, please add that line to top of your script and see if it helps.

Also use try it feature from below link and see if that works and if it does work then issue is with your firewall which is not allowing the connection.

https://learn.microsoft.com/en-us/rest/api/power-bi/reports/export-report 

 

Please let me know if it helps.

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors