2013-05-21 103 views
0

我試圖從使用PowerShell的RSS源解析出數據。Powershell - 您如何解析內容:從RSS提要(XML)編碼?

如何獲取標題,guid和內容:編碼字段的內容?

由於某種原因,我下面的代碼只是返回「...」。

任何幫助非常感謝!

[xml]$hsg = Invoke-WebRequest http://technet.microsoft.com/en-us/security/rss/comprehensive 
#$hsg.rss.channel.item | select title #this prints the list of blog posts 

$ContentNamespace = New-Object Xml.XmlNamespaceManager $hsg.NameTable 
$ContentNamespace.AddNamespace("content", "http://purl.org/rss/1.0/modules/content/") 

#$hsg.rss.channel.item #this prints the list of posts 

$hsg.rss.channel.item.selectSingleNode("content:encoded", $ContentNamespace) 

的數據是這樣的:

<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rssdatehelper="urn:rssdatehelper" version="2.0"> 
<channel> 
<title>Microsoft Security Content: Comprehensive Edition</title> 
<link>http://technet.microsoft.com/security/bulletin</link> 
<dc:date>Wed, 15 May 2013 08:00:00 GMT</dc:date> 
<generator>umbraco</generator> 
<description>Microsoft Security Content: Comprehensive Edition</description> 
<language>en-US</language> 
<item> 
<title> 
MS13-045 - Important : Vulnerability in Windows Essentials Could Allow Information Disclosure (2813707) - Version: 1.1 
</title> 
<link> 
http://technet.microsoft.com/en-us/security/bulletin/ms13-045 
</link> 
<dc:date>2013-05-15T07:00:00.0000000Z</dc:date> 
<guid> 
http://technet.microsoft.com/en-us/security/bulletin/ms13-045 
</guid> 
<content:encoded> 
<![CDATA[ 
Severity Rating: Important<br /> 
Revision Note: V1.1 (May 15, 2013): Corrected link to the download location in the Detection and Deployment Tools and Guidance section. This is an informational change only.<br /> 
Summary: This security update resolves a privately reported vulnerability in Windows Writer. The vulnerability could allow information disclosure if a user opens Writer using a specially crafted URL. An attacker who successfully exploited the vulnerability could override Windows Writer proxy settings and overwrite files accessible to the user on the target system. In a web-based attack scenario, a website could contain a specially crafted link that is used to exploit this vulnerability. An attacker would have to convince users to visit the website and open the specially crafted link. 
]]> 
</content:encoded> 
</item> 

謝謝!

回答

3

試試這個:解析數據的

$rss = [xml](Get-Content .\test.rss) 
$rss.SelectNodes('//item') | % { 
    $posts += New-Object psobject -Property @{ 
     Title = $_.Title.Trim() 
     Guid = $_.Guid.Trim() 
     Content = $_.Encoded."#cdata-section".Trim() 
    } 
} 

樣品(數組只包含一個項目,因爲那裏不僅是一個你的樣品中):

$posts 

Title        Guid        Content       
-----        ----        -------       
MS13-045 - Important : Vulnera... http://technet.microsoft.com/... Severity Rating: Important<br... 

順便說一句,你的樣品缺乏在以下結尾:

</channel> 
</rss> 
+0

這樣做!謝謝,@Graimer! – mbourgon

0

您可以繞過將輸出寫入中間文件,跳過獲取內容。弗羅德把它很好地包裝成了一個提供解決方案的psobject。

cls 
$x=[xml](iwr 'https://technet.microsoft.com/en-us/security/rss/comprehensive').content 
foreach ($y in $x.rss.channel.selectnodes('//item')) { 
"`r`n`t$($y.title)" 

$y.pubdate 
$y.link 
$y.encoded.'#cdata-section' 

} 

你可能會發現你的RSS/ATOM返回一個略微不同的結構,我發現這個必要針對不同的飼料:

foreach ($y in $x.feed.entry) 

IDE中的智能感知幫助我導航。