0

my request is a bit special but quite simple to understand.

I get a string that I would like to exploit as a hashtable but I can't, my format is quite special and I haven't been successful with methods like ConvertFrom-StringData for example.

format preview:

@{id = 04cc943e-14a8-4bf6-8601-bd6fde3cc229; name = 7.10 10-16 - 6.0 * * * * | 1 0 0 0; privileges =}

I would therefore like to be able to access the content of the name attribute. I spent a lot of time there but without success ... Thank you for your help

mklement0
  • 382,024
  • 64
  • 607
  • 775
Kevin Torres
  • 15
  • 1
  • 3
  • 2
    Show us what you’ve tried. As it’s written I fear this post may get closed. – Doug Maurer Nov 19 '21 at 16:33
  • 1
    I've done it this time, but in the future please [format your post properly](https://stackoverflow.com/help/formatting). – mklement0 Nov 19 '21 at 17:42
  • The format is the hashtable-_like_, for-display-only string representation of a `[pscustomobject]` object, as described in [this answer](https://stackoverflow.com/a/53107600/45375). Do you really only have the string representation, and not the object itself? – mklement0 Nov 19 '21 at 17:47

2 Answers2

0

So you may go with a string parsing and use combination of splitting and trimming.

$text = "@{id = 04cc943e-14a8-4bf6-8601-bd6fde3cc229; name = 7.10 10-16 - 6.0 * * * * | 1 0 0 0; privileges =}"

#this is to get rid of @{}
$preparedText = $text.Substring(2,$text.Length-3)
$obj = @{}

foreach ($kv in $preparedText.Split(";")) { 
    #Get key from string (which is "key = value")
    $key = $kv.Trim().Split("=")[0].Trim()
    Write-Host "Key = $key"
    #Get value from string
    $value = $kv.Trim().Split("=")[1].Trim()
    Write-Host "Value = $value"

    #Assign key-object pair to hashtable
    $obj[$key] = $value
}

Write-Host "obj.name = $($obj.name);`nobj.id = $($obj.id);`nobj.privileges = $($obj.privileges)"

Which returns:

obj.name = 7.10 10-16 - 6.0 * * * * | 1 0 0 0;
obj.id = 04cc943e-14a8-4bf6-8601-bd6fde3cc229;
obj.privileges =
Karolina Ochlik
  • 908
  • 6
  • 8
0

An alternative approach is to use the convertfrom-stringdata cmdlet, documented here: Link to official MS documentation

The ConvertFrom-StringData cmdlet converts a string that contains one or more key and value pairs into a hash table. Because each key-value pair must be on a separate line, here-strings are often used as the input format. By default, the key must be separated from the value by an equals sign (=) character.

Using the OP's example as in @Karolina Ochlik's post:

$text = "@{id = 04cc943e-14a8-4bf6-8601-bd6fde3cc229; name = 7.10 10-16 - 6.0 * * * * | 1 0 0 0; privileges =}"
#this is to get rid of @{}
$preparedText = $text.Substring(2,$text.Length-3)
# replace semicolon with newline - `n needs double quotes
$preparedText.replace(';',"`n") | ConvertFrom-StringData

This creates the output:

Name                           Value
----                           -----
name                           7.10 10-16 - 6.0 * * * * | 1 0 0 0
id                             04cc943e-14a8-4bf6-8601-bd6fde3cc229
privileges

the real work is done in the single line

$preparedText.replace(';',"`n") | ConvertFrom-StringData

I hope this helps someone.

user8150417
  • 61
  • 1
  • 6