PowerShell 3参数

时间:2015-06-08 15:35:02

标签: powershell powershell-v3.0 powershell-ise

嗨基本上我有一个问题我需要将参数加载到位于$URL&的下列代码中。 $Output但是我需要它与Join-Path一起使用,但每次尝试都会重新解决一系列错误,我会对此感到陌生,所以我正在尝试一切以使其正常工作我&#39我的老板已经完全要求我这样做了,但从来没有一个人回避挑战

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

收到的错误

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 个答案:

答案 0 :(得分:2)

您需要删除要加入的路径之间的+。查看Join-Path的语法。

PS> Get-Command Join-Path -Syntax

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

您应该像$output = Join-Path $args1 $filename

一样使用它

此外,$args1来自哪里?在函数中,您应该为参数命名。您定义它们的顺序将是它们绑定到的顺序,因此它们的行为仍然与参数相同,但它们更易于使用。

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

样品:

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

您也在调用该函数错误。逗号用于分隔数组中的对象,因此DownloadFileFromURL ('http://download.microsoft.com/download/F/4/2/F42AB12D-C935-4E65-9D98-4E56F9ACBC8E/wpilauncher.exe','C:\Users\Martin.beardmore\Downloads\test')会导致:

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

修改你的功能以使用参数并像上面所示那样调用它,它应该可以工作。

更新了脚本:

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'