Operating system: 64 bit windows enterprise
Hi,
I'm creating a GUI that sits in the systemtray. When it is clicked, it shows you computer information.
I have "drawn" the XAML with visual studio and i use Powershell Studio to make it into an EXE.
This all works fast and just like i want it.
Now the tool is meant to serve a second purpose, being an emergency communication platform with toast notifications.
A second script i wrote, copies a JSON file to a location on the computers harddrive, at the moment for testing this is just c:\temp\json.json
The GUI has a function called ShowMessage. This uses the build-in toast notification system provided by MS.
The function goes as follows. (some words are in Dutch)
- $global:file = "C:\Temp\json.json"
- # start listening
- FUNCTION global:ShowMessage
- {
- if (Test-Path -Path $file)
- {
- $script:file = "C:\Temp\json.json"
- $message = Get-Content $file | convertfrom-json
- # if body text is too long, trim and add 3 dots
- if ($message.Body.Length -gt 115)
- {
- $message.body = $message.Body.Substring(0, 115) + "..."
- }
- $app = 'Microsoft.Windows.Computer'
- #$app = 'windows.immersivecontrolpanel_cw5n1h2txyewy!microsoft.windows.immersivecontrolpanel'
- [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())
- switch ($message.Status)
- {
- 'beschikbaar' { $image = "$($env:TEMP)\beschikbaar.png" }
- 'ONBESCHIKBAAR' { $image = "$($env:TEMP)\onbeschikbaar.png" }
- 'storingen' { $image = "$($env:TEMP)\storingen.png" }
- }
- write-host 'Activated showmessage'
- ''
- write-host $($message.Title)
- write-host $($message.Status)
- write-host $($message.Body)
- ''
- [xml]$ToastTemplate = @"
- <toast launch="app-defined-string" >
- <visual>
- <binding template="ToastGeneric">
- <text>$($message.Title) - $($message.Status) </text>
- <text>$($message.Body)</text>
- <text>$($message.Date)</text>
- <image placement="appLogoOverride" src="$($image)"/>
- <text placement="attribution">$($message.Label)</text>
- </binding>
- </visual>
- </toast>
- "@
- $ToastXml = New-Object -TypeName Windows.Data.Xml.Dom.XmlDocument
- [void]$ToastXml.LoadXml($ToastTemplate.OuterXml)
- $notify = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($app).Show($ToastXml)
- Remove-Item $file -Force -Confirm:$false
- } # END IF TEST PATH
- } # END FUNCTION
- $timer = New-Object Timers.Timer
- $timer.Interval = 10000 # fire every 10s
- $timer.Enabled = $true
- $timer.AutoReset = $true
- $timer.start()
- Get-EventSubscriber -SourceIdentifier ShowMessage | Unregister-Event
- Register-ObjectEvent -InputObject $timer -EventName Elapsed -SourceIdentifier showmessage -Action {showmessage}
As far as i can tell, this is due to runspaces. But i can't seem to figure out how to make this function 'act\\run' in the same runspace as the rest of my code, being the GUI.
So to further clarify, if i run the code above in ISE and i just place the JSON-file at the location C:\\temp, this works perfectly, time and time again. It's just not working within Powershell Studio and in the build EXE.
I hope i made my post clear enough as to what the problem is.