Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
945 views
in Technique[技术] by (71.8m points)

windows - Powershell run job at startup with admin rights using ScheduledJob

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. enter image description here

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 !!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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."
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...