1

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.

1
  • 3
    You should inspect the inner exception in PowerShell using a try/catch (see here). I suspect the problem is that you don't have permission to write to the root folder on the Windows 8 machine unless you run as administrator. Commented Mar 30, 2015 at 23:18

2 Answers 2

1

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
'**********************************************************************************************
Sign up to request clarification or add additional context in comments.

Comments

0

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

3 Comments

"invoke-webrequest" takes much more time comparatively, and about other way..its working in server 2012 but not in windows 8. tried exact same way u suggested.
maby in win8 you don't have root access to write file make directory like download then second parameter must be "c:\download\pqr.exe" just test it
how to track $a.DownloadFile().. i mean how to check if download is complete or not through powershell. or does $a.downloadfile() returns any value..??

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.