I want to run curl command in PowerShell but I am facing error that I am not able to connect SSL/TLS
$loginurl= https://education.org/logon
$data= New-Object "System.collections.Generic.Dictionary[[String],[String]]"
$data.Add('username','abc')
$data.Add('password','abc')
$method=POST
$response= Invoke-RestMethod -Method $method -Uri $loginurl -Body $data
$response.RawContent
I tried writing in Shell it worked but in shell I used --insecure-sS as another switch to work. Can anyone please help how to resolve this in PowerShell and sometimes it gives me syntax error as well.
I think you need the -SkipCertificateCheck switch:
Invoke-RestMethod -Method $method -Uri $loginurl -Body $data -SkipCertificateCheck
Incidentally, a simpler way to create your hashtable is like this:
$data = #{'username'='abc';'password'='abc'}
Related
I need to create a solution to download the usage reports on a scheduled basis from here: https://reports.office.com/pbi/v1.0/{tenantid}/UserActivity
This should be as simple as:
$cred = Get-AutomationPSCredential -Name 'reportingserviceaccount'
$uri = "https://reports.office.com/pbi/v1.0/{tenantid}/UserActivity"
Invoke-RestMethod -Method Get -Uri $uri -Credential $cred -OutFile C:\users\Public\output.json
or
Invoke-WebRequest -Uri $uri -Credential $cred -OutFile C:\users\Public\output.json
However, both cases I only get the login form downloaded instead of the data. When I open the url in my browser I can correctly see the json response. Can somebody give me a hint what am I missing?
I would like to call a remote Rest web service from a Windows server hosting the remote certificate.
I've exported the certificate from the remote server and added it to the Windwos store. (/Personal/myCert)
I would like to use it on a Invoke-RestMethod PowerShell command.
Here bellow is the code I've tried
# Variables
$Remote_Uri = "https://remote.example.com/service/search"
$Remote_CertificateName = "myCert"
$Remote_ApiKey = "oisdjfSOEDJFKQDfSDKFjsQDKFJ"
$Remote_ContentType = "application/json"
$LocalArtifactPath = "C:\RemoteObjects.json"
# Get Certificate
$Remote_CertificateThumbprint = (Get-ChildItem -Path Cert:\LocalMachine\My | Where-Object {$_.Subject -match $Remote_CertificateName}).Thumbprint;
$Certificate = Get-ChildItem -Path Cert:\LocalMachine\My\$Remote_CertificateThumbprint
# Basic Encoding
$encoding = [System.Text.Encoding]::ASCII.GetBytes($Certificate)
$encodedString = [System.Convert]::ToBase64String($encoding)
$BasicAuth = "Basic " + $encodedString
# Set Headers
$Headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$Headers.Add("Authorization", $BasicAuth)
$Headers.Add("api", $Remote_ApiKey)
$Headers.Add("Content-Type", $Remote_ContentType)
# Self-signed certificate
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }
# Call Rest Service
Invoke-RestMethod -Method Get -Uri $Remote_Uri -OutFile $LocalArtifactPath -Headers $Headers
Invoke-RestMethod -Method Get -Uri $Remote_Uri -OutFile $LocalArtifactPath -Certificate $Certificate
Invoke-RestMethod -Method Get -Uri $Remote_Uri -OutFile $LocalArtifactPath -CertificateThumbprint $Remote_CertificateThumbprint
# Self-signed certificate off
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = $null
The three lines with Invoke-RestMethod commands failed with respectively :
Wrong header (this was expected but I gave it a try)
Authorization is empty or scheme is not basic
Certificate thumbprint not found
I've got the rest call working with #{"AUTHORIZATION"="Basic Base64Encode(user:pass)"} so I can tell the service is answering but I would like not to use user:pass in my script.
I would like to use the Certificate I've added to the Windows Store.
I'm wondering about two things :
Is the "Basic" authorization scheme is the good one to use with a certificate ?
In powershell, how to use a certificate from the local windows store running Invoke-RestMethod command ?
Thank you for your help
Adding this [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 in my script fixed the "The underlying connection was closed" issue.
Before this crosscheck whether IIS is enabled in your system.
I am trying to post a file to an api using powershell. I am able to post to the api with curl -F but not with Invoke-WebRequest ...-infile . I get an internal server error. I am assuming that it has something to do with how the file is broken up but I am not sure.
Invoke-RestMethod -Uri 'myapi' -ContentType 'multipart/form-data' -Method Post -Headers $headers -Body $Fields
Where:
$headers = #{'Authorization' : 'Basic <Token>'}
Error:
Invoke-RestMethod :
500 Internal Server Error
Internal Server Error
The server encountered an internal error or
misconfiguration and was unable to complete
your request.
When using PowerShell's Invoke-RestMethod to a service that uses Windows Authentication, I'm getting a "401.2 Not Authorized" Exception, but I've confirmed via
$currentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
that I'm using the expected user. I've verified that user is allowed in my service's web.config. Here's my actual invocation:
Invoke-RestService -Method "POST" -Uri $serviceApiUri -Body (ConvertTo-Json $body) -UseDefaultCredentials -ContentType "application/json"
Anyone else have issues with PowerShell's Invoke-RestMethod with Windows Authentication? How can I fix this?
I am using invoke-webrequest command to load a file on server with POST command, and the file does get loaded. However the command hangs in console until I hit enter.
Here's what I m executing:
Invoke-RestMethod -Uri $url -Method POST -ContentType $ContentType -Headers $headerHash -InFile $filePath -TimeoutSec $timeOut