5

I have the following powershell script that scans a location and adds the file details to a xml file,

Get-ChildItem -recurse c:\DATA | Select-Object  * , @{Name="Kbytes";Expression={ "{0:N0}" -f ($_.Length / 1Kb) }},@{Name="Age";Expression={ (((Get-Date) - $_.CreationTime).Days) }}  | Export-Clixml c:\DATA\Final.xml 

As far as I understand this should be a object in the .net framework, assuming a dataset ? what I would like to do is load this object into a c# application and use it as a dataset.

How would I load the object into a dataset in c# ?

1
  • By using Export-CliXML you are seriealizing the data. so, When you deserialize this, it won't be a live object. All the methods available on the object will be lost. Commented Jun 18, 2012 at 18:21

2 Answers 2

6
  1. Add reference to the System.Management.Automation.dll assembly.
  2. Create a PowerShell Runspace
  3. Open the Runspace
  4. Create a PowerShell Pipeline object with the Import-CliXml command
  5. Invoke the Pipeline
  6. Close the Runspace

        var rs = RunspaceFactory.CreateRunspace();
        rs.Open();
        var pl = rs.CreatePipeline(@"Import-CliXml c:\DATA\Final.xml;");
        var result = pl.Invoke();
        rs.Close();
    
Sign up to request clarification or add additional context in comments.

Comments

1

A faster approach might be:

-For lists

object[] xmlAsList =
     System.Management.Automation.PSSerializer.DeserializeAsList(System.IO.File.ReadAllText(@"C:\Users\batman\Desktop\myList.clixml"));

-For generic objects

object importedObject =
 System.Management.Automation.PSSerializer.Deserialize(System.IO.File.ReadAllText(@"C:\Users\batman\Desktop\myObject.clixml"));

Then, you manage the results via: System.Management.Automation.PSObject using the Members property.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.