webCommander Tip #1

I will be posting several tips on webCommander by #VMware #Fling for the next few weeks. For more information on the software, refer to http://labs.vmware.com/flings/web-commander. Also, it’s good to install a SSL certificate which can be directed to my old post https://ssbkang.com/2014/02/11/webcommander-ssl-certificate-installation/

Introduction

Using webCommander to output a table i.e. a result, the administrator must create a result table defined in webCmd.xsl, an example below:

<xsl:if test="result/datastore">
  <center>
    <table width="100%">
      <tr>
        <th>Datastore</th>
        <th>freespace</th>
      </tr>
      <xsl:for-each select="result/datastore">
        <tr>
          <td><xsl:value-of select="datastore" /></td>
          <td><xsl:value-of select="freespace" /></td>
        </tr>
      </xsl:for-each>
    </table>
  </center>
</xsl:if>

And on your script:

write-output "<datastore>"
write-output "<datastore>$datastore</datastore>"
write-output "<freespace>$freespace</freespace>"
write-output "</datastore>"

It’s quite simple to output a result but one problem is that when the number of output table increases, the webCmd.xsl file becomes massive and hard to manage. Later on, I ask myself “Did I add this and this and that and that” and takes a time to look for the table.

configuration

To make the script simpler, under webCmd.xsl file, added a simple if statement. There will be only one table defined in webCmd.xsl file which I named custom and has all variables used for all scripts, shown below:

<!-- Modify Here !-->
<xsl:if test="result/custom">
  <center>
    <table class="exceptionTable" width="100%">
      <tr>
        <xsl:if test="result/custom/cluster"><th>Cluster</th></xsl:if>
        <xsl:if test="result/custom/esxi"><th>ESXi</th></xsl:if>
        <xsl:if test="result/custom/vm"><th>Virtual Machine</th></xsl:if>
        <xsl:if test="result/custom/powerstate"><th>PowerState</th></xsl:if>                                
        <xsl:if test="result/custom/vmdk"><th>VMDK</th></xsl:if>
        <xsl:if test="result/custom/datastore"><th>Datastore</th></xsl:if>
        <xsl:if test="result/custom/freespacegb"><th>FreespaceGB</th></xsl:if>
        <xsl:if test="result/custom/capacitygb"><th>CapacityGB</th></xsl:if>
        <xsl:if test="result/custom/scsicontroller"><th>SCSIController</th></xsl:if>
        <xsl:if test="result/custom/customattribute"><th>Custom Attribute</th></xsl:if>
        <xsl:if test="result/custom/customattributevalue"><th>Custom Attribute Value</th></xsl:if>
        <xsl:if test="result/custom/ntpserver"><th>ntpserver</th></xsl:if>
        <xsl:if test="result/custom/service"><th>service</th></xsl:if>
        <xsl:if test="result/custom/status"><th>status</th></xsl:if>
        <xsl:if test="result/custom/resourcepool"><th>resourcepool</th></xsl:if>
        <xsl:if test="result/custom/path"><th>path</th></xsl:if>
        <xsl:if test="result/custom/networkadatper"><th>networkadatper</th></xsl:if>
        <xsl:if test="result/custom/macaddress"><th>macaddress</th></xsl:if>
      </tr>
      <xsl:for-each select="result/custom">
        <tr>
          <xsl:if test="cluster"><td><xsl:value-of select="cluster" /></td></xsl:if>
          <xsl:if test="esxi"><td><xsl:value-of select="esxi" /></td></xsl:if>
          <xsl:if test="vm"><td><xsl:value-of select="vm" /></td></xsl:if>
          <xsl:if test="powerstate"><td><xsl:value-of select="powerstate" /></td></xsl:if>
          <xsl:if test="vmdk"><td><xsl:value-of select="vmdk" /></td></xsl:if>
          <xsl:if test="datastore"><td><xsl:value-of select="datastore" /></td></xsl:if>
          <xsl:if test="freespacegb"><td><xsl:value-of select="freespacegb" /></td></xsl:if>
          <xsl:if test="capacitygb"><td><xsl:value-of select="capacitygb" /></td></xsl:if> 
          <xsl:if test="scsicontroller"><td><xsl:value-of select="scsicontroller" /></td></xsl:if>
          <xsl:if test="customattribute"><td><xsl:value-of select="customattribute" /></td></xsl:if>
          <xsl:if test="customattributevalue"><td><xsl:value-of select="customattributevalue" /></td></xsl:if>
          <xsl:if test="ntpserver"><td><xsl:value-of select="ntpserver" /></td></xsl:if>
          <xsl:if test="service"><td><xsl:value-of select="service" /></td></xsl:if>
          <xsl:if test="status"><td><xsl:value-of select="status" /></td></xsl:if>
          <xsl:if test="resourcepool"><td><xsl:value-of select="resourcepool" /></td></xsl:if>
          <xsl:if test="path"><td><xsl:value-of select="path" /></td></xsl:if>
          <xsl:if test="networkadatper"><td><xsl:value-of select="networkadatper" /></td></xsl:if>
          <xsl:if test="macaddress"><td><xsl:value-of select="macaddress" /></td></xsl:if>
        </tr>
      </xsl:for-each>
    </table>
  </center>
</xsl:if>
<!-- Modify Ends !-->

Now, your script can have something like:

write-output "<custom>"
write-output "<cluster>$cluster</cluster>"
write-output "<datastore>$datastore</datastore>"
write-output "<freespacegb>$freespacegb</freespacegb>"
write-output "<capacitygb>$capacitygb</capacitygb>"
write-output "</custom>"

Or:

write-output "<custom>"
write-output "<cluster>$cluster</cluster>"
write-output "<vm>$vm</vm>"
write-output "<customattribute>$customattribute</customattribute>"
write-output "<customattributevalue>$customattributevalue</customattributevalue>"
write-output "</custom>"

Summary

Using an if statement in the table makes the script much simpler and easier to manage. There must be a better way but this is how I think 🙂

Update

Check out the latest comment by Jason, it also could be done by using JSON here comment

1 Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s