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)

Open in new window

LVL 2
Glenn MSystems Engineer / ManagerAsked:
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.

footechCommented:
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>* Notification *</text>
      
<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>
"@

Open in new window

When the code is run, the here-string is evaluated, including any variables or subexpressions contained in it.
0
oBdACommented:
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)

Open in new window


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 ...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Glenn MSystems Engineer / ManagerAuthor Commented:
Both solutions are correct and helpful. Thank you!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.