Powershell and Rest Issue

So I am attempting to mess around with Powershell and REST but seem to be stuck on the most simplest of tasks. Every time I run the code below the result is always:

Invoke-RestMethod : 
401 - Unauthorized: Access is denied due to invalid credentials.
Server Error
  401 - Unauthorized: Access is denied due to invalid credentials.
  You do not have permission to view this directory or page using the credentials that you supplied.

I don’t understand why. I can take the url and go to it in a browser, enter the same creds (not what is shown below) and it works. Works in Excel too. Just can’t get Powershell to connect. Any suggestions?

epicor username
$user = “epicor”
epicor password
$pass = “epicor”

baq End point URL
$uri = “https://myserver/epicortest10/api/v1/BaqSvc/zCustomer01/

#Create encoded password
$secpasswd = ConvertTo-SecureString $pass -AsPlainText -Force

#Create Credential
$cred = New-Object System.Management.Automation.PSCredential ($pass, $secpasswd)

rest Call
$data = Invoke-RestMethod $uri -Method Get -Credential $cred -ContentType “Application/Json”

#Dump results as CSV
$data.value | Export-Csv -Path ‘C:\TEST\test.csv’ -NoTypeInformation

@josecgomez, @hkeric.wci

1 Like

That’s misspelled but it shouldn’t matter. This works for me right out of the box, there is one thing where you need to enable some special permissions in powershell for this to execute right however it should have prompted you about that.

If you run fiddler do you see the call being made? Can you give that shot?

Not sure what exactly I should look at but Fiddler sees a 200 result.

Here are the stats on that call:

 This is a Tunnel. Status: CLOSED, Raw Bytes Out: 1,047; In: 7,727

The selected session is a HTTP CONNECT Tunnel. This tunnel enables a client to send raw traffic (e.g. HTTPS-encrypted streams or WebSocket messages) through a HTTP Proxy Server (like Fiddler).

To enable Fiddler's HTTPS-decryption feature and view decrypted traffic, click Tools > Options > HTTPS.

Request Count:   1
Bytes Sent:      205		(headers:205; body:0)
Bytes Received:  184		(headers:184; body:0)
Tunnel Sent:     1,047
Tunnel Received: 7,727

ClientConnected:	15:58:43.267
ClientBeginRequest:	15:58:43.280
GotRequestHeaders:	15:58:43.280
ClientDoneRequest:	15:58:43.280
Determine Gateway:	0ms
DNS Lookup: 		0ms
TCP/IP Connect:	0ms
HTTPS Handshake:	0ms
ServerConnected:	15:58:43.281
FiddlerBeginRequest:	15:58:43.281
ServerGotRequest:	15:58:43.281
ServerBeginResponse:	00:00:00.000
GotResponseHeaders:	00:00:00.000
ServerDoneResponse:	16:00:23.824
ClientBeginResponse:	16:00:23.824
ClientDoneResponse:	16:00:23.824

	Overall Elapsed:	0:01:40.544

RESPONSE BYTES (by Content-Type)
~headers~: 184

If fiddler sees as 200 then I’m not sure how Powershell can see it any differently… That’s odd…

Do you have dollar signs in your password and or username?

I do not. I have also tried multiple different users. I am perplexed. When I run it the first time, I see it in fiddler as result 200 and powershell says 401. Every time I run it again, no activity appears in Fiddler. I have to close powershell and reopen and rerun to get another entry in Fiddler. Not confident we are seeing what we should in Fiddler.

My colleague also tried on his system and it gets the same results.

Is your SSL Cert trusted?

Yes. Just set it today and verified that calls in Excel now work where they didn’t before.

Where you are creating your credential, you should be passing $user instead of $pass.


Good catch @andrew.johnson

And if you happen to be using Windows Authentication, you just use the -UseDefaultCredentials flag on the Invoke-RestMethod command.

$data = Invoke-RestMethod $uri -Method Get -ContentType "Application/Json" -UseDefaultCredentials

… (sigh) …

Thanks @andrew.johnson!

1 Like

Hey, us neighbors look out for each other!


@josecgomez, that means your code on github is wrong. That is where i got it. … no blaming. :stuck_out_tongue_winking_eye:

I know not what speak of, my github code is perfect… go look :wink:

LMAO the reason it worked in my example is cause “epicor” / " epicor" hehe


There is a command for that…