A bit of a PowerShell neophyte here, so please be kind. I am creating a PS script which is helping me to document my environment. What I am trying to do is get the names of all of an object's properties and populate an XML document with the property names and corresponding values. For example:
PS C:\> $stuff = Get-SomeStuff
PS C:\> $stuff
Name: foo
Version: 1.0
The output XML would look like this:
<rootNode>
<stuff>
<Name>foo</Name>
<Version>1.0</Version>
</stuff>
</rootNode>
Essentially I want to use the $stuff property names to substitute for the properties when retrieving the object properties. Essentially I need to dynamically substitute the property name with the extracted name. My code looks like this:
$stuff = Get-SomeStuff
CreateXML $rootnode,"stuff",$stuff
Function CreateXML ($rootNode, $nodeName, $category){
$subRootNode = $xml.CreateElement($nodeName)
$catProps = $stuff | Get-Member -MemberType Property | Select -ExpandProperty Name
foreach ($prop in $catProps){
$newNode = $xml.CreateElement($prop)
$newNode.Text = $category.{$prop} #--- THIS IS THE LINE CAUSING PROBLEMS
$subRootNode.AppendChild($newNode)
}
$rootNode.AppendChild($subRootNode)
}
Any help is greatly appreciated, as there are thousands of properties I have to capture across multiple nodes.
Get-SomeStuff? And what problems exactly does$category.{$prop}cause?