question

oemScript avatar image
0 Votes"
oemScript asked AndreasBaumgarten commented

How to code this powershell script?

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
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @oemScript-8271 ,

Did the answer work for you? Are there any additional questions to this topic?

If you found the answer helpful, it would be great if you please mark it "Accept as answer". This will help others to find answers in Q&A

----------
Regards
Andreas Baumgarten

0 Votes 0 ·
AndreasBaumgarten avatar image
0 Votes"
AndreasBaumgarten answered AndreasBaumgarten edited

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://docs.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://docs.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://docs.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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

oemScript avatar image
0 Votes"
oemScript answered IanXue-MSFT edited

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" 
 }



error.png (21.7 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

You typed the wrong extension so the script was not found. The last character is the number "1", not "L".

0 Votes 0 ·