1

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.

Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a receive. At line:25 char:7 Invoke-RestMethod -Method Post -uri $url1 -Headers $authHeader ... CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebExcepti on FullyQualifiedErrorId:WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

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

$StatusCheck=0
$token = Get-PowerBIAccessToken -AsString

$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 also 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 if i add above line.

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. https://learn.microsoft.com/en-us/rest/api/power-bi/reports/export-to-file#code-try-0

EricShahi
  • 13
  • 4
  • Try using `Invoke-WebRequest -UseBasicParsing` in place of `Invoke-RestMethod` with the same parameters, and see if you get a better error message. Make sure you set the tls version as the first thing in a new powershell instance. If those don't work, try the second answer here to override cert checking in case of some rare ssl issues: https://stackoverflow.com/a/52996991/7411885 – Cpt.Whale Oct 08 '22 at 19:48
  • Also consider trying out `Connect-PowerBIServiceAccount` in combination with `Invoke-PowerBIRestMethod` (and make sure the module is up to date) – Cpt.Whale Oct 08 '22 at 19:53
  • That exact code works for me. What OS is that running on? Can you test on something else? Are you behind a web proxy? Corporate firewall? Can you use WireShark or Fiddler to see what's going on at the network layer? – David Browne - Microsoft Oct 09 '22 at 19:16
  • @DavidBrowne-Microsoft I'm working from home, and I usually connect to my company server using VPN. It could be firewall issue. I can only find out when I'm in the office. – EricShahi Oct 11 '22 at 16:39
  • Unless you've got PBI behind a private endpoint you should be able to test without your VPN. If you have conditional access policies preventing interactive login without your VPN, then disconnect after you generate the AccessToken, which is good for 30min or somesuch. – David Browne - Microsoft Oct 11 '22 at 16:41
  • @DavidBrowne-Microsoft this issue has been resolved from my side – EricShahi Oct 13 '22 at 14:30

0 Answers0