Following an answer here. I am doing the following:
param(
# Required. Download source location (website).
[Parameter(Mandatory=$True)]
[URI] $URL
)
process
{
$wc = New-Object System.Net.WebClient;
$fileItems = $wc.downloadstring($URL) -split "<a\s+" | %{ [void]($_ -match "^HREF=[`'`"] ([^`'`">\s]*)"); $matches[1] };
return $fileItems;
}
I am getting the following error:
ForEach-Object : Cannot index into a null array.
At J:\LMIData\PowerShell\HTTP_based_downloader.ps1:9 char:61
+ $fileItems = $wc.downloadstring($URL) -split "<a\s+" | % <<<< { [void]($_ -match "^HREF=[`'`"]([^`'`">\s]*)"); $matches[1]; };
+ CategoryInfo : InvalidOperation: (1:Int32) [ForEach-Object], RuntimeException
+ FullyQualifiedErrorId : NullArray,Microsoft.PowerShell.Commands.ForEachObjectCommand
The "char:61" location is right after the %.
Now, what is interesting is that this code worked previously. Is this possibly an issue in the -match regex? I have not declared the $matches array - could that be it?
I am at a loss here as I am still new to powershell. I am aware that I can get the list of links in powershell v3+ using Invoke-WebRequest but that is not an option as the system this is running on is locked at v2 for the time being.