Question:
I have the following powershell script that scans a location and adds the file details to a xml file,
1 2 |
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# ?
Answer:
- Add reference to the
System.Management.Automation.dll
assembly. - Create a PowerShell
Runspace
- Open the
Runspace
- Create a PowerShell
Pipeline
object with theImport-CliXml
command - Invoke the
Pipeline
- Close the
Runspace
123456var rs = RunspaceFactory.CreateRunspace();rs.Open();var pl = rs.CreatePipeline(@"Import-CliXml c:\DATA\Final.xml;");var result = pl.Invoke();rs.Close();