Powershell Script to download a file from HTTPS website using - powershell

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

Related

Powershell Invoke - RestMethod

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“.

PowerShell download exe from public Github

I'm unable to download a .exe file from Github. Script works for different sites and downloads files without issues.
https://github.com/ShareX/ShareX/releases/tag/v13.1.0
This is the .exe I'm trying to download:
https://github.com/ShareX/ShareX/releases/download/v13.1.0/ShareX-13.1.0-setup.exe
>$DownloadUrl = "https://github.com/ShareX/ShareX/releases/download/v13.1.0/ShareX-13.1.0-setup.exe"
>$WebResponse = Invoke-WebRequest -Uri "$DownloadUrl" -Method Head
Invoke-WebRequest : The remote server returned an error: (403) Forbidden.
At line:2 char:16
+ $WebResponse = Invoke-WebRequest -Uri "$DownloadUrl" -Method Head
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
Full script:
[Net.ServicePointManager]::SecurityProtocol = "Tls, Tls11, Tls12, Ssl3"
$DownloadUrl = "https://github.com/ShareX/ShareX/releases/download/v13.1.0/ShareX-13.1.0-setup.exe"
$WebResponse = Invoke-WebRequest -Uri "$DownloadUrl" -Method Head
Write-Output "Downloading $DownloadUrl"
Start-BitsTransfer -Source $WebResponse.BaseResponse.ResponseUri.AbsoluteUri.Replace("%20", " ") -Destination "C:\Users\Pegavo\Desktop\PS\"
You can just simply use Invoke-WebRequest with -OutFile parameter.
Invoke-WebRequest https://github.com/ShareX/ShareX/releases/download/v13.1.0/ShareX-13.1.0-setup.exe -OutFile "ShareX-13.1.0-setup.exe"
This command will download the file from GitHub and store the result to a file in your current directory.
Or, you can use WebClient.
$webClient = New-Object System.Net.WebClient
$webClient.DownloadFile("https://github.com/ShareX/ShareX/releases/download/v13.1.0/ShareX-13.1.0-setup.exe", "E:\your\path\ShareX-13.1.0-setup.exe")
I tested both on my local machine. Both worked.
Moreover, if you want to understand why Start-BitsTransfer won't work, here.
Edit:
You can get the filename automatically like this one, using Split-Path:
$url = "https://github.com/ShareX/ShareX/releases/download/v13.1.0/ShareX-13.1.0-setup.exe"
$file= Split-Path $url -Leaf #file is ShareX-13.1.0-setup.exe now
Invoke-WebRequest $url -OutFile $file
Is there a reason for using HEAD? GET seems to work

PowerShell Invoke-RestMethod throws WebCmdletWebResponseException

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?

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.

Invoke-WebRequest Failure

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