1

該命令與Windows Server 2012(PowerShell 4.0)正常工作,但它不能與Windows   8(PowerShell 4.0)一起使用。使用PowerShell命令行開關下載文件

我想從IIS服務器下載文件。

(New-Object System.Net.WebClient).DownloadFile('http://server12/vdir/v.exe','C:\pqr.exe') 

我試過了。它工作得很好,我在Windows Server 2012上,但在Windows   8它給了我一個方法調用錯誤:

Exception calling "DownloadFile" with "2" argument(s): "An exception occurred during a WebClient request.

+3

您應檢查使用try/catch語句在PowerShell中的內部異常(見[這裏](http://tasteofpowershell.blogspot.com/2011/10/innerexception-我們具備的,到去-deeper.html))。我懷疑問題在於,除非以管理員身份運行,否則您無權寫入Windows 8計算機上的根文件夾。 – 2015-03-30 23:18:21

回答

0

對於下載文件使用PowerShell可以使用Invoke-WebRequest

mkdir c:\download 
Invoke-WebRequest http://server12/vdir/v.exe -OutFile c:\download\pqr.exe 

或者使用它是這樣的:

mkdir C:\download 
$a = New-Object System.Net.WebClient 
$a.DownloadFile("http://server12/vdir/v.exe","c:\download\pqr.exe") 
+0

「invoke-webrequest」比較花費更多時間,並且關於其他方式..它在服務器2012中工作,但不在Windows 8中。嘗試完全相同的方式,你建議。 – 2015-03-30 23:01:22

+0

maby in win8你沒有root權限寫文件make目錄像下載那麼第二個參數必須是「c:\ download \ pqr.exe」只是測試它 – powershell 2015-03-30 23:14:21

+0

如何跟蹤$ a.DownloadFile()..我的意思是如何通過PowerShell檢查下載是否完成。或$ a.downloadfile()返回任何值.. ?? – 2015-04-27 14:47:15

1

我工作在Windows 7  (32位),並且我得到了同樣的錯誤喜歡你,所以,我寫了一個VBScript生成PowerShell腳本的腳本,以便將該文件下載到臨時文件夾中並執行它,並且它對我來說工作正常。

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 
'********************************************************************************************** 
相關問題