Hello,
I want to run a script and ususally i send my data to output in csv or internet explorer.
How do i do it to out put in xml?
Please advice, in xml I wanted it to show up as
<cluster>123123123</cluster>
-- <host>xxxx</host>
-- <datastore> 123123</datastore>
-- <vm>vmname</vm>
i am unsure how the xml tags would show up or appear.
Please advice,
Creating XML objects from PowerShell is quite easy.
Tobias has a good write-up in XML Part 1: Playing with RSS Feeds and XML Content and XML Part 2: Write, Add And Change XML Data.
I have an older post, called TA2650 scripts – Part 1 – Profiling your vSphere environment, that shows how to dump the Config property of Managed Objects to an XML file.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks Luc as always,
Looks like it isn't as easy as Exort-xml filename.xml ?
RJ
Hey Luc,
I looked at the scripts and it talks about xml parsing and manipulating.
I simply want the final out put of an object array to an xml.
Any ideas?
Thanks
RJ
There is a very intersting function, called New-Xml, in Jeffrey Snover's post, called Using PowerShell to Generate XML Documents.
It allows you to convert any array of objects to an XML file
function New-Xml{ param($RootTag = "ROOT",$ItemTag = "ITEM", $ChildItems = "*", $Attributes = $Null) Begin { $xml = "<$RootTag>`n"
} Process { $xml += " <$ItemTag"
if ($Attributes) { foreach ($attr in $_ | Get-Member -type *Property $attributes) {
$name = $attr.Name $xml += " $Name=`"$($_.$Name)`""
} } $xml += ">`n"
foreach ($child in $_ | Get-Member -Type *Property $childItems) { $Name = $child.Name $xml += " <$Name>$($_.$Name)</$Name>`n"
} $xml += " </$ItemTag>`n"
} End { $xml += "</$RootTag>`n"
$xml
} } $report = @() foreach($cluster in Get-Cluster){ foreach($esx in (Get-VMHost -Location $cluster)){ foreach($vm in (Get-VM -Location $esx)){ $row = "" | Select Cluster,Host,VM $row.Cluster = $cluster.Name $row.Host = $esx.Name $row.VM = $vm.Name $report += $row
} } } $report | New-XML -RootTag Root -ItemTag VM -ChildItems Cluster,Host,VM | `
Set-Content "C:\test.xml"
This produces a rather flat XML file, but you can play around with the ChildItems and Attributes parameters.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks Luc,
That fuction blew my head but I think that might do thanks a ton.
I will run that and see if the output is to my liking.
So in a funtion there is a param - declare variables , begin - initialize, process and then end parts?
Thanks
RJ
That is (nearly) correct.
There is a param entry to define the parameters that are passed.
Followed by a Begin, Process and End block.
See the full explanation when you do
Get-Help about_functions
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference