locked
How to code this powershell? RRS feed

  • Question

  • I would like to know on what wrong is following coding not able to download file.

    Does anyone have any suggestions?
    Thanks in advance for any suggestions

        $clnt = new-object System.Net.WebClient
        $url = "https://www.hkex.com.hk/chi/services/trading/securities/securitieslists/ListOfSecurities_c.xlsx"
        $file = "D:\Sample.xlsx"
        try {
            $request = [System.Net.WebRequest]::Create($url)
            $request.Method = 'HEAD'
            $response = $request.GetResponse()
            $httpStatus = $response.StatusCode
            $urlIsValid = ($httpStatus -eq 'OK')
            $tryError = $null
            $response.Close()
            $clnt.DownloadFile($url,$file)    }
        catch [System.Exception] {
            $httpStatus = $null
            $tryError = $_.Exception
            $urlIsValid = $false;


    Thanks in advance for any suggestions

    Monday, March 12, 2018 2:35 AM

Answers

  • Try using PowerShell.  It works well:

    $wc = New-Object system.net.webclient
    [Net.ServicePointManager]::SecurityProtocol = 'Tls12'
    $wc.DownloadFile('https://www.hkex.com.hk/chi/services/trading/securities/securitieslists/ListOfSecurities_c.xlsx','D:/test/ListOfSecurities_c.xlsx')
    

    Copying old C# code will not always work well.


    \_(ツ)_/

    • Proposed as answer by GouravIN Monday, March 12, 2018 4:36 AM
    • Marked as answer by oemMicrosoft Monday, March 12, 2018 5:06 AM
    Monday, March 12, 2018 3:14 AM

All replies

  • Try using PowerShell.  It works well:

    $wc = New-Object system.net.webclient
    [Net.ServicePointManager]::SecurityProtocol = 'Tls12'
    $wc.DownloadFile('https://www.hkex.com.hk/chi/services/trading/securities/securitieslists/ListOfSecurities_c.xlsx','D:/test/ListOfSecurities_c.xlsx')
    

    Copying old C# code will not always work well.


    \_(ツ)_/

    • Proposed as answer by GouravIN Monday, March 12, 2018 4:36 AM
    • Marked as answer by oemMicrosoft Monday, March 12, 2018 5:06 AM
    Monday, March 12, 2018 3:14 AM
  • It works with following coding, and I would like to know on where to find web site requiring following security protocol.

        [Net.ServicePointManager]::SecurityProtocol = 'Tls12'

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


    Thanks in advance for any suggestions


    Monday, March 12, 2018 4:32 AM
  • Monday, March 12, 2018 9:28 AM
  • Because it is using https, so it requires [Net.ServicePointManager]::SecurityProtocol = 'Tls12'.

    Would it be correct?

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


    Thanks in advance for any suggestions


    Monday, March 12, 2018 11:19 AM
  • https is blanket.  It is the web/client who negotiate the highest secure connection.  Some sites only support tls12.  The default for the web client is 'SSl3,Tls'.  We can also just add to the default:

    [Net.ServicePointManager]::SecurityProtocol += 'tls12'

    Which will allow those protocols to all be negotiated.


    \_(ツ)_/

    Monday, March 12, 2018 11:39 AM