2012-01-27 64 views
1

首先,是的,我是脚本和PowerShell的noob,因为这将变得非常明显。在我的努力学习,我一直与一个脚本从这里:http://www.leeholmes.com/blog/2010/03/05/html-agility-pack-rocks-your-screen-scraping-world/使用powershell和htmlAgilityPack收集输出

这是我迄今:

add-type -Path 'C:\Program Files (x86)\htmlAgilityPack\HtmlAgilityPack.dll' 
$doc = New-Object HtmlAgilityPack.HtmlDocument 
$result = $doc.Load("C:\scipts\test.html") 
$texts = $doc.DocumentNode.SelectNodes("//table[@class='dataTable']/tbody/tr/td[1]/a[1]") 
$result = $texts | % { 
$testText = $_ 
$Platform = $testtext.innertext 
New-Object PsObject -Property @{ Platform = $Platform} | Select Platform 
} 
$result | Sort Platform | out-string 

这给了我,我要找的项目。

*挑战*

我试图让输出到一个变量为一个字符串与每个项目后面跟一个逗号。对于示例Item1,Item2,Item 3等...

任何帮助或解释将不胜感激,因为我已经谷歌Google眼镜,并不一定忍受我找到的东西。

谢谢!

+0

我不知道是否有任何内置运营商PowerShell来做到这一点,但['的string.join()'](http://msdn.microsoft.com/ EN-US /库/ system.string.join.aspx)。 – 2012-01-27 16:28:12

+0

我曾尝试使用powershell变体的-split和-join以取得任何成功 – jsan 2012-01-28 00:45:58

回答

2

从PowerShell帮助(get-help about_join):

下图显示了连接操作的语法。

<String[]> -Join <Delimiter> 

解决你的问题,你需要:

  1. 创建一个包含您要加入的字符串字符串数组:

    $arrayofstrings = $result | Sort Platform | foreach-object {$_.Platform} 
    
  2. 使用连接字符串逗号作为分隔符:

    $joinedstring = $arrayofstrings -join ", " 
    

$joinedstring将包含Item1, Item2, Item3

+0

我可能会使用'$ result |选择-exp Platform | sort'。 – Joey 2012-01-28 09:06:43

+0

工作!非常感谢Jon。我以前曾尝试加入,但我没有正确使用它或在正确的地方使用它。 – jsan 2012-01-28 15:22:50