How to code this powershell?

oemScript 81 Reputation points
2022-12-06T23:57:20.883+00:00

Referring to following coding, I would like to know on how to setup security properly for downloading file, it seems that Tls12 is not working

Does anyone have any suggestions?
Thanks in advance

$url = "https://www.hkex.com.hk/eng/stat/dmstat/dayrpt/dqe221206.zip"
$file = "D:\ABC.zip"
[Net.ServicePointManager]::SecurityProtocol = 'Tls12'
Invoke-WebRequest $url -OutFile $file
catch [System.Exception] {
$httpStatus = $null
$tryError = $_.Exception
$urlIsValid = $false;
Write-Host "There was an error downloading $file"

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. DaveK 1,871 Reputation points
    2022-12-07T11:20:59.463+00:00

    This worked for me:

    $url = "https://www.hkex.com.hk/eng/stat/dmstat/dayrpt/dqe221206.zip"  
    $file = "D:\ABC.zip"  
    [System.Net.SecurityProtocolType]::Tls12  
    try {  
    	Invoke-WebRequest $url -OutFile $file -ErrorAction Stop  
    }  
    catch [System.Exception] {  
    	$httpStatus = $null  
    	$tryError = $_.Exception  
    	$urlIsValid = $false;  
    	Write-Host "There was an error downloading $file"  
    }  
    

3 additional answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2022-12-07T03:05:31.877+00:00

    The 1st four lines of your code work just fine.

    What exception was thrown? Was the exception caught by your Catch block? If it wasn't, try removing the "[System.Exception]" from the Catch. I'd also try adding "-ErrorAction STOP" to the Invoke-WebRequest.

    0 comments No comments

  2. oemScript 81 Reputation points
    2022-12-07T05:33:03.597+00:00

    Please see following error message.
    Could you please show on where to add "-ErrorAction STOP" to the Invoke-WebRequest.

    Do you have any suggestions?
    Thank you very much for any suggestions (^v^)

    Error Message

    Invoke-WebRequest : The request was aborted: Could not create SSL/TLS secure channel.

    • Invoke-WebRequest $url -OutFile $file
    • ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    • CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke=WebRequest],WebException
    • FullyQualifiedErrorId : WebCmdLetWebReqsponseException.Microsoft.Powershell.CommadsInvokeRequestCommand
    0 comments No comments

  3. Rich Matheisen 47,901 Reputation points
    2022-12-07T15:49:05.22+00:00

    Try changing line #3 of your script to: [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

    The literal "Tls12" is a reference to a value in an enumeration, not a literal value you can store as a security protocol. The type returned by [Net.SecurityProtocolType]::Tls12 is an object of type "System.Net.SecurityProtocolType". If you examine the "value__" property of that object you'll find it's 3072.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.