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.