Powershell run job at startup with admin rights using ScheduledJob

a 夏天 提交于 2019-11-26 21:30:11

问题


To ease some of my work I have created a powershell script which needs to :

  1. Run at startup.
  2. Run with admin rights as it has to write in c:\program files folder.

I created the startup service using powershell like this :

function MakeStartupService
{
    Write-Host "Adding script as a startup service"
    $trigger = New-JobTrigger -AtStartup -RandomDelay 00:00:15
    Try
    {
      Register-ScheduledJob -Trigger $trigger -FilePath "absolute_path" -Name "Job-name" -EA Stop
    }
    Catch [system.exception]
    {
        Write-Host "Looks like an existing startup service exists for the same. Overwriting existing job"
        Unregister-ScheduledJob "Job-name"
        Register-ScheduledJob -Trigger $trigger -FilePath "absolute_path" -Name "Job-name"
    }
}

The job is registered as a startup service successfully and is visible inside task scheduler. If I start it using Start-Job -DefinitionName Job-name or by right clicking from Task Scheduler, it works fine but it doesn't start when windows starts.

Currently I am testing this on my personal Windows 10 system, and have checked in another windows 10 system but the behavior remained name. I am attaching screenshot of task scheduler window for this job.

Sorry if this questions sounds repeated or dumb (I am a beginner in powershell), but believe me, none of the solutions I found online worked for this.

Thanks in advance !!


回答1:


This is code that is already in production that I use. If it does not work for you, you must have something else going on with your system.

function Invoke-PrepareScheduledTask
{
    $taskName = "UCM_MSSQL"
    $task = Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue
    if ($task -ne $null)
    {
        Unregister-ScheduledTask -TaskName $taskName -Confirm:$false 
    }

    # TODO: EDIT THIS STUFF AS NEEDED...
    $action = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument '-File "C:\Invoke-MYSCRIPT.ps1"'
    $trigger = New-ScheduledTaskTrigger -AtStartup -RandomDelay 00:00:30
    $settings = New-ScheduledTaskSettingsSet -Compatibility Win8

    $principal = New-ScheduledTaskPrincipal -UserId SYSTEM -LogonType ServiceAccount -RunLevel Highest

    $definition = New-ScheduledTask -Action $action -Principal $principal -Trigger $trigger -Settings $settings -Description "Run $($taskName) at startup"

    Register-ScheduledTask -TaskName $taskName -InputObject $definition

    $task = Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue

    # TODO: LOG AS NEEDED...
    if ($task -ne $null)
    {
        Write-Output "Created scheduled task: '$($task.ToString())'."
    }
    else
    {
        Write-Output "Created scheduled task: FAILED."
    }
}



回答2:


If it works, it's not a script problem. Assign it to the SYSTEM account or make a separate service account instead of the Gagan account shown. Make sure that service account has "Permission to run as batch job" in your local security policy.




回答3:


Please check the checkbox for "Run with highest privileges" for the task in the task scheduler and try again. Currently in the screenshot above it is unchecked.

I have circled it below in red for your easy reference:




回答4:


Here is a blog article from Microsoft what you can check, if your scheduled task is not running: https://blogs.technet.microsoft.com/askperf/2015/02/18/help-my-scheduled-task-does-not-run/

As also mentioned in other answeres here it also says:

  • Try running your script with the SYSTEM account


来源:https://stackoverflow.com/questions/36846688/powershell-run-job-at-startup-with-admin-rights-using-scheduledjob

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!