0

Creating the file from the console:

PS /home/nicholas/powershell> 
PS /home/nicholas/powershell> @'                        
>> UserPrincipalName : {UserPrincipalName*:LeeG@lazydev.com}
>> DisplayName       : {DisplayName:Lee Gu}
>> Title             : {Title:jr engineer}
>> UserType          : {UserType:Member}
>> IsLicensed        : {IsLicensed:True}
>> 
>> UserPrincipalName : {UserPrincipalName*:MeganB@lazydev.com}
>> '@ | Out-File full_template.psd1  
PS /home/nicholas/powershell> 
PS /home/nicholas/powershell> cat ./full_template.psd1  
UserPrincipalName : {UserPrincipalName*:LeeG@lazydev.com}
DisplayName       : {DisplayName:Lee Gu}
Title             : {Title:jr engineer}
UserType          : {UserType:Member}
IsLicensed        : {IsLicensed:True}

UserPrincipalName : {UserPrincipalName*:MeganB@lazydev.com}
PS /home/nicholas/powershell> 

but how is that file data imported?

This looks to be a "here string" but the usage, in particular, of the colon isn't clear from the documention I've referenced on data files for PowerShell.

  • 1
    thats not `psd1` it's just a template used by [Mathias in his answer](https://stackoverflow.com/a/75304609/15339544) that `ConvertFrom-String` can use as template to parse other strings with a similar format. What you're exporting to a file cannot be interpreted as objects by PowerShell no matter what extension you put it. – Santiago Squarzon Feb 03 '23 at 06:12
  • Yes, I'm looking to import the template used by Mathias from file on the interactive powershell CLI. – Nicholas Saunders Feb 03 '23 at 21:32
  • The obsolescent [`ConvertFrom-String`](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/convertfrom-string) provides separator-based parsing as well as _heuristics_-based parsing based on templates with example values. The separator-based parsing applies automatic type conversions you cannot control, and the behavior of the poorly documented template approach is inherently hard to predict - it's best to avoid this cmdlet altogether. It is no longer available in PowerShell (Core) 7+, except on Windows - indirectly, via the Windows PowerShell Compatibility feature. – mklement0 Feb 05 '23 at 04:06

1 Answers1

1

I used a string as input with code below for testing

$properties = @"
UserPrincipalName : {UserPrincipalName*:LeeG@lazydev.com}
DisplayName       : {DisplayName:Lee Gu}
Title             : {Title:jr engineer}
UserType          : {UserType:Member}
IsLicensed        : {IsLicensed:True}
"@
$reader = [System.IO.StringReader]::new($properties)

$table = [System.Collections.ArrayList]::new()

$pattern = "\{(?<name>[^:]+):(?<value>[^}]+)"

while(($line = $reader.ReadLine()) -ne $null)
{
   $match = $line | Select-String -Pattern $pattern
   $name = $match.Matches.groups['name'].value
   $value = $match.Matches.groups['value'].value
   $newRow = New-Object -TypeName psobject
   $newRow | Add-Member -NotePropertyName Name -NotePropertyValue $name
   $newRow | Add-Member -NotePropertyName Value -NotePropertyValue $value
   $table.Add($newRow)  | Out-Null   
}
$table

From a file

$input_filename = "c:\temp\test.txt"

$pattern = '\{(?<name>[^:]+):(?<value>[^}]+)'
$match = Select-String -Path $input_filename -Pattern $pattern

$table = [System.Collections.ArrayList]::new()

$newRow = New-Object -TypeName psobject
foreach($row in $match.Matches)
{
  
   $name = $row.groups[1].value
   $value = $row.groups[2].value
Write-Host "name = " $name "value = " $value

   $newRow | Add-Member -NotePropertyName $name -NotePropertyValue $value 
}
$table.Add($newRow)  | Out-Null  
$table | Format-Table
jdweng
  • 33,250
  • 2
  • 15
  • 20