Link to home
Start Free TrialLog in
Avatar of janhoedt
janhoedt

asked on

PS: Scheduled task

Hi,

I have a PS script which sets a PS scheduled task BUT it lacks some things = setting username to run with + 'run whether user is logged on or not', configure task for Windows 2012 R2 instead of default ".

Can't figure out howto do this in Powershell.

Please advise.

This is the script:


 
function new-PSScheduledTask
{

  param(

    [Parameter(Mandatory = $true)][string[] $Computername,      
    [Parameter(Mandatory = $true)][string] $ScriptPath,
    [Parameter(Mandatory = $true)][string]$FunctiontoExecute,
    [Parameter(Mandatory = $true)][string]$Taskname,
    [Parameter(Mandatory = $false)]$TriggerTime = '6:00'  
  )
 
    invoke-command -ScriptBlock {
   
      $TriggerFrequency = @{ Daily = $true }
      $Trigger = New-ScheduledTaskTrigger -At $using:TriggerTime @TriggerFrequency
 
      $commandtoexecute = "& {. $using:ScriptPath;$using:functiontoexecute}"
      $argument = "-NoProfile -WindowStyle Hidden -command $commandtoexecute"
     
      $action = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument $argument
      Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $using:TaskName -Description $using:TaskName  -RunLevel Highest #-User $UsernameTorunScheduledJob
    } -ComputerName $Computername
   
    Write-Output "TO SET: username to run with + 'run whether user is logged on or not', configure for Windows 2012 R2 "
}
Avatar of Panagiotis Toumpaniaris
Panagiotis Toumpaniaris
Flag of Greece image

To run the task as a specific user, use New-ScheduledTaskPrincipal.

I believe that if you save the password, the task will run whether the user is logged in or not.
ASKER CERTIFIED SOLUTION
Avatar of J0rtIT
J0rtIT
Flag of Venezuela, Bolivarian Republic of image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of janhoedt
janhoedt

ASKER

Does not work
Cannot validate argument on parameter 'UserId'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.

Command is
 new-PSScheduledTask -Computername servername -ScriptPath 'e:\myscript.pst' -FunctiontoExecute 'Get-something' -Taskname 'sometask'

then it prompts for user and password but it throws an error.
Post the error you're getting?
Error is

Cannot validate argument on parameter 'UserId'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
Well this example works:
$action = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument '-NoProfile -WindowStyle Hidden -command "& {get-eventlog -logname Application -After ((get-date).AddDays(-1)) | Export-Csv -Path c:\Reports\applog.csv -Force -NoTypeInformation}"'
$trigger =  New-ScheduledTaskTrigger -Daily -At 11am
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "AppLog" -Description "Daily dump of Applog"

Open in new window

here's the source: https://social.technet.microsoft.com/Forums/lync/en-US/d87da3b2-ea3b-4b68-82f4-be0bf685446b/powershell-to-create-schedule-task-on-windows-10?forum=winserverpowershell

So It should be related to the task itself, so the error should be within the Scriptblock:
#$action = New-ScheduledTaskAction -Execute foo.exe -Argument "bar baz"
    $principal = New-ScheduledTaskPrincipal -UserId $UserName -LogonType ServiceAccount -RunLevel Highest
    $settings = New-ScheduledTaskSettingsSet -MultipleInstances Parallel 

    $TriggerFrequency = @{ Daily = $true }
    $Trigger = New-ScheduledTaskTrigger -At $using:TriggerTime @TriggerFrequency 
  
    $commandtoexecute = "& {. $using:ScriptPath;$using:functiontoexecute}"
    $argument = "-NoProfile -WindowStyle Hidden -command $commandtoexecute"
      
    $action = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument $argument
    Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $using:TaskName -Description $using:TaskName  -RunLevel Highest -Principal $UserName -Settings $settings -Password $pwd 

Open in new window

Will check it later.