遇到非破坏性错误的问题

时间:2016-08-10 19:03:37

标签: powershell

首先关闭我的代码似乎每次运行时都能正常运行,即使我收到错误。我很想知道发生了什么以及如何解决它。

我的代码用于从一系列网站链接中抓取元数据。

非破坏性错误:

Cannot index into a null array.
At C:\test\websiteScrape.ps1:127 char:5
+     $List += [pscustomobject]@{
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray

代码:

$web = New-Object Net.WebClient
$web | Get-Member

function getMetaData($Array){
$fullArray = @()

foreach ($element in $Array){

    $metaString = $web.DownloadString($element)

    $metaArray = $metaString| Select-String -AllMatches '(meta name=".*?".+")|(a lang="fr" href=".*?")'  | % { $_.Matches } | % { $_.Value } 
       select -expa matches | select -expa value

       $fullArray += ,($element,$metaArray) 
       }

return $fullArray
}
#$Array is a System.Array and it holds a bunch of strings"links" to a website. 

$metaData = getMetaData $Array

$List = @()
for ($i=0; $i -le $metaData.length; $i++){

   $List += [pscustomobject]@{

   PageName = $metaData[$i][0]

   Description = [regex]::Replace($metaData[$i][1][1], 'meta name=".*?" content="(.*?)"', '$1');

   Creator = [regex]::Replace($metaData[$i][1][2], 'meta name=".*?" content="(.*?)"', '$1');

   Instituation = [regex]::Replace($metaData[$i][1][3], 'meta name=".*?" content="(.*?)"', '$1');

   Languague = [regex]::Replace($metaData[$i][1][4], 'meta name=".*?" content="(.*?)"', '$1');

   Subject =[regex]::Replace($metaData[$i][1][5], 'meta name=".*?" content="(.*?)"', '$1');

   Indentifier= [regex]::Replace($metaData[$i][1][6], 'meta name=".*?" content="(.*?)"', '$1');
   } 
}
List| Select-Object -Property PageName, Description| Export-Csv -path C:\Desktop\urlsAndMetaData.csv -NoTypeInformation  

0 个答案:

没有答案