1

I have a unix cmd that I need to rewrite to achieve the same result in Windows PowerShell. I am specifically stuck on the login...

Original Unix Command:

curl -u user:xxxx -X POST -s --data '{"one" : "one/text","two" : "two/text"}', http://myurl.net/run

My Attempted Conversion:

$params = '{"one" : "one/text","two" : "two/text"}'
Invoke-WebRequest -Uri http://myurl.net/run -Credential user:xxxx -Method POST -Body $params

Right now, I get the error:

Invoke-WebRequest : {"mimetype":[{"stderr":"text/plain"}],"streamdata":[{"stderr":"User cannot be authenticated"}],"status":"error"}

Thank you for any pointers or help!!

1
  • 2
    Body can take a hashtable so manually forming your own JSON is unnecessary. What you're getting, however, is an authentication error. Does your endpoint expect header authentication of some kind? Commented Jul 25, 2018 at 16:42

1 Answer 1

2

To me it looks you the error is with your -Credential param.
This expects a credential object, not a string.

Try:

$Credential = Get-Credential
$params = '{"one" : "one/text","two" : "two/text"}'
Invoke-WebRequest -Uri http://myurl.net/run -Credential $Credential -Method POST -Body $params

The Get-Credential command will promt you to enter Username and Password.

Also, if your endpoint is NOT https then you need to tell Powershell with the use of the following switch -AllowUnencryptedAuthentication paramter or it will always fail according to the documentation:

Allows sending of credentials and secrets over unencrypted connections. By default, supplying -Credential or any -Authentication option with a -Uri that does not begin with https:// will result in an error and the request will abort to prevent unintentionally communicating secrets in plain text over unencrypted connections. To override this behavior at your own risk, supply the -AllowUnencryptedAuthentication parameter.

Warning: Using this parameter is not secure and is not recommended. It is provided only for compatibility with legacy systems that cannot provide encrypted connections. Use at your own ris

The link to the cmdlet documentation on MS Docs

Sign up to request clarification or add additional context in comments.

5 Comments

Good catch. To shorten the amount of typing, you can also use -Credential (Get-Credential -Credential user) which will then prompt you for the password.
Thank you both Henrik & @theIncorrigible1 I am still getting the "user cannot be authenticated" error ... is this providing the credentials the same way that -u does in unix?
@EmaNekaf Depends what your endpoint expects as authentication.
just updated my answer, if your endpoint is not https you need to tell this to powershell too or it will always fail.
thank you both very much, I think it is a problem with my access rights

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.