Hi Basically i'm having an issue i need to load Arguments in to the following code located at the $URL
& $Output
but i need it to work with the Join-Path
but every time i try it kicks back a load of errors i'm new to this so i am trying everything to get it working I've been asked to do this by my boss completely out my depth but never been one to shy away from a challenge
Function DownloadFileFromURL
{
Add-Type -AssemblyName Microsoft.Visualbasic
#$url = 'http://download.microsoft.com/download/F/4/2/F42AB12D-C935-4E65-9D98-4E56F9ACBC8E/wpilauncher.exe'
$url = $args[0]
$filename = Split-Path -leaf $url
$output = Join-Path $args1 + $filename
$response = [System.Net.WebRequest]::Create($url).GetResponse()
$realurl = $response.ResponseUri.OriginalString
$response.Close()
(New-Object Net.WebClient).DownloadFile($url, $output)
#################Time Taken To Download Files######################
$start_time = Get-Date
Write-Output "Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)"
}
DownloadFileFromURL ('http://download.microsoft.com/download/F/4/2/F42AB12D-C935-4E65-9D98-4E56F9ACBC8E/wpilauncher.exe','C:\Users\Martin.beardmore\Downloads\test')
Errors being Recieved
Join-Path : Cannot bind argument to parameter 'Path' because it is null.
At C:\Users\Martin.beardmore\Documents\Query download.ps1:6 char:21
+ $output = Join-Path $args[1] + $filename
+ ~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Join-Path], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.JoinPathCommand
Cannot convert argument "requestUri", with value: "System.Object[]", for "Create" to type "System.Uri": "Cannot convert the "System.Object[]" value of type "System.Object[]" to type
"System.Uri"."
At C:\Users\Martin.beardmore\Documents\Query download.ps1:7 char:1
+ $response = [System.Net.WebRequest]::Create($url).GetResponse()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument
You cannot call a method on a null-valued expression.
At C:\Users\Martin.beardmore\Documents\Query download.ps1:9 char:1
+ $response.Close()
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Cannot convert argument "address", with value: "System.Object[]", for "DownloadFile" to type "System.Uri": "Cannot convert the "System.Object[]" value of type "System.Object[]" to type
"System.Uri"."
At C:\Users\Martin.beardmore\Documents\Query download.ps1:10 char:1
+ (New-Object Net.WebClient).DownloadFile($url, $output)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument
You need to remove the
+
between the paths you want to join. Look at the syntax forJoin-Path
.You should use it like
$output = Join-Path $args1 $filename
Also, where does
$args1
come from? In functions, you should name your parameters. The order in which you define them will be the order they are binded to, so they will still behave the same as arguments, but they are easier to work with.Sample:
You are also calling the function wrong. Comma is used to seperate objects in an array, so
DownloadFileFromURL ('http://download.microsoft.com/download/F/4/2/F42AB12D-C935-4E65-9D98-4E56F9ACBC8E/wpilauncher.exe','C:\Users\Martin.beardmore\Downloads\test')
results in:Modify your function to use parameters and call it like I've as shown above and it should work.
Updated script: