-2

我正在尝试编写一个脚本,该脚本将获取一个包含指向文档的 URL 链接的文本文件并下载它们。我很难理解如何在 powershell 中传递参数和操作它们。这是我到目前为止得到的。我认为我应该使用参数方法来获取参数,这样我就可以在脚本中使用它,但是 $args 在面值上似乎更容易......如果有一点帮助,将不胜感激。**更新

$script = ($MyInvocation.MyCommand.Name)
$scriptName = ($MyInvocation.MyCommand.Name -replace "(.ps1)" , "")
$scriptPath = ($MyInvocation.MyCommand.Definition)
$scriptDirectory = ($scriptPath.Replace("$script" , ""))

## ##################################
## begin code for directory creation.
## ##################################

## creates a direcory based on the name of the script.
do {
    $scriptFolderTestPath = Test-Path $scriptDirectory\$scriptName -PathType container
    $scriptDocumentFolderTestPath = Test-Path $scriptFolder\$scriptName"_Script_Documents" -PathType container
    $scriptLogFolderTestPath = Test-Path $scriptFolder\$scriptName"_Script_Logs" -PathType container

    if ($scriptFolderTestPath -match "False") { 
        $scriptFolder = New-Item $scriptDirectory\$scriptName -ItemType directory
    }
    elseif ($scriptDocumentFolderTestPath -match "False") {
        New-Item $scriptFolder\$scriptName"_Script_Documents" -ItemType directory       
    }
    elseif ($scriptLogFolderTestPath -match "False") {
        New-Item $scriptFolder\$scriptName"_Script_Logs" -ItemType directory
    }
} Until (($scriptFolderTestPath -match "True") -and ($scriptDocumentFolderTestPath -match "True") -and ($scriptLogFolderTestPath -match "True"))

## variables for downloading and renaming code.
$date = (Get-Date -Format yyyy-MM-dd)

## ################################
## begin code for link downloading.
## ################################

## gets contents of the arguement variable.
Get-Content $linkList

## downloads the linked file.
Invoke-WebRequest $linkList

产生的错误

PS C:\Windows\system32> C:\Users\Steve\Desktop\Website_Download.ps1
cmdlet Website_Download.ps1 at command pipeline position 1
Supply values for the following parameters:
linkList: C:\Users\Steve\Desktop\linkList.txt


    Directory: C:\Users\Steve\Desktop\Website_Download


Mode                LastWriteTime     Length Name                                                                                     
----                -------------     ------ ----                                                                                     
d----        10/27/2012   3:59 PM            Website_Download_Script_Documents                                                        
d----        10/27/2012   3:59 PM            Website_Download_Script_Logs                                                             
Get-Content : Cannot find path 'C:\Users\Steve\Desktop\linkList.txt' because it does not exist.
At C:\Users\Steve\Desktop\Website_Download.ps1:42 char:1
+ Get-Content $linkList
+ ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Users\Steve\Desktop\linkList.txt:String) [Get-Content], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand

Invoke-WebRequest : Could not find file 'C:\Users\Steve\Desktop\linkList.txt'.
At C:\Users\Steve\Desktop\Website_Download.ps1:45 char:1
+ Invoke-WebRequest $linkList
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.FileWebRequest:FileWebRequest) [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
4

2 回答 2

3

与任何其他类型的参数相比,在 Powershell 中传递参数数组没有区别。请参阅此处了解它是如何完成的。考虑到你有一个文本文件,你不需要传递一个参数数组,只需要传递一个文件名,所以只是一个字符串。

我对您正在使用的 Powershell 3.0 没有任何经验(根据Invoke-WebRequest您的代码中是否存在),但我会从以下内容开始:

$URLFile = @"
http://www.google.ca
http://www.google.com
http://www.google.co.uk/
"@

$URLs = $URLFile -split "`n";
$savedPages = @();
foreach ($url in $URLs) {
    $savedPages += Invoke-WebRequest $url   
}

也就是说,您只有一个文件,都在一个地方,并确保您正确接收您的内容。不知道你为什么需要Start-BitsTransfer,因为Invoke-WebRequest它已经为你提供了页面内容。请注意,我没有对 做任何事情$savedPages,所以我的代码实际上是无用的。

之后, 的内容$URLFile进入文件,您将调用替换为

gc "Path_To_Your_File"`

如果仍然有效,$Path请在脚本中引入一个参数,如下所示:

param([string]$Path)

再次测试,以此类推。如果您是 Powershell 新手,请始终从较小的代码片段开始,并不断扩展以包含您需要的所有功能。如果你从一大块开始,很可能你永远不会完成。

于 2012-10-27T19:18:48.873 回答
1

通过 Neolisk 关于处理参数的链接解决了这个问题。然后在最后更改了一些代码以创建另一个变量并像往常一样处理事情。只是与传递参数有些混淆。

## parameter passed to the script.
param (
    [parameter(Position=0 , Mandatory=$true)]
    [string]$linkList
)

## variables for dynamic naming.
$script = ($MyInvocation.MyCommand.Name)
$scriptName = ($MyInvocation.MyCommand.Name -replace "(.ps1)" , "")
$scriptPath = ($MyInvocation.MyCommand.Definition)
$scriptDirectory = ($scriptPath.Replace("$script" , ""))

## ##################################
## begin code for directory creation.
## ##################################

## creates a direcory based on the name of the script.
do {
    $scriptFolderTestPath = Test-Path $scriptDirectory\$scriptName -PathType container
    $scriptDocumentFolderTestPath = Test-Path $scriptFolder\$scriptName"_Script_Documents" -PathType container
    $scriptLogFolderTestPath = Test-Path $scriptFolder\$scriptName"_Script_Logs" -PathType container

    if ($scriptFolderTestPath -match "False") { 
        $scriptFolder = New-Item $scriptDirectory\$scriptName -ItemType directory
    }
    elseif ($scriptDocumentFolderTestPath -match "False") {
        New-Item $scriptFolder\$scriptName"_Script_Documents" -ItemType directory       
    }
    elseif ($scriptLogFolderTestPath -match "False") {
        New-Item $scriptFolder\$scriptName"_Script_Logs" -ItemType directory
    }
} Until (($scriptFolderTestPath -match "True") -and ($scriptDocumentFolderTestPath -match "True") -and ($scriptLogFolderTestPath -match "True"))

## variables for downloading and renaming code.
$date = (Get-Date -Format yyyy-MM-dd)

## ################################
## begin code for link downloading.
## ################################

## gets contents of the arguement variable.
$webTargets = Get-Content $linkList

## downloads the linked file.
Invoke-WebRequest $webTargets
于 2012-10-27T21:07:38.237 回答