I trying to write a script that can pull information from a webpage that only contains input that the user provided.
I am using the news site independents rss feed to parse.
$url = 'http://www.independent.co.uk/news/uk/rss'
Invoke-RestMethod -Uri $url -OutFile c:\Scripts\worldnews.xml
[xml]$Content = Get-Content C:\Scripts\worldnews.xml
$Feed = $Content.rss.channel
# User Input field
$UserTerm = Read-Host 'Enter a Term'
ForEach ($msg in $Feed.Item){
[PSCustomObject]@{
'Source' = "News"
'Title' = $msg.title
'Link' = $msg.link
'Description' = $msg.description
}#EndPSCustomObject
}#EndForEach
what do i need to add so that this script will only display results that include the input that the user has given? e.g. if the user types 'Police' in the user input then the script will only display the articles that have 'Police' written in the title.
I have tried an if statement but i'm unsure on the correct syntax
if(msg.title -match $UserTerm) {
How can i get this to work?
You can perform a where clause on the title in your foreach loop, like so:
$url = 'http://www.independent.co.uk/news/uk/rss'
Invoke-RestMethod -Uri $url -OutFile c:\scripts\worldnews.xml
[xml]$Content = Get-Content C:\scripts\worldnews.xml
$Feed = $Content.rss.channel
# User Input field
$UserTerm = Read-Host 'Enter a Term'
ForEach ($msg in $Feed.Item | ?{$_.Title.Contains($userTerm)})
{
[PSCustomObject]@{
'Source' = "News"
'Title' = $msg.title
'Link' = $msg.link
'Description' = $msg.description
}#EndPSCustomObject
}#EndForEach
If you wanted to do an if statement, then you would say
if($msg.Title.Contains($userTerm))
Or you could use the -like
operator with a wild card like so
if($msg.Title -like "*$($userTerm)*")