2012-10-27 48 views
-2

我正在尝试编写一个脚本,该脚本将包含指向文档的URL链接的文本文件并下载它们。我很难理解如何传递参数并在PowerShell中操作它们。这是我到目前为止。我认为我应该使用param方法来接受一个参数,这样我就可以为脚本要求它了,但是$ args在面值上似乎更容易一些......有一点帮助会让人感激不已。 ** UPDATE将一组URL作为参数传递给Powershell

$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 

回答

2

有在通过在Powershell的参数数组,相比于任何其他类型的参数没有差异。 See here它是如何完成的。考虑到你有一个文本文件,你不需要传递一个参数数组,只需要传递一个文件名,所以只需要一个字符串。

我没有使用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的新手,请始终从较小的代码片段开始,并不断发展以包含您所需的全部功能。如果你从一个大块开始,你有可能永远不会完成。

+0

感谢您的回复。尽管这对我来说没有意义。为什么我不能只传递一个带有URL的txt文件作为参数,并使用get-content将每行作为对象并将它们传递给Invoke-Webrequest cmdlet?我正在更新我的主帖子,添加了一个参数部分以及由此产生的错误。 – Steve

+0

@Steve:我建议你一个出发点。忘记外部文件,忘记参数。确保您的单个文件代码按预期工作。是的,然后你会传递一个带有URL的txt文件作为参数,但是你不需要对URL进行获取内容。假设文件中的每一行都是一个URL,你可以用换行符分隔,然后输入Invoke-Webrequest,这正是我在我的例子中所做的。 – Neolisk

+0

我不喜欢这个起点,因为它更像是一个perl脚本。我不应该解析txt文件。如果我想手动执行此操作,它将看起来像这样。 '$ links =“C:\ Users \ Steve \ Desktop \ linkList.txt” $ results =“C:\ Users \ Steve \ Desktop \ Results” $ crawl = get-content $ links Invoke-WebRequest $抓取' – Steve

1

通过Neolisk关于处理参数的链接找出了这一点。然后在最后改变了一些代码来创建另一个变量并按照我通常的方式处理事情。只是通过params混淆。

## 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 
+0

它实际上适合你吗? – Neolisk

+0

它确实抓住传递的参数并且拉动invoke-webrequest。如果参数有多行添加'Get-Content $ linkList | ForEach-Object {Invoke-WebRequest $ _}'无论出于何种原因,我只是没有正确传递参数,直到阅读该文章并改变它。我所有使用$ args的尝试都失败了。 – Steve