nchounlapane
Posts: 28
Joined: Tue Mar 31, 2020 10:01 pm

Executing custom actions after reboot

Tue Mar 31, 2020 10:30 pm

Is there a way to execute custom actions after a forced reboot? I am trying to delete the user folder however the whole folder cannot be deleted due to NTUSER.DAT and USRCLASS.DAT files being locked up from the system and the only way to delete them is to reboot the computer. I have checked "Delete user profile folder when user account is deleted" but those two files still remained after uninstall.

I have a custom action to set installer property REBOOT to FORCE and another custom action to remove the user folder but it does not work and is giving me "This installation package could not be opened..." error.

EDIT: This is all in attempt of an uninstall with removing the user account that was made from the installer.

Daniel
Posts: 8237
Joined: Mon Apr 02, 2012 1:11 pm
Contact:  Website

Re: Executing custom actions after reboot

Fri Apr 03, 2020 1:26 pm

Hi and welcome to our forums,

As a possible solution you can try:

1. to set, within the same custom action, the REBOOT property to "FORCE" and another property (e.g. STATUS_PROP) to "REBOOTING" value.

2. then within your second custom action you should just check to see if there is any "reboot_done" registry value on disk (that means a reboot was done previously) and so continue with your custom action coed execution and on exit delete the "reboot_done" registry value.

3. finally use a third custom action to write a registry value (e.g. reboot_done) using the following condition:

Code: Select all

STATUS_PROP = "REBOOTING"
Hope this helped.

All the best,
Daniel
Daniel Radu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

nchounlapane
Posts: 28
Joined: Tue Mar 31, 2020 10:01 pm

Re: Executing custom actions after reboot

Fri Apr 03, 2020 9:18 pm

Dan,

Thank you for your response. I would like to ask some clarification questions:

1. I'm a little unclear on this. So within the same custom action of setting the REBOOT property to FORCE, I am also setting a property called STATUS_PROP to REBOOTING? Or should that be a separate custom action that follows the first one?

I currently have the custom action (named ForceReboot) right before the FINISH EXECUTION step within the INSTALL EXECUTION STAGE.

2. So for this step, we are assuming that the reboot has already taken place. Once logon has been taken place, it is expected that the custom action (deletion of user folder) should run at this point. During the custom action execution, we are supposed to be checking a registry value to see if a reboot has been taking place? Where should this custom action be placed in sequence? After FINISH EXECUTION?

Nick

Catalin
Posts: 6537
Joined: Wed Jun 13, 2018 7:49 am

Re: Executing custom actions after reboot

Mon Apr 13, 2020 5:06 pm

Hello Nick,

First of all, please accept my apologies for such a delayed reply.

You can execute your custom action after a reboot through a Windows Scheduled Task. For instance, you can wrap up your custom action in the form of a script file and execute it at the next log-on.

Here is a possible way in which you can achieve what you want:

- first of all, we will need to create a .BAT file that will do what you need (e.g. deletes a specific folder)

- upon uninstallation, we will have a custom action that will do the following:

1. set the REBOOT property to FORCE

2. creates a scheduled task that calls cmd.exe and invokes the .BAT file

Important: Besides containing your logic, the .BAT file should also delete the scheduled task after it executes (we want the scheduled task to execute only once).

The above leads to the following: in order to create and delete a scheduled task, one must have elevated privileges (administrator privileges). With that in mind, the "Run as administrator"option should be checked in the "Install Parameters" page & the user should be an administrator.

The .BAT file should be added in your package (in "Files and Folders" page) as a temporary file. After adding the file, please double click on it and check the "Do not remove the file when setup ends" option. In addition to this, please pay attention to its "Property" field as this is the property that stores the .BAT file path (which we will further use in our script).

Now that the .BAT file was correctly added to the project, it is time to create the custom action that further creates the scheduled task.

Quick Info: Task Scheduler is a component of Microsoft Windows that provides the ability to schedule the launch of programs or scripts at pre-defined times or after specified time intervals.

Basically, what I have until now (in my tests) is a .BAT file that looks as it follows:

Name: test.bat
Content:

Code: Select all

mshta.exe vbscript:Execute("msgbox ""HelloWorld"",0,""HelloMsgBox"":close")
schtasks.exe /DELETE /TN MyTask /F
All it does is to spawn a message box and then delete a scheduled task named "MyTask".

Now coming back to our main script. Besides setting the REBOOT property to FORCE, your custom action should also create a scheduled task that runs the earlier mentioned .BAT file. In my testing, I have used a PowerShell script that does that. The script is as it follows (please use it as an example only - as I am not quite sure this is a production-ready script):

Code: Select all

# Block for declaring the script parameters.
Param()

# Your code goes here.

$bat = AI_GetMsiProperty AI_TEST.BAT

$schTaskAction = New-ScheduledTaskAction -Execute cmd.exe -Argument "/k $bat"
$trigger = New-ScheduledTaskTrigger -AtLogOn
$principal = New-ScheduledTaskPrincipal -GroupId "BUILTIN\Administrators" -RunLevel Highest
$schTask = New-ScheduledTask -Action $schTaskAction -Trigger $trigger -Principal $principal
Register-ScheduledTask -TaskName MyTask -InputObject $schTask

AI_SetMsiProperty REBOOT FORCE
The script can be added through a PowerShellScriptInline custom action, with sequence, scheduled after the "Finish Execution" action group.

The above script will create a scheduled task that will run the .BAT file at the next LogOn. This bat file firstly executes its logic and then deletes the scheduled task.

The .BAT file is launched using the "/k" argument, which means "launch cmd.exe, execute the .BAT file and then wait for user input". If you want the command line interpreter to terminate automatically, you could use the "/c" parameter instead.

As explained above, all of these tasks require the user running the custom aciton (implicitly running the uninstall process) to be an administrator.

Hope you will find this information useful.

Best regards,
Catalin
Catalin Gheorghe - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

nchounlapane
Posts: 28
Joined: Tue Mar 31, 2020 10:01 pm

Re: Executing custom actions after reboot

Fri May 08, 2020 9:23 pm

Catalin,

I appreciate your solution! It seems to work within our workflow with a few tweaks. Thanks!

Regards,
Nick

Catalin
Posts: 6537
Joined: Wed Jun 13, 2018 7:49 am

Re: Executing custom actions after reboot

Mon May 11, 2020 11:34 am

You are always welcome, Nick!

I am really glad the provided solution was of help for you.

Best regards,
Catalin
Catalin Gheorghe - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

Return to “Common Problems”