PowerShell 3 Parameters

1.3k views Asked by At

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
1

There are 1 answers

0
Frode F. On

You need to remove the + between the paths you want to join. Look at the syntax for Join-Path.

PS> Get-Command Join-Path -Syntax

Join-Path [-Path] <string[]> [-ChildPath] <string> [-Resolve] [-Credential <pscredential>] [-UseTransaction] [<CommonParameters>]

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.

Function DownloadFileFromURL ($url, $folder)
{
    Write-Host "URL: $url"
    Write-Host "Folder: $folder"
}

Sample:

PS > DownloadFileFromURL stackoverflow.com c:\folder
URL: stackoverflow.com
Folder: c:\folder

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:

$url = $args[0] = @('http://download.microsoft.com/download/F/4/2/F42AB12D-C935-4E65-9D98-4E56F9ACBC8E/wpilauncher.exe','C:\Users\Martin.beardmore\Downloads\test')

Modify your function to use parameters and call it like I've as shown above and it should work.

Updated script:

Function DownloadFileFromURL ($url, $destination)
{
    Add-Type -AssemblyName Microsoft.Visualbasic
    $filename = Split-Path -Leaf $url
    $output = Join-Path $destination $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'
#or even better
#DownloadFileFromURL -url 'http://download.microsoft.com/download/F/4/2/F42AB12D-C935-4E65-9D98-4E56F9ACBC8E/wpilauncher.exe' -destination 'C:\Users\Martin.beardmore\Downloads\test'