Reputation: 775
The command is working fine with Windows Server 2012 (PowerShell 4.0), but it is not working with Windows 8 (PowerShell 4.0) for the same.
I want to download a file from an IIS server.
(New-Object System.Net.WebClient).DownloadFile('http://server12/vdir/v.exe','C:\pqr.exe')
I tried this. It worked fine for me on Windows Server 2012, but on Windows 8 it gave me a method invocation error:
Exception calling "DownloadFile" with "2" argument(s): "An exception occurred during a WebClient request.
Upvotes: 1
Views: 5182
Reputation: 18827
I'm working on Windows 7 (32 bits), and I got the same error like you, so, I wrote a VBScript script that generates a PowerShell script in order to download this file in the temporary folder and execute it, and it works fine for me.
DownloadPSVBS.vbs
Option Explicit
Dim URL,Ws,ByPassPSFile,PSFile,MyCmd,Result
URL = "http://hackoo.alwaysdata.net/Matrix.mp3"
Set Ws = CreateObject("wscript.Shell")
PSFile = Left(Wscript.ScriptFullName, InstrRev(Wscript.ScriptFullName, ".")) & "ps1"
ByPassPSFile = "PowerShell.exe -ExecutionPolicy bypass -noprofile -file "
MyCmd = "$source = " & DblQuote(URL) & VbCrlF
MyCmd = MyCmd & "$Filename = [System.IO.Path]::GetFileName($source)" & VbCrlF
MyCmd = MyCmd & "$dest = " & DblQuote("$env:temp\$Filename") & VbCrlF
MyCmd = MyCmd & "$wc = New-Object System.Net.WebClient" & VbCrlF
MyCmd = MyCmd & "$wc.DownloadFile($source,$dest)" & VbCrlF
MyCmd = MyCmd & "Start-Process $dest"
Call WriteMyPSFile(MyCmd)
Result = Ws.run(ByPassPSFile & PSFile,0,True)
'**********************************************************************************************
Sub WriteMyPSFile(strText)
Dim fs,ts,PSFile
Const ForWriting = 2
PSFile = Left(Wscript.ScriptFullName, InstrRev(Wscript.ScriptFullName, ".")) & "ps1"
Set fs = CreateObject("Scripting.FileSystemObject")
Set ts = fs.OpenTextFile(PSFile,ForWriting,True)
ts.WriteLine strText
ts.Close
End Sub
'**********************************************************************************************
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'**********************************************************************************************
Upvotes: 1
Reputation: 837
For downloading a file with PowerShell you can use Invoke-WebRequest:
mkdir c:\download
Invoke-WebRequest http://server12/vdir/v.exe -OutFile c:\download\pqr.exe
Or use it like this:
mkdir C:\download
$a = New-Object System.Net.WebClient
$a.DownloadFile("http://server12/vdir/v.exe","c:\download\pqr.exe")
Upvotes: 0