Hey all,
I’m working through a customize OVF process on a Windows VM that someone in the community wrote and I'm struggling to find out what is getting stored in these variables. It’s pulling from an OVF Environment xml file that I’ve screenshot below, but any ideas on how to write-host to print what is actually getting stored in each variable so I can see why some aren’t passing properly?
$vmenvxml = 'D:\vmconfig\vmenv\vmenv.xml'
# Import XMLs save those as variables.
[xml]$vmenv = Get-Content $vmenvxml
# Collect Variables from vmenv XML
$vmHostname = $vmenv.Environment.PropertySection.Property | ?{ $_.key -like '1._Hostname' } | select -expand value
$vmIP = $vmenv.Environment.PropertySection.Property | ?{ $_.key -like '2._IP_Address' } | select -expand value
$vmNetmask = $vmenv.Environment.PropertySection.Property | ?{ $_.key -like '3._Subnet_Mask' } | select -expand value
$vmGW = $vmenv.Environment.PropertySection.Property | ?{ $_.key -like '4._Default_Gateway' } | select -expand value
$vmDNS1 = $vmenv.Environment.PropertySection.Property | ?{ $_.key -like '5._Primary_DNS' } | select -expand value
$vmDNS2 = $vmenv.Environment.PropertySection.Property | ?{ $_.key -like '6._Secondary_DNS' } | select -expand value
XML OVF Environment:
<?xml version="1.0" encoding="UTF-8"?>
<Environment
xmlns="http://schemas.dmtf.org/ovf/environment/1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:oe="http://schemas.dmtf.org/ovf/environment/1"
xmlns:ve="http://www.vmware.com/schema/ovfenv"
oe:id=""
ve:vCenterId="vm-230">
<PlatformSection>
<Kind>VMware ESXi</Kind>
<Version>6.7.0</Version>
<Vendor>VMware, Inc.</Vendor>
<Locale>en</Locale>
</PlatformSection>
<PropertySection>
<Property oe:key="1._Hostname" oe:value="demo-custom"/>
<Property oe:key="2._IP_Address" oe:value="1.1.1.2"/>
<Property oe:key="3._Subnet_Mask" oe:value="255.255.255.0"/>
<Property oe:key="4._Default_Gateway" oe:value="1.1.1.1"/>
<Property oe:key="5._Primary_DNS" oe:value="2.2.2.2"/>
<Property oe:key="6._Secondary_DNS" oe:value="3.3.3.3"/>
</PropertySection>
</Environment>
You could insert a line like
Get-Variable -Name vm*
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I’m actually trying to use the keys from the XML file. Some of them are getting set but others are not, so I was hoping to see what data is actually being parsed in this section so I could further troubleshoot why the IP address, mask, gw aren’t being set.
You mean something like this?
# Import XMLs save those as variables.
[xml]$vmenv = Get-Content $vmenvxml
$vmenv.SelectNodes('//*')
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
That doesn't seem to give me any output when I run it against the XML. I'm struggling with understanding what's being passed down into the first pipeline and then how it's being parsed and what's happening after the next pipeline. I'm not sure the -like or the value inside the single quotes are enough to actually pull the data needed which in this example below is the actual default gateway key which is tied to the gateway IP .
$vmenv.Environment.PropertySection.Property | ?{ $_.key -like '4._Default_Gateway' } | select -expand value
Works for me.
# Import XMLs save those as variables.
[xml]$vmenv = Get-Content $vmenvxml
$vmenv.SelectNodes('//*')
This outputs
If you want to find a specific property, a better way is probablyto use Select-Xml and provide a XPath.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I must have done something wrong last night because that does get the data I needed. However, is there something wrong with the syntax being used to tie each of them to variables? The keys in the select nodes output is the exact same as what's in the single quotes, I'm trying determine where the ball is dropping.
Not sure I'm following.
Are you saying that the variables do not contain the correct values?
When I test this (I use a here-string instead of a file, but that shouldn't make a difference), the variables contain the expected values.
<?xml version="1.0" encoding="UTF-8"?>
<Environment
xmlns="http://schemas.dmtf.org/ovf/environment/1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:oe="http://schemas.dmtf.org/ovf/environment/1"
xmlns:ve="http://www.vmware.com/schema/ovfenv"
oe:id=""
ve:vCenterId="vm-230">
<PlatformSection>
<Kind>VMware ESXi</Kind>
<Version>6.7.0</Version>
<Vendor>VMware, Inc.</Vendor>
<Locale>en</Locale>
</PlatformSection>
<PropertySection>
<Property oe:key="1._Hostname" oe:value="demo-custom"/>
<Property oe:key="2._IP_Address" oe:value="1.1.1.2"/>
<Property oe:key="3._Subnet_Mask" oe:value="255.255.255.0"/>
<Property oe:key="4._Default_Gateway" oe:value="1.1.1.1"/>
<Property oe:key="5._Primary_DNS" oe:value="2.2.2.2"/>
<Property oe:key="6._Secondary_DNS" oe:value="3.3.3.3"/>
</PropertySection>
</Environment>
'@
[xml]$vmenv = $data
$vmHostname = $vmenv.Environment.PropertySection.Property | ?{ $_.key -like '1._Hostname' } | select -expand value
$vmIP = $vmenv.Environment.PropertySection.Property | ?{ $_.key -like '2._IP_Address' } | select -expand value
$vmNetmask = $vmenv.Environment.PropertySection.Property | ?{ $_.key -like '3._Subnet_Mask' } | select -expand value
$vmGW = $vmenv.Environment.PropertySection.Property | ?{ $_.key -like '4._Default_Gateway' } | select -expand value
$vmDNS1 = $vmenv.Environment.PropertySection.Property | ?{ $_.key -like '5._Primary_DNS' } | select -expand value
$vmDNS2 = $vmenv.Environment.PropertySection.Property | ?{ $_.key -like '6._Secondary_DNS' } | select -expand value
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Alright I was able to take what you gave and play with it a little to see what I was looking for. I was just trying to make sure that those lines were gathering the data I needed. It must be somewhere else that in the larger script that is broken.
[xml]$vmenv = Get-Content $vmenvxml
$vmIP = $vmenv.Environment.PropertySection.Property | ?{ $_.key -like '2._IP_Address' } | select -expand value
Write-Host “$vmIP”
Thanks for your help!!