How to code this powershell script?

oemScript 81 Reputation points
2021-04-01T23:12:31.54+00:00

Referring to following code, I would like to know on why not able to download the zip file. If I download manually by placing URL into browser, file can be downloaded without any problem.

Does anyone have any suggestions?
Thanks in advance

$url = "https://www.hkex.com.hk/eng/stat/dmstat/dayrpt/dqe210401.zip"
    $file = "D:\Sample.zip"
    [Net.ServicePointManager]::SecurityProtocol = 'Tls12'
    Invoke-WebRequest $url -OutFile $file

    If ((Test-Path $file) -and (Get-Item $file).length -gt 5kb) {break}

    catch [System.Exception] {
        $httpStatus = $null
        $tryError = $_.Exception
        $urlIsValid = $false;
        Write-Host "There was an error downloading $file" 

    }
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,355 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Andreas Baumgarten 95,496 Reputation points MVP
    2021-04-02T00:30:41.693+00:00

    Hi @oemScript-8271 ,

    this is working here:

    # This is working in PowerShell 5.1.19041.868  
    $url = "https://www.hkex.com.hk/eng/stat/dmstat/dayrpt/dqe210401.zip"  
    $file = "Sample.zip"  
    Invoke-WebRequest $url -OutFile $file # Download file  
    # Verify file  
    If ((Get-Item $file).length -gt 5kb) {  
        Write-Output "Filesize of $file is greater than 5kb"   
        }  
    else {Write-Output "There was an error downloading $file"}  
    

    Not sure if you posted the full script. The Catch in your script without a Try doesn't make sense or better should not work at all.
    https://learn.microsoft.com/de-de/powershell/module/microsoft.powershell.core/about/about_try_catch_finally?view=powershell-7.1
    Break will break out loops like foreach, for, while, do, switch or trap.
    https://learn.microsoft.com/de-de/powershell/module/microsoft.powershell.core/about/about_break?view=powershell-7.1
    I would recommend using Return to terminate a PowerShell script.
    https://learn.microsoft.com/de-de/powershell/module/microsoft.powershell.core/about/about_return?view=powershell-7.1

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

  2. Ian Xue (Shanghai Wicresoft Co., Ltd.) 29,486 Reputation points Microsoft Vendor
    2021-04-02T02:32:24.867+00:00

    Hi,

    The try block is missing in your script. Please check to see if this works

    $url = "https://www.hkex.com.hk/eng/stat/dmstat/dayrpt/dqe210401t.zip"  
    $file = "D:\Sample.zip"  
    try{    
        if ((Test-Path $file) -and (Get-Item $file).length -gt 5kb) {  
            write-host "$file already exists."  
        }  
        else  
        {  
            [Net.ServicePointManager]::SecurityProtocol = 'Tls12'  
            Invoke-WebRequest $url -OutFile $file -ErrorAction Stop  
            Write-Host "$file has been downloaded."  
        }  
    }         
    catch [System.Exception] {  
        $httpStatus = $null  
        $tryError = $_.Exception  
        $urlIsValid = $false;  
        Write-Host "There was an error downloading $file"   
    }  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  3. oemScript 81 Reputation points
    2021-04-02T06:30:18.787+00:00

    Referring to following error image, I would like to know on what error is that, do I need to use the latest powershell?

    Furthermore, I would like to confirm on running powershell command

    1) .\TestData.psl (last char is lower cap "L")
    2) .\TestData.ps1 (last char is "1" or One)

    83839-error.png

    Do you have any suggestions?
    Thanks, to everyone very much for any suggestions (^v^)

    $url = "https://www.hkex.com.hk/eng/stat/dmstat/dayrpt/dqe210401.zip"  
    $file = "D:\Sample.zip"  
    try{    
        if ((Test-Path $file) -and (Get-Item $file).length -gt 5kb) {  
       write-host "$file already exists."  
        }  
        else  
        {  
       [Net.ServicePointManager]::SecurityProtocol = 'Tls12'  
       Invoke-WebRequest $url -OutFile $file -ErrorAction Stop  
       Write-Host "$file has been downloaded."  
        }  
    }    
    catch [System.Exception] {  
        $httpStatus = $null  
        $tryError = $_.Exception  
        $urlIsValid = $false;  
        Write-Host "There was an error downloading $file"   
    }