With PowerShell we are able to easily generate HTML from objects and, with some amount of adjusting, publish presentable reports. This post is not about html publishing from PowerShell. It is about exporting information as XML and then using PowerShell to generate a conversion to another format using XSLT in numerous ways.
What is XSLT?
XSLT stands for eXtensible StyleSheet Language Transormation. It is a declaritive XML extension. XML is the “eXensible” in XML and XSL/XSLT. XSL is the XML specification for the language used to manipulate XML data. It can manipulate any XML variation including XSL.
Background Definitions:
- XML http://en.wikipedia.org/wiki/XML
- XSL http://en.wikipedia.org/wiki/XSL
- XSLT http://en.wikipedia.org/wiki/XSLT
W3C:
W3Schools:
PowerShell and XML
There are numerous examples and tutorials on the web. The above information I but a very small part of what is available so why add more information and tutorials?
PowerShell can an does natively consume and emit XML. Under the covers Microsoft Windows consumes and emits XML nearly everywhere. Nearly all tools and tool products for Windows are XML based to some degree. PowerShell support files are all XML.
Given the amount of XML everywhere it should be easy to generate reports that are readable and printable using the XML, but, in PowerShell there are no tools for doing this. We will build some simple tools that will makes access to this XML data much easier.
CovertTo-Xml – A Simple XML Generator
http://technet.microsoft.com/library/hh849889.aspx
This CmdLet takes an object and converts it to XML. By default it converts all top level properties of any object.
Try it:
$xml=get-process | ConvertTo-Xml
$xml.Objects.Object.Property
This will show you how the object is constructed. The basic wrapper looks like this:
<Objects>
<Object>
<Property ..../>
<Property ..../>
</Object>
...
</Objects>
What can we do with this?
PowerShell can convert this into any other format required very easily by treating it as a collection of objects. We can also just turn it directly into a report that can be published on a web site or sent by email. We can do this without manipulating strings or using the PowerShell ConvertTo-Html which has many limitations when we need to produce complex dynamic reports. An XSLT will allow us to merge two powerful tools into a solution.
The Code
# generate XML
$xml=get-process | ConvertTo-Xml
$xml.Save("$pwd\processes.xml")
#transform to HTML report
$xslt=New-Object System.Xml.Xsl.XslCompiledTransform
$xslt.Load("$pwd\processes.xsl")
$xslt.Transform("$pwd\processes.xml","$pwd\processes.html")
That is all of the code it takes to dump a collection of objects mand run a transform to produse an HTML report.
What is the Advantage?
The advantage is that we can dump as many collections of XML objects as we need in out report and run one transform – the same transform as above – just that few lines of code.
Yes. We can add as many XML data dumps as we need to gather the data for our report or reports. We can deign a reporting system that just runs simple data dumps and not have to be concerned about how the data can be combined. The data can be combined in PowerShell or dumped independently. It all depends on what our reporting system needs to consume. What we buy is that the reporting system does not have to be data smart. It just has to consume and transform XML.
Other Advantages
There are almost an unlimited number of other advantages to using XML/XSLT as the presentation tool. Here are a few:
- We can easily inject extra info like table name or report name.
- XML can be converted to almost any other format for use by external systems
- XSL can generate dynamic web pages.
- XML can be reused in different reports and different sections.
- XML can be used to generate tables and lists with collapsible elements
- ….More..
I am not recommending that all reporting should be done this way. For many simple reports just dumping text or direct use of ConvertTo-Html would be more convenient. Your choice of XML will depend on what we discuss in the next few issues of this set of blog posts.
Where did the XSL file come from in your example?
ReplyDeleteHi David,
DeleteDid you read part 2 yet?
http://tech-comments.blogspot.com/2013/09/powershell-reporting-with-xml-part-2.html