The issue is that I got a script from a vendor to pull data from a DB.
The script is having a problem with the Invoke-WebRequest Section.
Here is the Script:
$url = #'
https://demo.liquidwarelabs.com/lwl/api?json={"inspector":"0","basis":"users","date":"yesterday","limit":"0","columns":"user_name,login_count","output":"1:html","header":"1"}
'#
$output = "c:\export\Tier1\view1.csv"
Invoke-WebRequest $url -OutFile $output
Here is the Error I get:
Invoke-WebRequest : The underlying connection was closed: An
unexpected error occurred on a send. At C:\Scripts\3.ps1:38 char:1
+ Invoke-WebRequest $url -OutFile $output
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest],
WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
You probably need to URL encode the JSON:
$url = 'https://demo.liquidwarelabs.com/lwl/api?json='
$json = '{"inspector":"0","basis":"users","date":"yesterday","limit":"0","columns":"user_name,login_count","output":"1:html","header":"1"}'
$encodedjson = [System.Web.HttpUtility]::UrlEncode($json)
$output = "c:\export\Tier1\view1.csv"
Invoke-WebRequest ($url+$encodedjson) -OutFile $output
Related
I have been working with Powershell all day, playing with proxy settings and related. A POST request that once worked earlier in the day, is now stating a very bizarre error:
The request:
$tenantId = '8331e1..........'
$client_id = 'a3b869ef................
$client_secret = '0w;m$................'
#************ GET AAD TOKEN ************
$url = "https://login.microsoftonline.com/" + $tenantId + "/oauth2/token"
$body = #{
grant_type = 'client_credentials'
client_id = $client_id
client_secret = $client_secret
resource = 'https://dev.azuresynapse.net'
}
$GetAADResult = Invoke-WebRequest -Method 'Post' -Uri $url -Body $body
$GetAAD_Content_JSON = $GetAADResult.Content | ConvertFrom-Json
$access_token = $GetAAD_Content_JSON.access_token
# print access_token
$access_token
This used to provide a simple token as a response, but now it is returning the error:
Invoke-WebRequest : The remote name could not be resolved: 'http'
At line:24 char:17
+ ... GetAADResult = Invoke-WebRequest -Method 'Post' -Uri $url -Body $body
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : System.Net.WebException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
ConvertFrom-Json : Cannot bind argument to parameter 'InputObject' because it is null.
At line:26 char:48
+ $GetAAD_Content_JSON = $GetAADResult.Content | ConvertFrom-Json
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [ConvertFrom-Json], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ConvertFromJsonCommand
I do not know what needs to change to get this functionating again, it appears PowerShell is looking for a proxy setting in every request...? Anny ideas?
I ended up undoing these steps: https://spaghettidba.com/2017/12/19/recovering-the-psgallery-repository-behind-a-corporate-proxy/
I would like to use an POST request with powershell but am getting the following errormessage. I have searched many hours, but can't find the problem. The json body seems also to be correct(?).
Thanks in advance for every help!
$AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'
[System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols
(New-Object System.Net.WebClient).Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
$body = #{
"client"="myKey"
"request-type"="authentication"
"username"="myUsr"
"password"="myPwd"
} | ConvertTo-Json -Depth 10
$header = #{
"Content-Type"="application/x-www-form-urlencoded"
}
$res = Invoke-RestMethod -Uri "https://myUrl:4000/abc" -Method 'POST' -Body $body -Headers $header
Invoke-RestMethod :
Error
Cannot POST /abc
In Zeile:1 Zeichen:8
+ $res = Invoke-RestMethod -Uri "https://myUrl:400 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebExc
eption
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
Edit:
I have edited my code but get the same error:
$AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'
[System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols
(New-Object System.Net.WebClient).Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
$body = #{
"client"="mykey"
"request-type"="authentication"
"username"="myusr"
"password"="mypw"
}
$res = Invoke-RestMethod -Uri "https://myUrl:4000/abc" -Method 'POST' -Body $body
And the 400 error is'nt a error code, I think it's the port.
Invoke-RestMethod :
Error
Cannot POST /abc
In Zeile:1 Zeichen:8
+ $res = Invoke-RestMethod -Uri "https://myUrl:400 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebExc
eption
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
The request should look like the following:
client=sadsda42424242&request-type=authentication&username=extern&password=geheim
Also I have to use the content type „application/x-www-form-urlencoded“.
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?
I want to use PowerShell to set shareddataset for a report using SSRS API.
I have already tried to use PowerShell to get shareddataset using SSRS API successfully. But failed to use put method.
Success using get method:
$Cred = Get-Credential
$Url = "http://localhost/Reports/api/v2.0/DataSets"
`
`$Body = #{
Path ="/Shared_data_sets/P_Calculate_date_Default_key";
output_mode = "csv";
earliest_time = "-2d#d";
latest_time = "-1d#d";
}
Invoke-RestMethod -Method 'GET' -Uri $url -Credential $Cred -Body $body -OutFile output.csv
failed using put method:
$Cred = Get-Credential
$Url = "http://localhost/Reports/api/v2.0/Reports(23a6af79-9de3-49cb-b66b-625d3ff3909b)/SharedDataSets"
$Body = #{
Id = "935be257-5a65-4c0d-b372-d5a6a188daf6";
Name = "P_Calculate_Date_Default_Key";
Path = "/Shared_data_sets/P_Calculate_date_Default_key";
Type = "DataSet" ;
}
Invoke-RestMethod -Uri $Url -Credential $Cred -Body $body -Method 'PUT'
Error returned is
The remote server returned an error: (400) Bad Request
detailed error:
Invoke-RestMethod : The remote server returned an error: (400) Bad Request. At line:12 char:1 + Invoke-RestMethod -Uri $Url -Credential $Cred -Body $body -Method 'PU ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
I hope the put method could be working. I thought I was using the same way as the get method but it still failed.
I tried another way but still failed :
$Cred = Get-Credential
$Url = "http://localhost/Reports/api/v2.0/Reports(23a6af79-9de3-49cb-b66b-625d3ff3909b)/SharedDataSets"
$Body = #{
Id = "935be257-5a65-4c0d-b372-d5a6a188daf6"
Name = "P_Calculate_Date_Default_Key"
Path = "/Shared_data_sets/P_Calculate_date_Default_key"
Type = "DataSet"
}| ConvertTo-Json
Invoke-RestMethod -Uri $Url -Credential $Cred -Body $body -Method PUT -ContentType "application/json" | Out-Null
Just for you to know that for the put method, i have already tested on postman and it works. So I guess currently it is only due to some logic in powershell side inconsistent.
in postman I put the link as :
http://localhost/Reports/api/v2.0/Reports(23a6af79-9de3-49cb-b66b-625d3ff3909b)/SharedDataSets
Body as :
[
{
"Id": "935be257-5a65-4c0d-b372-d5a6a188daf6",
"Name": "P_Calculate_Date_Default_Key",
"Path": "/Shared_data_sets/P_Calculate_date_Default_key",
"Type": "DataSet"
}
]
I tried your recommendation and it shows below , I have added it below all:
PS C:\Users\dkx42a8adm> $Cred = Get-Credential
$Url = "http://localhost/Reports/api/v2.0/Reports(23a6af79-9de3-49cb-b66b-625d3ff3909b)/SharedDataSets"
$Body = #{
Id = "935be257-5a65-4c0d-b372-d5a6a188daf6"
Name = "P_Calculate_Date_Default_Key"
Path = "/Shared_data_sets/P_Calculate_date_Default_key"
Type = "DataSet"
}| ConvertTo-Json
Write-Output $Body
Invoke-RestMethod -Uri $Url -Credential $Cred -Body $body -Method PUT -ContentType "application/json"
$error[0]|format-list -force
cmdlet Get-Credential at command pipeline position 1
Supply values for the following parameters:
{
"Path": "/Shared_data_sets/P_Calculate_date_Default_key",
"Name": "P_Calculate_Date_Default_Key",
"Id": "935be257-5a65-4c0d-b372-d5a6a188daf6",
"Type": "DataSet"
}
Invoke-RestMethod : The remote server returned an error: (400) Bad Request.
At line:14 char:1
+ Invoke-RestMethod -Uri $Url -Credential $Cred -Body $body -Method PUT ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
Exception : System.Net.WebException: The remote server returned an error: (400) Bad Request.
at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.GetResponse(WebRequest request)
at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.ProcessRecord()
TargetObject : System.Net.HttpWebRequest
CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
ErrorDetails :
InvocationInfo : System.Management.Automation.InvocationInfo
ScriptStackTrace : at , : line 14
PipelineIterationInfo : {}
PSMessageDetails :
I need to download a file daily from a website where date will passed. I tried using following code -
$url = "https://www.theocc.com/webapps/threshold-securities?reportDate=20190730"
$output = "C:\Users\Himanshu.Vats\Downloads\"
Invoke-WebRequest -Uri $url -OutFile $output
But it is giving error -
Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send.
At line:3 char:1
+ Invoke-WebRequest -Uri $url -OutFile $output
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
It's because your path is not a path file (you have a '\' to end path)
try this:
$url = "https://www.theocc.com/webapps/threshold-securities?reportDate=20190730"
$output = "C:\Users\Himanshu.Vats\Downloads\result.csv"
Invoke-WebRequest -Uri $url -OutFile $output