1

I am using Windows 7. It has powershell Version 2 installed in it. But the version has some bug with split path. So I went with the System.IO.Path for a powershell downloading and executing script. But it is still showing error.

This is the error what I am getting :

+ @('https://cdn.bringatrailer.com/wp-content/uploads/2018/01/GTX-Low-Res-4-940
x627.jpg','https://upload.wikimedia.org/wikipedia/commons/thumb/1/1f/2019_Ford_
Mustang_GT_5.0_facelift.jpg/1200px-2019_Ford_Mustang_GT_5.0_facelift.jpg') |for
each{$fileName = $env:TEMP + ([System.IO.Path]::GetFileName($Path) );(New-Objec
t System.Net.WebClient).DownloadFile <<<< ($_,$fileName);Invoke-Item $fileName}
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException
Exception calling "DownloadFile" with "2" argument(s): "An exception occurred d
uring a WebClient request."
At line:1 char:351
+ @('https://cdn.bringatrailer.com/wp-content/uploads/2018/01/GTX-Low-Res-4-940
x627.jpg','https://upload.wikimedia.org/wikipedia/commons/thumb/1/1f/2019_Ford_
Mustang_GT_5.0_facelift.jpg/1200px-2019_Ford_Mustang_GT_5.0_facelift.jpg') |for
each{$fileName = $env:TEMP + ([System.IO.Path]::GetFileName($Path) );(New-Objec
t System.Net.WebClient).DownloadFile <<<< ($_,$fileName);Invoke-Item $fileName}
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

Here is my code that i tried to run :

@echo off


powershell "@('https://cdn.bringatrailer.com/wp-content/uploads/2018/01/GTX-Low-Res-4-940x627.jpg','https://upload.wikimedia.org/wikipedia/commons/thumb/1/1f/2019_Ford_Mustang_GT_5.0_facelift.jpg/1200px-2019_Ford_Mustang_GT_5.0_facelift.jpg') |foreach{$fileName = $env:TEMP + ([System.IO.Path]::GetFileName($Path) );(New-Object System.Net.WebClient).DownloadFile($_,$fileName);Invoke-Item $fileName}"

1 Answers1

0

PowerShell 2 is depreciated and no longer receiving any updates and has serious risk issues, compared to the current state of the industry and what the newer versions provide for control.

Win7 can use PowerShell v5x. I strongly encourage you to update and use the web cmdlets for this effort and take advantage of the advances in the newer versions.

Now on to you problem. Your have syntax issues, here, even the way you are using PowerShell v2.

@echo off --- is a cmd.exe .bat/.cmd thing, not PowerShell.

If you open this in the ISE or VSCode, you see it is show as an error. So, it's not needed.

For what you are after, take a look at this article: 3 ways to download files with PowerShell

# 1. Invoke-WebRequest

$url = "http://mirror.internode.on.net/pub/test/10meg.test"
$output = "$PSScriptRoot\10meg.test"
$start_time = Get-Date

Invoke-WebRequest -Uri $url -OutFile $output
Write-Output "Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)"


# 2. System.Net.WebClient

$url = "http://mirror.internode.on.net/pub/test/10meg.test"
$output = "$PSScriptRoot\10meg.test"
$start_time = Get-Date

$wc = New-Object System.Net.WebClient
$wc.DownloadFile($url, $output)

#Or

(New-Object System.Net.WebClient).DownloadFile($url, $output)
Write-Output "Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)"


# 3. Start-BitsTransfer

$url = "http://mirror.internode.on.net/pub/test/10meg.test"
$output = "$PSScriptRoot\10meg.test"
$start_time = Get-Date

Import-Module BitsTransfer
Start-BitsTransfer -Source $url -Destination $output

#Or

Start-BitsTransfer -Source $url -Destination $output -Asynchronous
Write-Output "Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)"

This would be my approach for your use case:

# DownloadPic.ps1

'https://cdn.bringatrailer.com/wp-content/uploads/2018/01/GTX-Low-Res-4-940x627.jpg',
'https://upload.wikimedia.org/wikipedia/commons/thumb/1/1f/2019_Ford_Mustang_GT_5.0_facelift.jpg/1200px-2019_Ford_Mustang_GT_5.0_facelift.jpg' | 
foreach{
    $webclient = New-Object System.Net.WebClient
    $filename = [System.IO.Path]::GetFileName($PSItem)
    $file = "E:\Temp\$filename"
    $webclient.DownloadFile($url,$file)
}

Yet, if you are trying to do the traditional double-click to run a file, which does not work with PowerShell natively, so, you are using a batch file for the double-click effort, then I get that.

You'd save the above as a .ps1 file, and in your batch file just call it, something like this.

DownloadPic.bat

::rem batch file calling powershell to download pics.
@echo off
Powershell -noprofile -File "%~E:\temp\DownloadPic.ps1"

See this Q&A

... and point of note. I just wrote this up quickly and tested and it downloaded the pics as expected.

Yet, you can just start a PowerShell prompt and run the script directly.

postanote
  • 15,138
  • 2
  • 14
  • 25