coverting CURL command to powershell

Hi,

Am trying to access an API, hosted by our team, using the following CURL command.

openssl pkcs12 -in iapi.fix.acp.aws.accenture.com.pfx -out certs.pem -nodes

curl --cacert certs.pem --key iapi.fix_priv.key --cert certs.pem:bpochp@123 -include -H "Accepts: application/json" -X GET https://iapi.fix.acp.aws.accenture.com/tenants -u admin:Password1

While this works fine off curl, am trying to replicate the same in powershell.

Can somebody help me please?

Have figured this out till here,

$cert=Get-PfxCertificate -FilePath iapi.fix.acp.aws.accenture.com.pfx

$user = "admin"
$pass = "Password1"
$pair = "$($user):$($pass)"

$bytes = [System.Text.Encoding]::ASCII.GetBytes($pair)
$base64 = [System.Convert]::ToBase64String($bytes)

$basicAuthValue = "Basic $base64"

$headers = @{ Authorization = $basicAuthValue }

$r=Invoke-WebRequest -uri "https://iapi.fix.acp.aws.accenture.com/tenants" -Headers $headers -Certificate $cert -CertificateThumbprint $cert.Thumbprint -ContentType application/json -Method Get

$r.Content

But an guessing am doing something wrong, as i end up in a could not connect to remote host error, can somebody also please tell me how to insert keys into a power shell cal?(--key iapi.fix_priv.key equivalent in PowerShell).

Any help here would be greatly appeciated!

July 7th, 2015 12:15am

#
$r=Invoke-WebRequest -uri https://iapi.fix.acp.aws.accenture.com/tenants -Certificate $cert -CertificateThumbprint $cert.Thumbprint -ContentType application/json
#

With cert you cannot claim basic authentication.

Free Windows Admin Tool Kit Click here and download it now
July 7th, 2015 12:34am

Here is a cleaner way to lay it out:

$splat=@{
    Uri='https://iapi.fix.acp.aws.accenture.com/tenants'
    ContentType='application/json'
    Certificate=$cert
    CertificateThumbprint=$cert.Thumbprint
}
$r=Invoke-WebRequest @splat


July 7th, 2015 12:41am

If you also have to use page validation then this will likely work:

$r=Invoke-WebRequest @splat -Credential 'userid'

You will be prompted for the password.

Free Windows Admin Tool Kit Click here and download it now
July 7th, 2015 12:47am

Thank You!

This has proved really helpful!

Also, one more query please.

$cert=Get-PfxCertificate -FilePath iapi.fix.acp.aws.accenture.com.pfx

The .pfx file is locked within a password, and contains a cert file that am using to authenticate my API.

When i do automate my scripts, i always do get the type Password  field,  post which the script doesn't execute.

Is there any way to tell this command $cert=Get-PfxCertificate -FilePath iapi.fix.acp.aws.accenture.com.pfx to sort of use "ABC123" as password always(without giving me a prompt, which stops further script execution, unless typed in manually)?

Any thoughts here would be helpful!

July 8th, 2015 3:03am

No.  Why would you send your certificate private key to t remote?  That is not very secure.

The password is embedded in the cert and is encoded. YOu only need to pass the cert to the remote as the remote will have the other key and be able to validate it.

Free Windows Admin Tool Kit Click here and download it now
July 8th, 2015 3:04am

Let me explain-

The .pfx file exists in my local(which is locked with a password), and am trying to get that cert from the pfx file and then pass it to the API call.

In SSL i would do it like this:

[root@ip-172-31-18-185 iapi.fix]# openssl pkcs12 -in iapi.fix.acp.aws.accenture.com.pfx -out certs.pem -nodes

Enter Import Password:

MAC verified OK

Same thing in power shell as well:

Would like to automate this completely so that it knows the password of the local .pfx file without me manually trying to do it every time i call the API.

July 8th, 2015 3:15am

Apologies, the text was missed above.

In PowerShell,

$cert=Get-PfxCertificate -FilePath iapi.fix.acp.aws.accenture.com.pfx
Enter password: ***********

Free Windows Admin Tool Kit Click here and download it now
July 8th, 2015 3:15am

$c=Get-PfxData -FilePath C:\test\test.pfx -Password $pwd

July 8th, 2015 3:39am

Strangely thats the first option i tried.

Get-PfxCertificate : A parameter cannot be found that matches parameter name 'Password'.
At line:1 char:71
+ $cert=Get-PfxCertificate -FilePath iapi.fix.acp.aws.accenture.com.pfx -Password  ...
+                                                                       ~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-PfxCertificate], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetPfxCertificateCommand

What version of power shell are you running?

Free Windows Admin Tool Kit Click here and download it now
July 8th, 2015 3:43am

Look at what I posted. You are not typing what I pasted.

July 8th, 2015 3:54am

Apologies on missing that, but there still seems to be an issue here.

$cert1=Get-PfxData -FilePath iapi.fix.acp.aws.accenture.com.pfx -Password $pwd
Get-PfxData : The term 'Get-PfxData' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:8
+ $cert1=Get-PfxData -FilePath iapi.fix.acp.aws.accenture.com.pfx -Password $pwd
+        ~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Get-PfxData:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Am i missing something here?

Free Windows Admin Tool Kit Click here and download it now
July 8th, 2015 5:10am

Get-PfxData  - Windows 8.1 and later.
July 8th, 2015 9:43am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics