How to read ATOM XML in PowerShell - powershell

How to call System.ServiceModel.Syndication.SyndicationFeed from PowerShell?
I was trying to read RSS feed from Powershell, and need to traverse through feed, I am wondering what would be the way to do it?
I could not find any example on it.
Thanks in advance!!
Ramani

What you're really trying to do is parse XML with PowerShell. This one liner will get you the entries from this question:
((New-Object Net.Webclient).DownloadString("http://stackoverflow.com/feeds/question/10589059") -as [xml]).feed.entry
It creates a new webclient, and downloads the URL, turns it into XML, and then just dots into the XML elements (as you can do in PowerShell).
It's a very simple one liner that packs in a lot of the power of PowerShell. A similar example is at the beginning of Bruce Payette's "PowerShell In Action"
Hope this Helps

Here's what I came up with in a few seconds:
$url = "http://stackoverflow.com/feeds/question/10589059"
[System.Reflection.Assembly]::LoadWithPartialName("System.ServiceModel") | Out-Null
[System.ServiceModel.Syndication.SyndicationFeed] $feed = [System.ServiceModel.Syndication.SyndicationFeed]::Load([System.Xml.XmlReader]::Create($url))
$feed | Get-Member

Related

How to read a PDF using Powershell

I am at the beginning of my first real powershell project. Right now I am trying to read certain fields from a PDF, specifically account number and District ID. However, despite scouring the internet for a couple of hours, there is not an answer when it comes to using iText 7. I tried using a iTextSharp video while substituting what I thought was the correct add on for iText 7 but that just failed. I am new to trying to read a PDF and am literally just trying to get it to return the pdf into a text file. Once I get that, I'll worry about getting the right information. If there is an easier way to just pull directly from the field, I'm all ears.
**The final task is to pull this information from literally hundreds of the same document. I'm just trying to get through this process using baby steps. The fields are typed in, so reading, in theory, should be easy enough
Add-Type -Path "file path\itext.pdfa.dll"
$path= "file path\doc.pdf"
$pdf = New-Object iText.text.pdf.PdfReader -ArgumentList $path
$export=""
foreach($page in 1..($pdf.NumberOfPages)){
$export=
[iText.text.pdf.parser.PdfTextExtractor]::GetTextFromPage($pdf,$page)
}
$export | Out-File "file path\Test.txt"

Assemble oneliners from multiline PS script

Need to manipulate ICS from command line batch script, but did not found suitable console tool. But found powershell script which do such task. The problem is the script is multiline and as I have zero experiences with PS, looking for someone who could create few oneliners from such script to easily run trhough batch script and parse its output. Need following oneliners:
List adapters with ICS enabled
Enable ICS for specific interfaces
Disable ICS on specific interface
Anyone could help me with this?
Why on Earth would they need to be one-liners? None of these are suitable for one-liners, for instance your first one could look like this.
(New-Object -ComObject HNetCfg.HNetShare -OutVariable netshare).EnumEveryConnection | foreach {[PSCustomObject]#{Interface = $netshare.NetConnectionProps.Invoke($_).name; ICSEnabled = $netshare.INetSharingConfigurationForINetConnection.invoke($_).sharingenabled}}

How can I use Powershell to extract mail headers from .msg file?

I'm trying to write a script that reads the mail headers from a directory full of .msg files so I can later parse them via regex. I tried $MSG = Get-Content .\message.msg, which could work, but it's a pretty dirty output. Has anyone tried this? I can't seem to find a working example online.
You have a few options depending on your environment. If you are on a computer with Outlook installed you can easily do this with an Outlook com object. The problem is that the headers are not exposed by default so you have to dig for them.
$ol = New-Object -ComObject Outlook.Application
$msg = $ol.CreateItemFromTemplate("SOME\PATH\TO\A\MSG\FILE.msg")
$headers = $msg.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x007D001E")
$headers
At this point you have a text block with all of the header information in it. If you want a specific header you will need to write a regex to extract it.
You could also write a class that reads the raw content based on the specification. Or read in the raw content with powershell and write a regex to attempt to extract it.

preplog.exe ran in foreach log file

I have a folder with x amount of web log files and I need to prep them for bulk import to SQL
for that I have to run preplog.exe into each one of them.
I want to create a Power script to do this for me, the problem that I'm having is that preplog.exe has to be run in CMD and I need to enter the input path and the output path.
For Example:
D:>preplog c:\blah.log > out.log
I've been playing with Foreach but I haven't have any luck.
Any pointers will be much appreciated
I would guess...
Get-ChildItem "C:\Folder\MyLogFiles" | Foreach-Object { preplog $_.FullName | Out-File "preplog.log" -Append }
FYI it is good practice on this site to post your not working code so at least we have some context. Here I assume you're logging to the current directory into one file.
Additionally you've said you need to run in CMD but you've tagged PowerShell - it pays to be specific. I've assumed PowerShell because it's a LOT easier to script.
I've also had to assume that the folder contains ONLY your log files, otherwise you will need to include a Where statement to filter the items.
In short I've made a lot of assumptions that means this may not be an accurate answer, so keep all this in mind for your next question =)

Are there good references for moving from Perl to Powershell?

I hate to say it, but powershell is really annoying me. I just cannot seem to get my mind around it. I have an O'Reilly book on the topic, and I can see how to do some extremely powerful stuff -- it's the easy stuff I can't seem to get right.
Case in point: Iterate across a list of filenames.
In CMD:
for /F %x in ('dir EXPRESSION') do #(
arbitrary-action %x
)
In Perl:
#foo=glob("*");
foreach (#foo)
{
arbitrary-command $_ ;
}
In Powershell:
I'm dumbfounded. I can't seem to get it right.
But I am not sending this post so somebody can tell me the answer. I don't want the answer to this simple question. I want to know how to figure it out, and Google/Bing searches are just not cutting it. The get-help functionality is powershell is nice, but it's still not enough.
I've been programming for 20 years.
I've learned BASIC, Fortran, C, Pascal, Perl, C++, C#, BASH and CMD scripting...And never have I had the trouble I'm having with Powershell.
Are there no references "out there" for migrating from Perl to Powershell? It seems like such a straightforward thing to publish, but I have yet to find one.
Any help would be appreciated. Thanks.
Update:
Okay, so maybe this wasn't the best example to post.
I think I was thrown off by the fact that when I tried gci interactively, I got a directory listing, where what I wanted was an array of strings.
I took a leap of faith and tried:
foreach ($foo in gci "*") {
echo $foo;
}
And yeah, it worked. And yes, I can continue to do searches to piece my way through. I guess I was just hoping to find a guide that makes use of the similarity to languages I already know. I know that Microsoft published a VBScript-to-Powershell guide, so I was hoping for a Perl equivalent.
Thanks again
I've never seen such a guide. I've seen something to help people going from VBScript to PowerShell.
Bruce Payette's PowerShell in Action does have a few pages on PowerShell vs X scripting language, but that won't cut it for a conversion guide.
Now, there was a site out there that had all kinds of constructs in multiple languages, thus providing a task, and then going about solving it in all kinds of languages based on answers from the community... Anyone know what I'm talking about?
I don't know of any good Perl to Powershell comparisons but I can answer your secondary question.
$files = get-childitem "c:\test\" -filter *.dll
foreach ($file in $files) { $file.Name }
There are two different ways you can express a foreach loop in Powershell.
You can pipe an array of objects into foreach and $_ becomes the current object on each iteration.
0,1,2,3 | foreach { $_ }
Alternatively, you can pass a variable to iterate over.
foreach ($num in 0,1,2,3) { $num }
Output in both cases.
0
1
2
3
Like he said himself, he realized the potential to get some very powerful things using powershell...I recently started using it myself and the ease with which things can be done is astounding...just a few lines is all it takes to accomplish things that in python woulda taken me some extra workarounds etc.
I'm curious do you have the powershell cookbook? I thought since I had programming experience that it would be the best way to quickly learn powershell. This turned out to be a poor assumption. Even though the book was really good, I was struggling because I needed a book that was structured more for learning than reference.
The two best free online ebooks I found are:
https://blogs.technet.com/chitpro-de/archive/2007/05/10/english-version-of-windows-powershell-course-book-available-for-download.aspx
http://powershell.com/cs/blogs/ebook/
I'm still looking for a good print book.
I think finding a Perl to PowerShell guide is going to be difficult. It is actually more accurate to compare PowerShell to BASh and the C Shell than Perl. I think what makes learning PowerShell difficult from most of the available books is that they are aimed at system admins, not programmers. I recommend the "PowerShell in Action" for the best coverage of PowerShell as a general purpose programming language.
The other thing you need to do is immediately embrace the core principal of PowerShell -- you are dealing with objects, not text. With BASh and the other Unix shells, it's all about text manipulation and while Perl can do objects, its roots are still very much in the Unix shell and command line utilities (grep, sed, awk, etc.).
Larry Wall stole a lot of great ideas from other languages when he created Perl. PowerShell has done the same, using the POSIX shell as its starting point. You can see a lot of Perl in PowerShell, as well as other languages. I think the best way to learn PowerShell is by having a PowerShell window in front of you while reading "PowerShell in Action" which will help you get into the PowerShell way of thinking about how it does objects. It is easy to interactively enter code snippets in a PowerShell window and examine the properties and methods available within the objects returned by the commands.
BTW -- if you are use to using BASh with the default command line editing features, put the following command in your PowerShell $PROFILE
Set-PSReadlineOption -editmode Emacs
Do this and you'll feel right at home. Now that I can run PowerShell on Linux and the Mac, I'm not sure what I will ever need BASh for again.
Funny... I had the same issues when I started with PowerShell, but after using PowerShell now for a couple of months, I have dumped Perl like ugly Sally after the Prom. There are several great ways of using foreach to loop through an list of objects. Let's look at the following example where I have a number of services that I want to make sure are running on my Windows Server (the processes all have Equal in the name). I can do this with a one liner command as follows:
Get-Service | where-object {$_.displayname -like "Equal*"} | foreach {
if($_.Status -eq "Stopped") {
Write-Host "`nRestarting..."
write-host $_.DisplayName
Start-Service $_.name
}
}
The first part of the command is the get-service command - filtering the services looking for services with the display name with Equal in them. The next part of the one liner really shows the beauty of PowerShell. The get-service command returns a list of objects, which can then be acted upon by piping them into a foreach loop. Note I did not have to declare any variables or an array to hold the objects. The objects are stored in the default $_ variable from which I can pull out object properties like name and status. Foreach returned object (service) I check its status to see if it's status is stopped and if it is it is restarted with the Start-Service command. I could do this a similar action with Perl, but I would have to screen scrape and place the values into an array, then foreach over the array in a similar manner as I did above. The screen scrapping would involve grep with regexp which adds lines of code. Because PowerShell commands produce objects with properties, I can quickly drill down to the properties I am looking for without the screen scraping hassles.