MongoDB Atlas interaction with PowerShell - mongodb

I need help with the connection MongoDB atlas API with PowerShell. With bash, it works perfectly
curl --include --user "{PUBLIC-KEY}:{PRIVATE-KEY}" --digest \
--header "Accept: application/json" \
--header "Content-Type: application/json" \
"https://cloud.mongodb.com/api/atlas/v1.0/orgs/{ORG-ID}?pretty=true"
but I'd like to do the same thing Powershell, Can someone help with that? (

#RamanSilopal Thank you so much for the link :)
I've converted it to PowerShell like that:
$ApiPrivateKey
$ApiPublicKey
$Uri = 'https://cloud.mongodb.com/api/atlas/v1.0/groups/{ORG-ID}/accessList?pretty=true'
[securestring]$secStringPassword = ConvertTo-SecureString $ApiPrivateKey -AsPlainText -Force
[pscredential]$credential = New-Object System.Management.Automation.PSCredential ($ApiPublicKey, $secStringPassword)
Invoke-RestMethod -Uri $Uri -ContentType Application/Json -Headers #{Authorization = “Basic $base64AuthInfo”} -Credential $credential -Method Get

Related

Convert HTTP request to Powershell and cURL

I'm trying to convert a HTTP request (which I've never dealt with before) into a cURL and Powershell command, but I'm not getting anywhere....can someone help me fill in the blanks?
POST /api/users.profile.set
Host: slack.com
Content-type: application/json; charset=utf-8
Authorization: bearer_token
{
"profile": {
"status_text": "Eating some french fries from the frituur",
"status_emoji": ":fries:"
}
}
curl 'https://slack.com/api/users.profile.set' `
--header 'Authorization: bearer_token' `
--header 'Content-Type: application/json' `
--data-raw "{
\"profile\": {
\"status_text\": \"On Lunch\",
\"status_emoji\": \":hamburger:\"
}
}"
and
Invoke-WebRequest -Headers #{"Authorization" = "bearer_token"} `
-Method POST `
-Uri https://slack.com/api/users.profile.set `
-ContentType application/json
Fixed it myself.
curl -X POST 'https://slack.com/api/users.profile.set' `
-H 'Authorization: Bearer xoxp-xxxxxxxxxxxxxxxxx' `
-H 'Content-Type: application/json; charset=utf-8' `
-d '{
\"profile\": {
\"status_text\": \"Sick\",
\"status_emoji\": \":sickpepe:\",
\"status_expiration\": \"480\"
}
}'
$body = #{
profile = #{
status_text = "Eating some french fries from the frituur"
status_emoji = ":fries:"
status_expiration = 60
}
}
Invoke-WebRequest -Headers #{"Authorization" = "Bearer xoxp-xxxxxxxxxxxxxxxxxxxx"} `
-Method POST `
-Uri https://slack.com/api/users.profile.set `
-ContentType 'application/json; charset=utf-8' `
-Body ($body|ConvertTo-Json)
```

Convert a curl command to powershell

I would like to convert a command to generate a masterkey from curl to PowerShell but I never did things like that before..
The objective is to access to an ovea Api but first I have to generate a masterkey.
Here is the curl command (tested on centos and works):
curl -L -H 'Content-Type: application/json' -H 'Authorization: Basic Yw...=' -X POST https://dnsadmin.test.com/apikeys --data '{"description": "masterkey","domains":["testapi.com"], "role": "User"}'
And here is what I tried:
$headers = #{}
$headers.Add = ("Authorization",'Basic Yw...=')
$headers.Add {-description = "masterkey","domains" ["testapi.com"], "role": "User"}
Invoke-RestMethod -Method Post -Uri https://dnsadmin.test.com/apikeys -Headers $headers -ContentType "application/json"
If any of you have an idea I would take it thank you.
When you specify --data with curl it is sent as the body of the request. In the Powershell example you're trying to do some stuff with the header that doesn't make sense.
This would be the equivalent to your curl command:
$headers = #{
Authorization = 'Basic Yw...='
}
Invoke-RestMethod -Method Post -Uri https://dnsadmin.test.com/apikeys -Headers $headers -ContentType "application/json" -Body '{"description": "masterkey","domains":["testapi.com"], "role": "User"}'

Problem with cURL to Powershell conversion

I have been trying to convert the below cURL command to PowerShell so that I can use it on my script. I converted it but, every time I execute the command, it gives me error like in the screenshot.
cURL command: curl -X POST "https://www.hybrid-analysis.com/api/v2/quick-scan/file?_timestamp=1585901455112" -H "accept: application/json" -H "user-agent: API_KEY" -H "Content-Type: multipart/form-data" -F "scan_type=all" -F "file=#xyz.png;type=image/png" -F "no_share_third_party=false" -F "allow_community_access=false"
converted powershell command:
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12;
$fileqq = Get-ChildItem -Path "C:\temp\xyz.jpg"
Invoke-WebRequest "https://www.hybrid-analysis.com/api/v2/quick-scan/file" -Headers #{'accept' = 'application/json'; 'user-agent' = 'Falcon Sandbox' ; 'Content-Type' = 'multipart/form-data' ; 'api-key' = "API_KEY"} -Method Post -Body #{"scan_type" = "all"; "file" = "$fileqq" }
powershell command error
It would be helpful for me if someone pointed out the error or correct my converted command
Thank you very much for the help.

How to convert curl POST with headers authorization and form to PowerShell v3.0

I have follwoing curl command which I would like to convert to PowerShell (PowerShell v3.0):
curl -s -k -H 'Authorization: api_key 1234567890' -F upload_file=#"C:\path\to\file" -X POST "https://my.url.com/submit"
What I have so far:
$Body= #{ "upload_file" = "C:\path\to\file" };
$Headers = #{ "Authorization" = "api_key 1234567890" };
$Uri = "https://my.url.com/submit"
Invoke-WebRequest -Method Post -Uri $Uri -H $Headers -Body $Body
I think the form parameter is the issue.
Thanks in advance.

Converting Curl API Call to Invoke-WebRequest in PowerShell 5

I have done a bunch of searches, but nothing so far as helped me solve this. Any help is appreciated.
I am trying to convert a curl command to Invoke-WebRequest, but for some reason I am not getting a response back. I have tried:
$headers = #{
outh_a = "WEBREQ";
oauth_key = "key";
etc...
}
Invoke-WebRequest -Uri $url -Method POST -Body "{}" -Headers $headers -Verbose
The curl command looks like this:
.\curl.exe -x POST -d "{}" -H "oauth_a: WEBREQ" -H "oauth_key:key" -H "Content-Type: application/json" -H "oauth_sm:HMAC-SHA2" -H "oauth_t:timestamp"-H "oauth_s:key+signature=" "https://example.com/services/api/request?t=data&a=data2&n=404&i=0"
The command works perfectly and returns the data. I am using PowerShell as we want to parse through the data once received. Any help is greatly appreciated.
The curl command will fail if I submit it without -d "{}", so that part is required. The server I guess is expecting to receive a specific amount of data.
I am not sure what is going on to prevent a response. I have tried curl from the same machine I am doing the PowerShell script on and it works. I even used SoapUI to make the same call and it works there too.
Edit:
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
[System.Net.WebRequest]::DefaultWebProxy.Credentials =
[System.Net.CredentialCache]::DefaultCredentials
[Net.ServicePointManager]::SecurityProtocol =
[Net.SecurityProtocolType]::Ssl3, [Net.SecurityProtocolType]::Tls,
[Net.SecurityProtocolType]::Tls11, [Net.SecurityProtocolType]::Tls12
$Domain = "https://example.com/services/api/request?t=oauth_auth_token&a=appid&n=404&i=0"
# OAUTH Authorization Request Header
$Header = #{
o_nonce = 'TEST'
o_con_key = 'key'
o_sig_meth = 'type'
o_timestamp = 'timestamp'
o_sig = 'signature'
o_ver = '1.0'
o_realm = 'http://example.com/services'
}
$Body = #{}
$API_Response = Invoke-RestMethod -Method POST -Headers $Header -Uri $Domain -Body $Body -Verbose -TimeoutSec 20
Update:
#MikeW I'd have to look at your PS code in more detail, could you post the entire API call minus the sensitive information (api key etc...)?
Also, on a side note, sometimes just passing the parameters on the URI works just as well. Have you tried passing the parameters through the URI? You can see the following example of an API call I'm making using just the URI:
Invoke-RestMethod -Uri ("$EndpointURL/Tickets/$TicketID/?username=$ServiceAccount&apiKey=$APIKey");