Search code examples
powershellwindow

How to run a msi file in windows when scheduleded from administrator as a user.?


I have a msi file which I am able to push to users connected to the network. I am able to run it from Task Scheduler in widows as an admin.

But is it possible to schedule a task from admin and run that msi file as a user in windows .

In short:

1, I am installing and msi application to users connected in the domain. (The application is getting installed)

  1. Run the application as a user when the users in the domain are logged on or working. (I am able to run the installed script as an admin user)

What I want is to run the installed file as the user who will or is logged in to that windows computer.

$taskName = "My Task2"

 

$taskDescription = "This task runs every 10 second"

 

# Define the task trigger
$trigger = New-ScheduledTaskTrigger -User Sanket.Wagh -AtLogOn
# Define the task action
$taskAction = New-ScheduledTaskAction -Execute "<any application path>"

 
#-Argument "-Command 'Write-Host Hello World'"

 

# Register the task
Register-ScheduledTask -TaskName $taskName -Trigger $trigger -Action $taskAction -Description $taskDescription

here user sanket.wagh is static. I want this to be dynamic so it will be for all user where I push the script as an IT-admin

I am completely new to powershell but any help or documentations related to this would be highly appreciated.


Solution

  • Create a scheduled task principal.

    Use a scheduled task principal to run a task under the security context of a specified account.

    By using the interactive users groupid "S-1-5-4" it should get what you want.

    Users who log on for interactive operation. This is a group identifier added to the token of a process when it was logged on interactively.

    Your modified code. The task will run when any user is logged in.

    # Define the task trigger
    $trigger = New-ScheduledTaskTrigger -AtLogOn
    # Define the task action
    $taskAction = New-ScheduledTaskAction -Execute "<any application path>"
    
    # create a scheduled task princiapl. Specify the interactive users group id. 
    $principal = New-ScheduledTaskPrincipal -GroupId "S-1-5-4"
     
    #-Argument "-Command 'Write-Host Hello World'"
    
     
    
    # Register the task. add the "principal" parameter
    Register-ScheduledTask -TaskName $taskName -Trigger $trigger -Action $taskAction -Description $taskDescription -Principal $principal
    

    The created task

    scheduled task

    enter image description here