0

I am working on a project over here and I am a bit stuck in one thing. The project is an automation of assessment process for customer environment.

The idea is to run a script on the customer environment that will retrieve a number of information from the customer environment and then dump it to an xml file then later using another script, read the dumped xml file and convert the data within it to PowerShell Object to perform analysis on this data.

The Data retrieved should be organised in one way so what I am doing is that I am getting all info and data and adding them to a PSCustomeObject/HashTable and then converting this into xml then save it to the required path.

To organise the data I have retrieved in the PSCustomObject/HashTable I am actually having a nested PSCustomObject/HashTable which makes it more complicated.

The data that will be retrieved is with different type, some of them are actually a csv file, some of them are strings and other are system objects.

Below is an example of what I am doing with the code.

#Get NSX Controller Config From Managing NSX Manager. $ControllerConfig = $ContConfig #Get NSx Controller VM vSphere Info. $ControllerVmInfo = Get-VM -Server $vCenterServerHostingWorkingNsxControllers | Where-Object {$_.ExtensionData.moref.value -Eq $ContConfig.virtualMachineInfo.objectId} $ControllerESXi = Get-VM -Server $vCenterServerHostingWorkingNsxControllers -Name $ControllerVmInfo.Name | Get-VMHost $ControllerCluster = Get-VM -Server $vCenterServerHostingWorkingNsxControllers -Name $ControllerVmInfo.Name | Get-Cluster $ControllerDataStore = Get-VM -Server $vCenterServerHostingWorkingNsxControllers -Name $ControllerVmInfo.Name | Get-Datastore $ControllerSnapShot = Get-VM -Server $vCenterServerHostingWorkingNsxControllers -Name $ControllerVmInfo.Name | Get-Snapshot $ControllerClusterResources = Get-ClusterInfo -ReqVIServer $vCenterServerHostingWorkingNsxControllers -ReqCluster $ControllerCluster.Name $ControllerVMCpuUsagePercent = (Get-VM -Server $vCenterServerHostingWorkingNsxControllers -Name $ControllerVmInfo.Name | Get-Stat -Realtime -Stat cpu.usage.average | Sort-Object Timestamp -Descending | Select-Object -First 1).Value $ControllerVMMemUsagePercent = (Get-VM -Server $vCenterServerHostingWorkingNsxControllers -Name $ControllerVmInfo.Name | Get-Stat -Realtime -Stat mem.usage.average | Sort-Object Timestamp -Descending | Select-Object -First 1).Value $ControllerVMUsedDiskSpace = [math]::Round(((Get-VM -Server $vCenterServerHostingWorkingNsxControllers -Name $ControllerVmInfo.Name).UsedSpaceGB | Measure-Object -Sum).sum) $ControllerCpuHistoricUsage = (Get-VM -Server $vCenterServerHostingWorkingNsxControllers -Name $ControllerVmInfo.Name | Get-Stat -Stat cpu.usage.average) $ControllerMemoryHistoricUsage = (Get-VM -Server $vCenterServerHostingWorkingNsxControllers -Name $ControllerVmInfo.Name | Get-Stat -Stat mem.usage.average) $ControllerVMDiskReadLatency = (Get-VM -Server $vCenterServerHostingWorkingNsxControllers -Name $ControllerVmInfo.Name | Get-Stat -Realtime -Stat virtualdisk.totalreadlatency.average) $ControllerVMDiskWritLatency = (Get-VM -Server $vCenterServerHostingWorkingNsxControllers -Name $ControllerVmInfo.Name | Get-Stat -Realtime -Stat virtualdisk.totalwritelatency.average) $ControllerVMMaxTotalLatency = (Get-VM -Server $vCenterServerHostingWorkingNsxControllers -Name $ControllerVmInfo.Name | Get-Stat -Stat disk.maxTotalLatency.latest) #Get NSX Controller SSH Output. $ControllerSSHPassword = ConvertTo-SecureString $EnvironmentInfo.NsxControllersPassword -AsPlainText -Force $ControllerCredentials = New-Object System.Management.Automation.PSCredential ("admin", $ControllerSSHPassword) $ControllerSSHSession = New-SSHSession -ComputerName $ContConfig.ipAddress -Credential $ControllerCredentials -AcceptKey $ControllerClusterStatus = (Invoke-SSHCommand -SSHSession $ControllerSSHSession -Command "show control-cluster status").Output $ControllerClusterStartupNodes = (Invoke-SSHCommand -SSHSession $ControllerSSHSession -Command "show control-cluster startup-nodes").Output $ControllerDiskStatus = (Invoke-SSHCommand -SSHSession $ControllerSSHSession -Command "show status").Output | Where-Object {$_ -Match "\S+\s+\d+\s+\d+\s+\d+\s+(\d+)%\s(\/\S*)$"} $ControllerUpTime = (Invoke-SSHCommand -SSHSession $ControllerSSHSession -Command "show status").Output | Where-Object {$_ -Match "Uptime"} Remove-SSHSession -SSHSession $ControllerSSHSession | Out-Null #Add all retrived data in a Hash Table $NsxControllerDataRetrival = New-Object PSObject -Property @{ ControllerId = $ControllerConfig.id ControllerConfig = $ControllerConfig ControllervSphereInfo = New-Object PSObject -Property @{ ControllerVmInfo = $ControllerVmInfo ControllerESXi = $ControllerESXi ControllerCluster = $ControllerCluster ControllerDataStore = $ControllerDataStore ControllerSnapShot = $ControllerSnapShot ControllerClusterResources = $ControllerClusterResources ControllerVMCpuUsagePercent = $ControllerVMCpuUsagePercent ControllerVMMemUsagePercent = $ControllerVMMemUsagePercent ControllerVMUsedDiskSpace = $ControllerVMUsedDiskSpace ControllerCpuHistoricUsage = $ControllerCpuHistoricUsage ControllerMemoryHistoricUsage = $ControllerMemoryHistoricUsage ControllerVMDiskReadLatency = $ControllerVMDiskReadLatency ControllerVMDiskWritLatency = $ControllerVMDiskWritLatency ControllerVMMaxTotalLatency = $ControllerVMMaxTotalLatency } ControllerSshInfo = New-Object PSObject -Property @{ ControllerClusterStatus = $ControllerClusterStatus ControllerClusterStartupNodes = $ControllerClusterStartupNodes ControllerDiskStatus = $ControllerDiskStatus ControllerUpTime = $ControllerUpTime } } ($NsxControllerDataRetrival | ConvertTo-Xml).Save($ResultFilePath + "NSX-Controller-Data-" + $EnvironmentInfo.NsxManagerFqdn + ".xml") 

Actually if I used a PSObject and convert it to an XML it will error out with the below error as I would need to add depth but it is hard to identify the required depth adding in mind that the above is repeated multiple time through out the process with different data to be retrieved different time.

ConvertTo-Xml : Unexpected end of file has occurred. The following elements are not closed: Property, Property, Property, Property, Property, Property, Property, Property, Property, Property, Property, Property, Property, Property, Object, Property, Property, Property, Property, Property, Property, Property, Property, Property, Property, Property, Property, Property, Property, Property, Object, Property, Property, Property, Property, Property, Property, Property, Property, Property, Property, Property, Property, Property, Property, Property, Object, Objects. Line 6, position 10817. At \\tsclient\RDP-Shared-Folder\New-Multi-Site-Tool\Sub-Scripts\CGS-AT-Gather-NSX.ps1:1065 char:29 + ($NsxControllerDataRetrival | ConvertTo-Xml).Save($Re ... + ~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [ConvertTo-Xml], XmlException + FullyQualifiedErrorId : System.Xml.XmlException,Microsoft.PowerShell.Commands.ConvertToXmlCommand 

If used HashTable to add all data and then dump them into an XML file, then the XML file formate is very hard and I am not able to figuring out the dot notation or XPath query to extract the attributes/elements when I read the xml file in a later point. an example of the xml file is as follow:

<?xml version="1.0" encoding="utf-8"?> <Objects> <Object Type="System.Collections.Hashtable"> <Property Name="Key" Type="System.String">ControllerSshInfo</Property> <Property Name="Value" Type="System.Collections.Hashtable"> <Property Name="Key" Type="System.String">ControllerClusterStatus</Property> <Property Name="Key" Type="System.String">ControllerUpTime</Property> <Property Name="Value" Type="System.String">Uptime: 106 days 16 hours 43 minutes 54 seconds </Property> <Property Name="Key" Type="System.String">ControllerClusterStartupNodes</Property> <Property Name="Value" Type="System.String[]"> <Property Type="System.String">192.168.4.61, 192.168.4.62, 192.168.4.63</Property> </Property> <Property Name="Key" Type="System.String">ControllerDiskStatus</Property> <Property Name="Value" Type="System.Object[]"> <Property Type="System.String">devtmpfs 2009560 0 2009560 0% /dev</Property> <Property Type="System.String">/dev/sda2 3997376 2266940 1504340 61% /</Property> <Property Type="System.String">/dev/sda1 999320 40952 889556 5% /boot</Property> <Property Type="System.String">/dev/sda6 1998672 3092 1874340 1% /config</Property> <Property Type="System.String">/dev/sda3 3997376 8184 3763096 1% /os_bak</Property> <Property Type="System.String">/dev/sda5 5029504 1795892 2955084 38% /var/log</Property> <Property Type="System.String">/dev/sda7 5029504 10236 4740740 1% /image</Property> <Property Type="System.String">/dev/sda4 3997376 142004 3629276 4% /var/cloudnet/data</Property> </Property> </Property> 

I would appreciate your help if you can help me out with dumping the data into an xml file with an more easier formate or even point me to a different path where I can do the above.

Thanks for your help.

2
  • Can't help with the XML issues, but having multiple identical Get-VM commands is quite wasteful. You could likely just run Get-VM once, assign the result to a variable and use that in-place of the other commands. Commented Sep 6, 2018 at 15:46
  • @JamesC. good point, not sure I did not do that but will defiantly change it. Thanks for the advice Commented Sep 9, 2018 at 9:53

1 Answer 1

1

Have you tried Export-CliXml and Import-CliXml? That's made for saving PS objects as XML and back.

https://learn.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/Export-Clixml?view=powershell-5.1

This will get dirinfo objects, export and import them, and dump out the first one.

Export-Clixml -InputObject $x -path c:\temp\test.xml $listing = Import-Clixml -Path c:\temp\test.xml $listing[0] | gm 

Output

TypeName: Deserialized.System.IO.DirectoryInfo .... 
Sign up to request clarification or add additional context in comments.

1 Comment

That worked like a charm, I knew about Export-CliXml but I did not know about the import option. Thank you for your help

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.