Link to home
Create AccountLog in
Powershell

Powershell

--

Questions

--

Followers

Top Experts

Avatar of janhoedt
janhoedt

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

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of Panagiotis ToumpaniarisPanagiotis Toumpaniaris🇬🇷

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 J0rtITJ0rtIT🇻🇪

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Avatar of janhoedtjanhoedt

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.

Avatar of J0rtITJ0rtIT🇻🇪

Post the error you're getting?

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


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.

Avatar of J0rtITJ0rtIT🇻🇪

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.
Powershell

Powershell

--

Questions

--

Followers

Top Experts

Windows PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language built on the .NET Framework. PowerShell provides full access to the Component Object Model (COM) and Windows Management Instrumentation (WMI), enabling administrators to perform administrative tasks on both local and remote Windows systems as well as WS-Management and Common Information Model (CIM) enabling management of remote Linux systems and network devices.