0
votes

I have a process parsing out a number of CSV documents and saving changes for later processing in a database. When one row (a person) in any of the CSVs isn't found in the person database, the process saves off that record from the CSV. I currently have it saving as the string output of the hashtable. So CSV document

First,Last,Office
Joe,Smith,Detroit

The output from import-csv "path to my doc.csv" when using the debugger in VS 2015 the string is visualized as:

@{First=Joe; Last=Smith; Office=Detroit;}

It also expands out to be a key/value set of:

First  | Joe
Last   | Smith
Office | Detroit

In powershell, I'm able to just use the names objects in the table as $obj.First and move on with things. But I'm not able to save that string to a database, and bring it back as a usable powershell object. I always end up with the string literal.

Is there a way to convert this string back to a hashtable, or other key/value pair?

I've given this a try:

$data = ConvertFrom-String $raw -PropertyNames First,Last,Office

I've tried that on both the raw string as is, which just gives the string back, with the property names in it. I've also converted the original hash to an array:

[string[]]$l_array = $raw | out-string

Then when I convert it back using the ConvertFrom-String command, it is actually shifting the data elements and adding the keys, as values.

Thanks for the help. I can provide more details, or sample code as needed.

Edit The other side of this, is, how could I store the data that is currently a PSCustomObject, in a better format, that I can rebuild into the same PSCustomObject? I thought perhaps Export-Csv might work, but you can't export to a string. At least as far as I can tell.

1
Import-Csv produces an array of PSCustomObjects not a hash table. Can you explain what "when looking" means? How are you viewing the result? - Dave Sexton
I may be wrong but isn't the PSCustomObjects create utilized the same as a hashtable? I'll update the post to clarify. - Tim Meers
@DaveSexton I also update the title to reflect your note of PSCustomObject, not hash table. - Tim Meers

1 Answers

0
votes

What you are looking for is called serialization. A straight-forward way is to use built-in object serialization cmdlets: Export-CliXml and Import-CliXml.

The former will save your object as an XML file. This can be saved on disk as well as in a database. When you need the object later on, retrieve the XML document and re-create the object by deserializing it. Like so,

# Create simple object
$o = New-Object -TypeName PSObject
$o | Add-Member -Name "Attr1" -Value "Val1" -MemberType NoteProperty
$o | Add-Member -Name "Attr2" -Value "Val2" -MemberType NoteProperty
$o

Attr1 Attr2
----- -----
Val1  Val2

# Serialize it
Export-Clixml -Path ./myObj.xml -InputObject $o

# Deserialize as another object, print it
$p = Import-Clixml -Path .\myObj.xml
$p

Attr1 Attr2
----- -----
Val1  Val2

# Look at file contents for magic!    
Get-Content .\myObj.xml
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
  <Obj RefId="0">
    <TN RefId="0">
      <T>System.Management.Automation.PSCustomObject</T>
      <T>System.Object</T>
    </TN>
    <MS>
      <S N="Attr1">Val1</S>
      <S N="Attr2">Val2</S>
    </MS>
  </Obj>
</Objs>