Register-ScheduledTask in WinForm complied exe

This forum can be browsed by the general public. Posting is limited to current SAPIEN license holders with active maintenance and does not offer a response time guarantee.
Forum rules
DO NOT POST LICENSE NUMBERS, ACTIVATION KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
User avatar
crazykroger
Posts: 7
Last visit: Mon Mar 15, 2021 7:16 am

Register-ScheduledTask in WinForm complied exe

Post by crazykroger »

Product: PowerShell Studio 2018 (64 Bit)
Build: v5.5.155
OS: Windows 10 Enterprise (64 Bit)
Build: v10.0.17763.0

#############

I have been working on trying to schedule a task one day after the form closes to delete the directory that the EXE is residing in. If I run the code outside of the form it works fine, also if I export the project to a ps1 file it works fine. I am using the default manifest for elevation considering I need admin rights to perform the tasks of the form. Below is the code but I think something is wrong in the settings of exe.

Code: Select all

$form_FormClosed=[System.Windows.Forms.FormClosedEventHandler]{
	$TaskName = 'Task'
	Get-ScheduledTask -TaskName $TaskName | Unregister-ScheduledTask -Confirm:$false
	$Action = New-ScheduledTaskAction -Execute powershell.exe -Argument "-noprofile -windowstyle hidden -executionpolicy bypass -command `"& {Remove-Item $ScriptDirectory -Recurse -Force}`""
	$Trigger = New-ScheduledTaskTrigger -Once -At ((Get-Date).AddDays(1))
	$Trigger.EndBoundary = ((Get-Date).AddDays(2).ToString('s'))
	$Setting = New-ScheduledTaskSettingsSet -Hidden -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable -DeleteExpiredTaskAfter (New-TimeSpan -Seconds 0)
	Register-ScheduledTask -TaskName $TaskName -Action $Action -Trigger $Trigger -Settings $Setting -User 'NT AUTHORITY\SYSTEM'
}
***Edit - I have tried removing -User from Register-ScheduledTask and Importing the ScheduledTask module however neither work.
User avatar
crazykroger
Posts: 7
Last visit: Mon Mar 15, 2021 7:16 am

Re: Register-ScheduledTask in WinForm complied exe

Post by crazykroger »

davidc wrote: Thu Oct 11, 2018 1:41 pm [TOPIC MOVED TO POWERSHELL GUIS FORUM BY MODERATOR]
I don't think this needs to reside in this forum. Since the code works fine in a PS1 file as a winform however not complied. I think it's something either in the settings of packager or a bug in PSStudio.
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Register-ScheduledTask in WinForm complied exe

Post by davidc »

Something functions differently in a package executable. For example, getting the path of the executing script file is different in an executable. For that you will need to use the Get-ScriptDirectory function (in snippet of the same name).

As for packager settings, make sure you are using the same platform settings as in PowerShell Studio. For example, use a 64-Bit platform on V5. Use an elevation manifest, if you are running the script in PowerShell Studio using elevation.

FYI, I moved the topic back.
David
SAPIEN Technologies, Inc.
User avatar
crazykroger
Posts: 7
Last visit: Mon Mar 15, 2021 7:16 am

Re: Register-ScheduledTask in WinForm complied exe

Post by crazykroger »

davidc wrote: Thu Oct 11, 2018 2:27 pm As for packager settings, make sure you are using the same platform settings as in PowerShell Studio. For example, use a 64-Bit platform on V5. Use an elevation manifest, if you are running the script in PowerShell Studio using elevation.
Thanks for that I didn't even think about the version. I try to keep the lowest possible PS version. Updated to PS3.0 winform and it works.
This topic is 5 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.