This is not something new to the most of you PowerShell guys out there, but still there are a lot of IT Pros which do not know about this. Sometimes we have to do some remote troubleshooting without having access to the system itself. The thing you can do is to let the customer send you some screenshots but that doesn’t really show everything and maybe you have to contact the customer like 100 times to get the right information. A better solution is to let the customer to run a PowerShell command or script and send you the output. But even a text file or screenshot of the PowerShell output is not the best solution. If you get a lot of text in a TXT file it is hard to sort it and maybe there are some information missing because the txt output does not include all information of the PowerShell object.
I have started to use a simple method to export PowerShell objects to a XML file and import the object on another system. This can be done by the PowerShell cmdlets Export-Clixml and Import-Clixml.
What I do is, I tell the customer to run the following command to generate a XML with the PowerShell objects about his disks for example.
Get-Disk | Export-Clixml C:\temp\Servername_disks.xml
After I got this XML file, I can import it here on my local system and can work with it as I would be in front of the customer system.
$disks = Import-Clixml C:\mylocaltemp\Servername_disks.xml
As I said, this is nothing new but this can save you and your customer some time. Of course this works with other objects not just disks ;-) For example you can get Cluster Configurations, Hyper-V Virtual Switch Configurations and much more.
Update:
Jeffrey P Snover (Microsoft Technical Fellow and Lead Architect of Windows Server) commented on my blog post and had some great input. If you want to troubleshoot sometimes you often need more information than just one information. To save multiple PowerShell objects into a single file you can use a hashtable to do this:
$info = @{ host = hostname date = get-date Disks = Get-disk; Services = get-service; Processes = get-process } $info | export-clixml c:\temp\info.xml
You can see more information on this topic in Jeffery Snovers comment on this blog.
Tags: CLIXML, Export, Hyper-V, Import, Microsoft, Object, PowerShell, Save, Troubleshooting, Windows, Windows 8, Windows 8.1, Windows Powershell, Windows Server, Windows Server 2012, Windows Server 2012 R2, XML Last modified: September 2, 2018
When it comes to troubleshooting, often you want multiple pieces of information so instead of putting them into different files, you can use a hashtable and then export it. e.g.
$info = @{
host = hostname
date = get-date
Disks = Get-disk;
Services = get-service;
Processes = get-process
}
$info | export-clixml c:\temp\info.xml
If you try to add hashtables of hashtables of hashtables, you’ll quickly run into something called serialization depth. Basically the idea is that we serialize to containers with properites using a base set of types. If a property doesn’t belong to the base set, we recurse and create a container of it’s properties. Some datastructures are recursive so if we don’t put a depth limit, it doesn’t work. When we hit the depth limit, we’ll call the properities ToString() method and save that.
You can control this with -Depth but be careful – it acts like a foaming agent on your data and the sizes ballon!
Jeffrey Snover
Technical Fellow [MSFT]
Hi,
This doesn’t import it back as the same object type though. It ends up being of type
> $test.GetEnumerator() | foreach {$_.gettype()}
IsPublic IsSerial Name BaseType
——– ——– —- ——–
True True PSObject System.Object
The original was not that. So, any ideas how I can export an object then import it and keep it’s type?