none
What wrong iis this powershell script? RRS feed

  • General discussion

  • Referring to following coding, when I manually input url into browser, file is available for download, but when I run following powershell, it fails to download.

    Does anyone have any suggestions on what wrong it is?
    Thanks in advance for any suggestions

    	$clnt = new-object System.Net.WebClient
    	$url = "http://www.censtatd.gov.hk/showtableexcel2.jsp?tableID=127&charsetID=2"
    	$file = "D:\File.xls"
    	try {
    		$request = [System.Net.WebRequest]::Create($url)
    		$request.Method = 'HEAD'
    		$response = $request.GetResponse()
    		$httpStatus = $response.StatusCode
    		$urlIsValid = ($httpStatus -eq 'OK')
    		$tryError = $null
    		$response.Close()
                    Write-Host "File is downloaded ... :>"
    		$clnt.DownloadFile($url,$file)
    	}
    	catch [System.Exception] {
    		$httpStatus = $null
    		$tryError = $_.Exception
    		$urlIsValid = $false;
    		Write-Host "There was an error downloading $file" 
    	}


    Thanks in advance for any suggestions

    • Changed type Bill_Stewart Monday, July 30, 2018 1:41 PM
    • Moved by Bill_Stewart Monday, July 30, 2018 1:42 PM Help vampire
    Thursday, June 7, 2018 4:01 AM

All replies

  • This is all you need to do:

    $url = 'http://www.censtatd.gov.hk/showtableexcel2.jsp?tableID=127&charsetID=2'
    $file = 'D:\test\File.xls'
    [System.Net.ServicePointManager]::SecurityProtocol+='tls12'
    $clnt = new-object System.Net.WebClient
    $clnt.DownloadFile($url,$file)
    


    \_(ツ)_/

    Thursday, June 7, 2018 4:18 AM
  • I receive an error message about not including "op_Addition"

    Do you have any suggestions on how to fix it?
    Thank you very much for any suggestions (^v^)



    Thanks in advance for any suggestions



    Thursday, June 7, 2018 4:30 AM
  • You language version does not support that.

    Try this:

    [System.Net.ServicePointManager]::SecurityProtocol += [System.Net.SecurityProtocolType]::Tls12


    \_(ツ)_/

    Thursday, June 7, 2018 4:49 AM
  • I still get the same error.

    Do you have any suggestions on how to fix it?
    Thank you very much for any suggestions (^v^)

    	[System.Net.ServicePointManager]::SecurityProtocol += [System.Net.SecurityProtocolType]::Tls12
    	$clnt = new-object System.Net.WebClient
    	$url = 'http://www.censtatd.gov.hk/showtableexcel2.jsp?tableID=127&charsetID=2'
    	$file = 'D:\Test\File.xls'
    	$clnt.DownloadFile($url,$file)
    


    Thanks in advance for any suggestions

    Thursday, June 7, 2018 5:12 AM
  • What version of PowerShell?


    \_(ツ)_/

    Thursday, June 7, 2018 5:36 AM
  • 2013 Microsoft Corporation


    Thanks in advance for any suggestions

    Thursday, June 7, 2018 2:19 PM
  • "PowerShell" and not Windows.


    \_(ツ)_/

    Thursday, June 7, 2018 2:21 PM
  • Version 4.0

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

    Major  Minor  Build  Revision
    -----  -----  -----  --------
    4      0      -1     -1

    Thanks in advance for any suggestions



    Thursday, June 7, 2018 2:30 PM
  • It appears that your version of Windows does not support TLS 1.2 which is required by the web site.


    \_(ツ)_/

    Thursday, June 7, 2018 2:38 PM
  • Could you please tell me on what version support TLS 1.2? and where to download?

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


    Thanks in advance for any suggestions

    Thursday, June 7, 2018 3:19 PM
  • Could you please tell me on what version support TLS 1.2? and where to download?

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


    Thanks in advance for any suggestions

    No idea.  You appear to have an international version of Windows.  Perhaps you are missing some patches or components.


    \_(ツ)_/

    Thursday, June 7, 2018 4:25 PM
  • Do you mean that my window are missing some patches or components? not powershell?

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


    Thanks in advance for any suggestions

    Friday, June 8, 2018 12:34 AM
  • Do you have any problem on downloading this file using powershell?

    If yes, what version of powershell and window do you use?

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


    Thanks in advance for any suggestions

    Friday, June 8, 2018 12:37 PM
  • This is not a break/fix forum for your issue.

    If you need further assistance, please hire a consultant.


    -- Bill Stewart [Bill_Stewart]

    Friday, June 8, 2018 1:41 PM
  • I just want to confirm on whether this case can be solved or not, if not, I would confirm and give up this issue, that is all.

    Thanks, to everyone very much for suggestions (^v^)


    Thanks in advance for any suggestions

    Friday, June 8, 2018 2:50 PM
  • This is not the right place to ask your question. Your question is missing way too much information. We are not your personal help desk.

    -- Bill Stewart [Bill_Stewart]


    Friday, June 8, 2018 3:47 PM
  • 

    Referring to following Powershell coding, I would like to know on why not able to download file?

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

        $clnt = new-object System.Net.WebClient
        $url = "http://www.hkma.gov.hk/media/eng/doc/market-data-and-statistics/monthly-statistical-bulletin/T030501.xls"
        $file = "C:\Temp\T030501.xls"
        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;
            Write-Host "Error"
        }


    Thanks in advance for any suggestions

    • Merged by Bill_Stewart Friday, August 10, 2018 1:56 PM Duplicate
    Friday, August 10, 2018 1:26 AM
  • Stop copying dopie pointless code from the Internet.  Learn how this actually works first.

    $url = 'http://www.hkma.gov.hk/media/eng/doc/market-data-and-statistics/monthly-statistical-bulletin/T030501.xls'
    [Net.ServicePointManager]::SecurityProtocol +='tls12'
    $filename = 'd:\test\T030501.xls'
    $wc = New-Object System.Net.WebClient
    $wc.DownloadFile($url,$filename)

    Works as expected.


    \_(ツ)_/

    Friday, August 10, 2018 2:09 AM
  • 

    Referring to following Powershell coding, I would like to know on why not able to download file?

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

        $clnt = new-object System.Net.WebClient
        $url = "http://www.hkma.gov.hk/media/eng/doc/market-data-and-statistics/monthly-statistical-bulletin/T030501.xls"
        $file = "C:\Temp\T030501.xls"
        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;
            Write-Host "Error"
        }


    Thanks in advance for any suggestions

    The code I posted is PowerShell coding.  Your code was copied from a years old method that is not longer required since Net has had the WebCLient class added.  Your code was only need in PS version1 which is now no longer in use or supported.


    \_(ツ)_/

    Friday, August 10, 2018 3:19 PM