PowerShell Invoke-RestMethod throws WebCmdletWebResponseException - powershell

I am trying the following command
Invoke-RestMethod -Uri 'http://serverIP/my/application/URL' -Method 'GET' -WebSession $CurrentSession
I tried it with Invoke-WebRequest as well But I am getting this error for both of them
Invoke-WebRequest : An unexpected error has occurred while attempting to serialize a Date datatype.
At line:1 char:1
+ Invoke-WebRequest -Uri 'http://serverIP/my/application/UR ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
I am quite new at using the Invoke-RestMethod
Can someone please help me with this error?

Related

Invoke web request throwing an error : Invoke-WebRequest : {"errors":[]}

I am using a powershell script to get details from a website via invoke-webrequest. By using the credentials i have connecting to the website using invoke-restmethod to get the token.
After getting the token i used the token to retrieve the details that i need by using invoke-webrequest. But then it is throwing the error this way.The URL is workingfine.
"Invoke-WebRequest : {"errors":[]}"
How can i get to know what is the problem with this request.
API Query:
Invoke-WebRequest -Headers #{"X-Token" = "$token"} -Method Get -Uri "$URL"
error message:
Invoke-WebRequest : {"errors":[]} At C:\temp\RTPA\rtpa_vault3.ps1:24 char:24
+ ... ltdetails = Invoke-WebRequest -Headers #{"X-Token" = "$tok ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

Basic Authentication for Artifactory from PowerShell is throwing 403

I am trying to authenticate to JFROG from powershell i tried it with my token and its giving 403 forbidden
PS C:\Myproject> $myHeaders = #{'X-JFrog-Art-Api' = 'AKCp8sdFTEKF1Y5MDgM3M8RK6bRkKWoX43jWranZvS2U2DE82KFE7243F'}
PS C:\Myproject> Invoke-WebRequest -Uri "https://org.jfrog.io/" -Method Get -Headers $myHeaders
Invoke-WebRequest : 403 Forbidden
nginx
At line:1 char:1
+ Invoke-WebRequest -Uri "https://fiprod.jfrog.io/" -Method Get -Header ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
Any idea on how to authenticate to Artifactory via a token using PowerShell ?

Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send via proxy

Error:
Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send
via : proxy
powershell version 4.0, the same request works with later version of powershell
Tried to invoke web request
$headers = #{ "Authorization" = "Bearer 7jNQ0nL"}
Invoke-WebRequest -Uri https://api.line.me/v2/bot/user/all/richmenu -Method Get -Proxy proxy.xx.xxxnet.com:8080 -ProxyUseDefaultCredentials -Headers $headers
but this is what happens:
Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send.
At line:1 char:1
+ Invoke-WebRequest -Uri https://api.line.me/v2/bot/user/all/richmenu -Method Ge ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

Powershell : Invoke-WebRequest - Jenkins api build trigger

$apit="userid:apitoken"
$serverhost="#host1.com"
$vin="https://${apit}\${serverhost}"
Invoke-WebRequest -Uri $vin -Method POST
error log :
Invoke-WebRequest : Cannot bind parameter 'Uri'. Cannot convert value "https://${apit}\${serverhost}" to type "System.Uri". Error:
"Invalid URI: The hostname could not be parsed."
At line:5 char:24
+ Invoke-WebRequest -Uri $vin -Method POST
+ ~~~~
+ CategoryInfo : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingException
+ FullyQualifiedErrorId :
CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
$apit="userid:apitoken"
$serverhost="#host1.com"
$vin="https://" + $apit + $serverhost
Invoke-WebRequest -Uri $vin -Method POST
This syntax for string concatenation is the most readable IMO. Your curly braces are unnecessary.

Error: System.ArgumentException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

While going through https://github.com/Azure/azure-iot-remote-monitoring at Local deployment and while debugging got an error:
Error:
At G:\Azure-IoT\2.Customize a preconfigured solution\Trail_1\azure-iot-remote-monitoring\Common\Deployment\DeploymentLib.ps1:704 char:23
+ $result = Invoke-RestMethod -Method "GET" -Uri $uri -Headers #{"Auth ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Invoke-RestMethod], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
I am using Azure power shell 1.6.0 Version.
Can anyone tell me what's going wrong ?