1

I have a Powershell script to add a new schedule task. It's used to launch a PS1 file which includes some SQL querying on both local SQL box and remote SQL boxes.

If this script creates the schedule, I have to make one change manually for it to work: I have to set the option "Run whether user is logged on or not".

Here is the function:

function createschedule {
    $startdatetime = (get-date).AddMinutes(1).ToString("HH:mm")
    $taskName = "My Bestest Schedule"
    $action = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument '-file "C:\mypath\bin\mypowershellscript.ps1"'
    $trigger = New-ScheduledTaskTrigger -Once -At $startdatetime
    $settings = New-ScheduledTaskSettingsSet -StartWhenAvailable
    echo ""
    echo "Adding Basic parameterized task..."
    Register-ScheduledTask -TaskName $taskName -Trigger $trigger -Action $action -Setting $settings -User "Administrator" -description "Sitewatch v2 Collector" -RunLevel 1 | Out-null
    $trigger.RepetitionInterval = (New-TimeSpan -Minutes 1)
    $trigger.RepetitionDuration = (New-TimeSpan -Days 1000)
    echo "Adding trigger to the new task..."
    Set-ScheduledTask $taskName -Trigger $trigger | Out-Null
    echo ""
}

When the script creates the entry, the command runs and pops up a window for a few seconds. It still works, but only if logged in. If I switch the setting manually, it works fine logged out.

Things I've tried/considered:

  • changed the user to NETWORKSERVICE, or SYSTEM but those cannot authenticate with SQL when doing the querying
  • There is a switch I found -logontype ServiceAccount but that doesn't seem to work
  • I tried using the 'Principal' portion: #$principal = New-ScheduledTaskPrincipal -GroupID "BUILTIN\Administrators" but that gives errors too
  • Lastly, I cannot simply supply the password because this will be run on different boxes that have different passwords for Administrator (or whatever admin use I set)

Looking at this post, I couldn't determine what I needed: How to set schedule.service "Run whether user is logged on or not" in Powershell?

Also, Microsoft appears to say that simply having a Principal, it enables a task to run whether the user is logged in or not but it doesn't work for me:

Detailed Description The New-ScheduledTaskPrincipal cmdlet creates an object that contains a scheduled task principal. Use a scheduled task principal to run a task under the security context of a specified account. When you use a scheduled task principal, Task Scheduler can run the task regardless of whether that account is logged on.

Found here: https://technet.microsoft.com/en-us/library/jj649825.aspx

Thx.

Community
  • 1
  • 1
Claude Lag
  • 113
  • 1
  • 2
  • 7

2 Answers2

5

I did some more searching and got some good examples to use.

function createschedule {
    $startdatetime = (get-date).AddMinutes(1).ToString("HH:mm")
    $jobname = "My bestest Schedule"
    $repeat = (New-TimeSpan -Minutes 1)
    $action = New-ScheduledTaskAction –Execute "$pshome\powershell.exe" -Argument  '-file "C:\mypath\bin\mypowershellscript.ps1"'
    $duration = ([timeSpan]::maxvalue)
    $trigger = New-ScheduledTaskTrigger -Once -At $startdatetime -RepetitionInterval $repeat -RepetitionDuration $duration
    $msg = "Enter the username and password that will run the task"; 
    $credential = $Host.UI.PromptForCredential("Task username and password",$msg,"$env:userdomain\$env:username",$env:userdomain)
    $username = $credential.UserName
    $password = $credential.GetNetworkCredential().Password
    $settings = New-ScheduledTaskSettingsSet -StartWhenAvailable -RunOnlyIfNetworkAvailable -DontStopOnIdleEnd
    Register-ScheduledTask -TaskName $jobname -Action $action -Trigger $trigger -RunLevel Highest -User $username -Password $password -Settings $settings | out-null
    echo ""
}

I believe my issue was that the "Run whether user is logged on or not" cannot be set unless a real user is used complete with password. With my code above, I prompt for credentials (which was ideal for my use case) and that was used to register. The setting "Run whether user is logged on or not" gets set properly now.

Claude Lag
  • 113
  • 1
  • 2
  • 7
0

Can you use SCHTASKS.exe directly with a command like follows.

$newtask = {SCHTASKS /create /v1 /s $ComputerName /ru "Administrator" /rp "MyPlanTextPass" /sc once /tn "My BesTest Schedule" /st "23:00:00" /tr "C:\Temp\Test.bat"}

Invoke-Command $newtask 

I don't have the other task scheduling cmdlets to try anything else out so I figured I would throw this out as an option.

ATek
  • 815
  • 2
  • 8
  • 20