Big truble whit pwsh 7 and powershell_ise

hi,
i have a big truble whit powershell (pwsh 7) on windows server 2019,
i send an API POST with powershell and if i try with WIN10/WIN11 its ok, but if i use win server 2019 the server have no permission, but if i use POWERSHELL_ISE the script was ok…

the response whit pwsh is:

e[31;1me[0me[36;1me[36;1me[0me[36;1me[0me[36;1me[31;1me[31;1me[36;1m | e[31;1m Access Denied Access Denied You don’t have permission to accesse[0m
e[31;1me[0me[36;1me[36;1me[0me[36;1me[0me[36;1me[31;1me[31;1me[36;1me[31;1me[36;1m | e[31;1m"http://cloudsso.hilti.com/hc/token" on this server.e[0m

the ISE is ok…
mistake

davide,
Welcome to the forum. :wave:t3:

First of all: pwsh 7 does not support PowerShell_ISE!

Without seeing your actual code it would be guess work from us - please show your code.

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org 1 <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )

yes, i use pwsh 7…whit any powershell lower get error, in ISE no error and its work perfectly, but i need use powershell.exe non ISE

this is code
$headers = New-Object “System.Collections.Generic.Dictionary[[String],[String]]”

$headers.Add(“Content-Type”, “application/x-www-form-urlencoded”)

$headers.Add(“Authorization”, “Basic XXXXXXXXXXXXX”)

$headers.Add(“Cookie”, “_abck=9B56572CD1EEC886B0117532B216691A~-1~YAAQ1UMkF8Y0Tx2PAQAAEAriIwvn8KQ/+j/oVHAqhvLNyJ+B2UueVy5bJT4iJLEp4fzSLTTTrOHo7QBKjmYC/N3eKsOgwzLZmmhvxGdP9CLK/uRkLdm67gz1uXbZvj38lLy2TPoRElP1AFWFQP7qEvAa1iOpppipvvY/kpgRFJULrtLc0PaY9hagO8PdQ9FL9Zpeqyf0nXkwl0tT7oI0tqPA3W1+XjqDRbCUw2gaMSRA5KBf59cfV8Y1RK2JQHvrokz/suoyZ4MO+0ytbOjnU7aV5/hOPLjbar4dcggsh8JP844rQUzE5KQGKdJ26fJh1ARYLtkaCyrIylosmrl6XKzZlPT17S9okPqRBEyxLlTDCQ/lK0nxaw==~-1~-1~-1; bm_sz=76E177CB45E05F047D8D8A90F9E58ED3~YAAQ1UMkF8c0Tx2PAQAAEAriIxfdYCZmYFoZBdwJhuLKeNdWC8ZW0tq9W/2YWAFAa2FyZdlrIwe19zc3usZsdEO9zKdCykD0ILXYb0HRTd1eEyCs0vkm6vXc76VSCf79cwR7Ao8jRSGotiHnYHwSgRUDRJKHH8s24+AYeTTIDLFvDEHsF38fb2ozGR0UOaVD4ACQ1Q+h036F4kTaLFgtY6DTRMB0h/a+1vufn+dXnDPciFOUkUOZg8nONOgHsnteeW/sU7K+Mt4BYhdg6kWIBFWFxdTYLHk/K5LDWWECKdS/Q+Gkri9JN92DjulFRABQE++fuywifRshgWFaRQqIruN6/j5InhzeSD4=~4471095~3687475”)
$file = “C:\API\token.json”
$url = “https://cloudsso.hilti.com/hc/token
$body = “grant_type=password&scope=HC.Request.AllScopes&username=USER&password=PASSWORD”
$response = Invoke-RestMethod -Uri $url -Method ‘POST’ -Headers $headers -Body $body -OutFile $file -UseBasicParsing
$response | ConvertTo-Json

whit correct user, pass and auth…the response is

Invoke-RestMethod: C:\api\token.ps1:8
Line |
8 | $response = Invoke-RestMethod -Uri $url -Method ‘POST’ -Headers $head …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Access Denied Access Denied You don’t have permission to access
| “http://cloudsso.hilti.com/hc/token” on this server.
| Reference #18.a56a7b5c.1714473848.104d94fb
| https://errors.edgesuite.net/18.a56a7b5c.1714473848.104d94fb
null

i think that the win server have any policies that blocked me…in win10/11 its all ok

Hmmm … once more …

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org 1 <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )

That does not make any sense. If your code runs in the PowerShell_ISE it runs on Windows PowerShell - not PowerShell 7!

From where do you get this error message. From your local server or from the server you query with your code? :smirk:

I’m not going to try and look at your code because it not formatted and easy to read, but it might be possible this isn’t a PS issue based on what you’re reporting, but I do have some thoughts on what you might try via PS to see if you might see improvement. Also it’s hard to follow your logic as you mentioned several things.

My best guess from your report, it sounds like:

PS7 : works
PS5.1 : doesn’t work
PS5.1 via ISE : works

is that correct? and these are all on the same system or is that a variable as well? Something contextually is missing in this report that outlines the issue.

One thing that I always try to confirm is if my PS is running the latest TLS when making calls if I see weird errors. That could be a reason why, as I’ve had issues with this myself back in earlier versions of PS where I was interacting with a API and it had similar access denied messages. For me the fix for these was to do something like this:

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

I think this is the code I’ve used in the past to force TLS version to 1.2. This also might be further explained if your $profile has this line in it for ISE, but not powershell (they are different profiles). Pure guess but worth checking, and it would explain why PS7 does work.

Outside of that nothing comes to mind that means its PS related specifically, more troubleshooting on your end would need done to rule out things. I do think its possible that things could be blocked or it has to do the user agents. it is extremely hard for us to assess. you may just need to try things.

i make a recap:
pwsh 7 with any pc on win10 and win 11 its work perfectly
pwsh 7 with win server 2019 doesn’t work
powershell 5.x doesn’t work
powershell_ise works ok

i think that the problem is on win serverr with any policies…is possible?

Hi Davide,

I’d suggest you read and try my suggestion as it is 5 seconds to rule that out as a potential thing. That said i’m inclined to think it might not help if PS7 doesn’t work as I feel like that configuration is default in PS7, but still worth trying, esp on the PS 5.1 boxes

Asking a forum if something is possible… we simply don’t know anything about your environment, so the answer is going to be always yes. I think you need to troubleshoot those issues.

If the PS code works in most places, including PS7 on multiple types of systems just not on windows server boxes, I’d say it’s less likely your code and more something related to your environment/system.