Powershell download file not working properly
15,602
$DevDownloadDirectory = [IO.Directory]::GetCurrentDirectory
Should be
$DevDownloadDirectory = [IO.Directory]::GetCurrentDirectory()
GetCurrentDirectory() is a Method and if you dont use the "()", it will just return the same name but not the current directory.
Author by
th3flyboy
Updated on June 04, 2022Comments
-
th3flyboy almost 2 years
I am trying to write a powershell script that will set a download directory variable based on the current directory, and download a file to that directory.
The code I have is:
cd downloads $DevDownloadDirectory = [IO.Directory]::GetCurrentDirectory $clnt = New-Object System.Net.WebClient # download and extract the file $url = “fileurl/file.zip" $file = "$DevDownloadDirectory\file.zip" $clnt.DownloadFile($url,$file)
The problem I get is whenever I get to this part of the code it pumps out:
Exception calling "DownloadFile" with "2" argument(s): "An exception occurred during a WebClient request." At C:\directory\script.ps1:462 char:20
- $clnt.DownloadFile <<<< ($url,$file)
- CategoryInfo : NotSpecified: (:) [], MethodInvocationException
- FullyQualifiedErrorId : DotNetMethodException
Could anyone please help me figure out why this is happening?