0

Very concisely writing a here string to file:

PS C:\Users\saunders\Desktop\misc>
PS C:\Users\saunders\Desktop\misc> @'
>> foo : {abc}
>> bar : {123}
>> baz : {a1b2c3}
>> '@ | Out-File here_string_output.txt
PS C:\Users\saunders\Desktop\misc>
PS C:\Users\saunders\Desktop\misc> cat .\here_string.txt
@'
foo : {abc}
bar : {123}
baz : {a1b2c3}
'@
PS C:\Users\saunders\Desktop\misc>
PS C:\Users\saunders\Desktop\misc> $hereString = @'
>> foo : {abc}
>> bar : {123}
>> baz : {a1b2c3}
>> '@
PS C:\Users\saunders\Desktop\misc>
PS C:\Users\saunders\Desktop\misc> $hereString
foo : {abc}
bar : {123}
baz : {a1b2c3}
PS C:\Users\saunders\Desktop\misc>

How would the inverse, reading the contents of here_string.txt into the $hereString variable?

------------------------original form of question--------------------

Working with sample data:

PS C:\Users\saunders\Desktop\misc>
PS C:\Users\saunders\Desktop\misc> $recordTemplate = @'
>> 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 C:\Users\saunders\Desktop\misc>
PS C:\Users\saunders\Desktop\misc>
PS C:\Users\saunders\Desktop\misc> $recordTemplate
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 C:\Users\saunders\Desktop\misc>
PS C:\Users\saunders\Desktop\misc> cat .\recordTemplate.txt
$recordTemplate = @'
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 C:\Users\saunders\Desktop\misc>
PS C:\Users\saunders\Desktop\misc> $newRecordTemplate = Get-Content .\recordTemplate.txt
PS C:\Users\saunders\Desktop\misc>
PS C:\Users\saunders\Desktop\misc> $newRecordTemplate
$recordTemplate = @'
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 C:\Users\saunders\Desktop\misc>

How would the above template recordTemplate.txt be read as a text file for processing as a template with either ConvertFrom-String or ConvertFrom-StringData?

So that the template itself can be read from a file rather than typed directly into the interactive CLI.


The file being parsed is records.txt with the first two records being:

UserPrincipalName : LeeG@lazydev.com
DisplayName       : Lee Gu
Title             : jr engineer
UserType          : Member
IsLicensed        : True

UserPrincipalName : MeganB@lazydev.com
DisplayName       : Megan Bowen
Title             : recruiter
UserType          : Member
IsLicensed        : True

Yes, that's string data, and, yes, looking to utilize ConvertFrom-String or ConvertFrom-StringData but first need to read a template from file. The template is, I think, a here string.

  • 1
    I think what you want is a dictionary of users where the key is the user name and the value is a hash table. Is each user in a separate file or are the users combined into one file? It would be better to post a sample of multiple users in one file. – jdweng Feb 03 '23 at 01:04
  • I'm updating this q in a few minutes. It's more than a bit jumbled. – Nicholas Saunders Feb 03 '23 at 05:19
  • 1
    A few years ago, I built a small function that plugs a table of actual values into a template text to produce a series of expansions. The actual values come from a CSV file. The template comes from a text file as you suggest in your question. The name of the function is Expand-Csv. you can find it on github and in here. It's different from what you are looking for, but it might suggest some ideas. – Walter Mitty Feb 09 '23 at 12:31

0 Answers0