我正在尝试编写一个脚本,该脚本将包含指向文档的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
感谢您的回复。尽管这对我来说没有意义。为什么我不能只传递一个带有URL的txt文件作为参数,并使用get-content将每行作为对象并将它们传递给Invoke-Webrequest cmdlet?我正在更新我的主帖子,添加了一个参数部分以及由此产生的错误。 – Steve
@Steve:我建议你一个出发点。忘记外部文件,忘记参数。确保您的单个文件代码按预期工作。是的,然后你会传递一个带有URL的txt文件作为参数,但是你不需要对URL进行获取内容。假设文件中的每一行都是一个URL,你可以用换行符分隔,然后输入Invoke-Webrequest,这正是我在我的例子中所做的。 – Neolisk
我不喜欢这个起点,因为它更像是一个perl脚本。我不应该解析txt文件。如果我想手动执行此操作,它将看起来像这样。 '$ links =“C:\ Users \ Steve \ Desktop \ linkList.txt” $ results =“C:\ Users \ Steve \ Desktop \ Results” $ crawl = get-content $ links Invoke-WebRequest $抓取' – Steve