I am working with VMware vCenter 5 and want to create a quick report of the DRS rules that have been created. Below is an excerpt of the data:
<obj xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:vim25" versionId="5.0" xsi:type="ArrayOfClusterRuleInfo">
<ClusterRuleInfo xsi:type="ClusterAffinityRuleSpec">
<key>1</key>
<enabled>true</enabled>
<name>SQL Servers</name>
<vm type="VirtualMachine" xsi:type="ManagedObjectReference">vm-100</vm>
<vm type="VirtualMachine" xsi:type="ManagedObjectReference">vm-200</vm>
<vm type="VirtualMachine" xsi:type="ManagedObjectReference">vm-300</vm>
</ClusterRuleInfo>
<ClusterRuleInfo xsi:type="ClusterAntiAffinityRuleSpec">
<key>2</key>
<enabled>true</enabled>
<name>Oracle DBs</name>
<vm type="VirtualMachine" xsi:type="ManagedObjectReference">vm-1000</vm>
<vm type="VirtualMachine" xsi:type="ManagedObjectReference">vm-2000</vm>
</ClusterRuleInfo>
</obj>
Note that the number of VMs per rule may differ.
I have not been able to turn this XML data into a table, so I tried locating just the name (within the tags) with this query:
SELECT ref.value('name[1]', 'varchar(200)') as [enabled]
FROM @data.nodes('/obj/ClusterRuleInfo') data( ref )
It returns no data. I found that if I remove the and lines, it works, but I’m not sure why. I believe it is because SQL Server does not understand the XML schema (or something similar – I’m not an XML expert).
In the end, I’d like to see a table like this:
Name VM
----------- ----
SQL Servers 100
SQL Servers 200
SQL Servers 300
Oracle DBs 1000
Oracle DBs 2000
Your XML uses a namespace, so you need the
WITH XMLNAMESPACESclause: