2

I am writing a PowerShell script that displays a WPF GUI to the user. The Task Scheduler is supposed to run this script every day at a certain time.

The script works fine when I start it manually, but when I schedule it with the Task Scheduler the GUI won't show. The task will enter 'running' state for about half a minute, then exit without errors. During this time it shows up as background process in the Task Manager.

The task is run as SYSTEM user and starts a powershell.exe with these arguments:

-NonInteractive -NoProfile -Command "& { C:\path\to\script.ps1 }"

At first I was getting the error described in this blog post and the provided instructions fixed it. Now I only see in that the SYSTEM user was authenticated and that a Powershell was invoked with the arguments from the task.

Can anybody offer any advice on how to make the WPF window visible or what the cause for this error may be?

I am running PowerShell v3 with .NET version 3.5. I have tested this on Windows Server 2016 and 2012.

Powershell script: https://pastebin.com/xLw76arr

Exported Task XML: https://pastebin.com/JRTkeFSA

mvrma
  • 143
  • 1
  • 9

2 Answers2

2

Could you show the code used to display the WPF form ?

  • Why using SYSTEM account ?
  • Why using the -NonInteractive parameter of powershell.exe ?

You should use a logged user to see the WPF form during execution.

  • I tried running it with my account, but it didn't work either. I don't want the user to use the powershell for anything else, beside showing the GUI. – mvrma Apr 24 '17 at 14:40
  • i got it to run by setting the 'Run only when is logged on' and using my account. I'll mark you as answer. – mvrma Apr 24 '17 at 15:42
0

PowerShell.exe doesn't load the WPF assemblies. You need to force load the assemblies in the script:

Add-Type –AssemblyName PresentationFramework
Add-Type –AssemblyName PresentationCore
Add-Type –AssemblyName WindowsBase
omniomi
  • 130
  • 1
  • 5