# Import content of text file into powershell script before executing?

I need a way to simply the <text> portion of my Powershell Script that makes a TOAST command. I'm trying to prevent people from making mistakes to the script when being edited.

Is there a way to import the body of a text file and have it imported into the PowerShell script where you find  <text> *** </text> ?

Here is my PS so far:

$app = '{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\WindowsPowerShell\v1.0\powershell.exe' [Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime]$Template = [Windows.UI.Notifications.ToastTemplateType]::ToastImageAndText01

#Gets the Template XML so we can manipulate the values
[xml]$ToastTemplate = ([Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent($Template).GetXml())

[xml]$ToastTemplate = @" <toast launch="action=viewAlarm&amp;alarmId=3" scenario="alarm"> <visual> <binding template="ToastGeneric"> <text>* Notification *</text> <text>ALERT - Faxing Services are in the Cloud, It's Raining and therefore could faxing no longer works. Stay Tuned for more Information!</text> <image placement="appLogoOverride" hint-crop="circle" src="https://picsum.photos/64?image=000"/> <image placement="hero" src="http://finalbca.16mb.com/noti2.png"/> </binding> </visual> <actions> <action activationType="system" arguments="snooze" content=""/> <action activationType="background" arguments="dismiss" content="Dismiss"/> </actions> </toast> "@$ToastXml = New-Object -TypeName Windows.Data.Xml.Dom.XmlDocument
$ToastXml.LoadXml($ToastTemplate.OuterXml)

$notify = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($app)

$notify.Show($ToastXml)

LVL 2
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
If I'm not mistaken, line 7 is superfluous, since the variable is overwritten by line 9.

You can have the code read in the text file as part of the here-string.
$alertFile = ".\alert.txt" [xml]$ToastTemplate = @"
<toast launch="action=viewAlarm&amp;alarmId=3" scenario="alarm">

<visual>
<binding template="ToastGeneric">

<text>$(Get-Content$alertFile | Out-String)</text>
<image placement="appLogoOverride" hint-crop="circle" src="https://picsum.photos/64?image=000"/>
<image placement="hero" src="http://finalbca.16mb.com/noti2.png"/>
</binding>
</visual>

<actions>

<action
activationType="system"
arguments="snooze"
content=""/>

<action
activationType="background"
arguments="dismiss"
content="Dismiss"/>

</actions>

</toast>
"@

When the code is run, the here-string is evaluated, including any variables or subexpressions contained in it.
Commented:
If you want to prevent mistakes, you can't just replace the InnerText with the text from the file. That would require that the user generating the text file properly escapes all symbols with a special meaning in XML. Without that, the script will fail if the text contains, for example, an ampersand (&).
Try this:
$app = '{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\WindowsPowerShell\v1.0\powershell.exe' [Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null$TextFile = "C:\Temp\text.txt"
$Text = [System.Security.SecurityElement]::Escape((Get-Content -Path$TextFile -Raw))

$Template = [Windows.UI.Notifications.ToastTemplateType]::ToastImageAndText01 # Gets the Template XML so we can manipulate the values$ToastTemplate = [Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent($Template).GetXml()$ToastTemplate = @"
<toast launch="action=viewAlarm&amp;alarmId=3" scenario="alarm">
<visual>
<binding template="ToastGeneric">
<text>$($Text)</text>
<image placement="appLogoOverride" hint-crop="circle" src="https://picsum.photos/64?image=000" />
<image placement="hero" src="http://finalbca.16mb.com/noti2.png" />
</binding>
</visual>
<actions>
<action activationType="system" arguments="snooze" content="" />
<action activationType="background" arguments="dismiss" content="Dismiss"/>
</actions>
</toast>
"@

$ToastXml = New-Object -TypeName Windows.Data.Xml.Dom.XmlDocument$ToastXml.LoadXml($ToastTemplate)$Notify = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($app)$Notify.Show(\$ToastXml)


footech,
line 7 is "sort of" required - it loads an assembly that is required to create Windows.Data.Xml.Dom.XmlDocument object. Haven't figured out yet how to do that explicitly ...

Experts Exchange Solution brought to you by