Monday, March 10, 2014

List models as a HTML Table using PowerShell

I was working on a PowerShell script to list my servers and their specs the other day and one of the cool things I learned was how easy it is to push the result out as a HTML table. Why would you want to do that? Well, it can be (just) another way of presenting the models in a modelstore. Also it is a matter of copy and paste to grab the table over to Excel if that is your swag.

So let's get on with the command:

axmodel | select modelid, name, layer, version, elementcount | `
sort modelid | ConvertTo-Html -CssUri http://skaue.com/ps.css | Out-File c:\models.html

I'm not specifying the modelstore here, just grabbing whatever my local config is pointing too. I'm also making use of the default verb, so I can write "axmodel" instead of "get-axmodel". Nifty, ey?

I'm a former web developer, so I uploaded a small stylesheet to prettify my table. Feel free to grab it and reuse it if you want to, or simply reuse the URL.

body{background-color:#efefef; font-family:tahoma;font-size:90%;}
table{border: 1px solid #999;}
th{border-bottom:1px solid #999; text-align:left;}
td{padding:2px;}
tr:nth-child(odd) { background-color:#eee;}
tr:nth-child(even) { background-color:#fff;}

And here is the result:



PowerShell + AX + Web development - fun, fun, fun!

No comments:

Post a Comment