This is my powershell script that creates a simple task that repeats every hour.
$lSTName="logrotate_all0"
$lTSettings=New-ScheduledTaskSettingsSet -Compatibility Vista -MultipleInstances IgnoreNew -StartWhenAvailable
$lUser= "$env:USERDOMAIN\sssssssssssssss"
$lPW= "zzzzzzzzzzzzzzz"
$lAction= New-ScheduledTaskAction -Execute "c:\winapp\cygwin64\bin\bash.exe" -Argument "c:\winapp\bu\bin\logrotate.all.sh" -WorkingDirectory "c:\winapp\bu\bin"
$lTaskDescription="send logs"
$lTimeStart=(Get-Date).AddHours(1)
$lTriggerOnce= ( New-ScheduledTaskTrigger -At $lTimeStart -Once )
$lTrigger=$lTriggerOnce
$lTrigger.Enabled=$true
$lTrigger.RepetitionInterval = (New-TimeSpan -Hours 1)
$lTrigger.RepetitionDuration = (New-TimeSpan -Days 1)
#$lTrigger.Repetition.StopAtDurationEnd=$false
Register-ScheduledTask -TaskName $lSTName -Trigger $lTrigger -User $lUser -Password $lPW -Action $lAction -RunLevel Limited –Force -Settings $lTSettings -Description $lTaskDescription
In the Task Scheduler interface, the task trigger looks like this
But I need the "stop all running tasks at end of repetition duration" flag to be false
My question: How I can to set the state "StopAtDurationEnd" = $false in powershell?
I was read Create an idle trigger in PowerShell for Windows Task Scheduler but i cant reconfigure that script action to "once" or "starup" and i dont know how i can setup workdir into action object.
The New-ScheduledTaskTrigger is very poor to description and doesnt describe case "StopAtDurationEnd"
Related
I am trying to create a task scheduler like below
So far I came up with below code.
#Variables
$TaskName = "TestingTask"
$username ="user"
$password ='password'
$description = "This is a testing task"
#Create Scheduled Task
$Action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "E:\script.ps1"
$Trigger = New-ScheduledTaskTrigger -At 10AM -Weekly -WeeksInterval 2 -DaysOfWeek Friday
$taskPrincipal = New-ScheduledTaskPrincipal -UserId 'user' -RunLevel Highest
$ScheduledTask = New-ScheduledTask -Action $action -Trigger $trigger -Description $description -Principal $taskPrincipal
Register-ScheduledTask -TaskName $TaskName -InputObject $ScheduledTask -User $username -Password $password
Please let me know how can we schedule monthly like this.
From the help for the cmdlet Get-Help New-ScheduledTaskTrigger -full
Example 4: Register a scheduled task that starts every-other week
PS C:\>$Sta = New-ScheduledTaskAction -Execute "Cmd"
The second command creates a scheduled task trigger that starts every other Sunday at 3:00 A.M and assigns the ScheduledTaskTrigger object to the $Stt variable.
PS C:\>$Stt = New-ScheduledTaskTrigger -Weekly -WeeksInterval 2 -DaysOfWeek Sunday -At 3am
The third command registers the scheduled task Task01 to run the task action named Cmd every other Sunday at 3:00 A.M.
PS C:\>Register-ScheduledTask Task01 -Action $Sta -Settings $Stt
This example registers a scheduled task that starts every other week.
The first command creates a scheduled task action named Cmd and assigns the ScheduledTaskAction object to the $Sta variable.
I would assume that if you set -Weekly -WeeksInterval 4 should set the task to run once every 4 weeks, and should essentially make it a month. But you are right, there is no real option to identify a "month", you should try this out and see how it looks like the GUI.
Another thing to try is, to make a montly based task in the GUI and use Get-ScheduledTask to see how the interval is configured.
I am trying to figure out how to write a PowerShell script to find and run a scheduled task, or if it is not there then to create it. Here is what I have built so far. The else statement works but the first task does not.
$taskName = "crebackup"
$taskExists = Get-ScheduledTask | Where-Object {$_.TaskName -like $taskName}
if($taskExists) {
Start-ScheduledTask -TaskName "crebackup"}
else {
$action = New-ScheduledTaskAction -Execute “C:\POSNation\SQLBackup\sqlbackup.exe”
$Trigger = New-ScheduledTaskTrigger -Daily -At 3am
$Settings = New-ScheduledTaskSettingsSet
$Task = New-ScheduledTask -Action $Action -Trigger $Trigger -Settings $Settings
Register-ScheduledTask -TaskName 'crebackup' -InputObject $Task -User 'usernamehere' -Password 'passwordhere'}
In this case the sqlbackup.exe task needed the working directory to start in. This is displayed as "Start in (optional)" when you look at the task using Task Scheduler. To add that, use this for the $action variable:
$action = New-ScheduledTaskAction -WorkingDirectory "C:\POSNation\SQLBackup" -Execute "C:\POSNation\SQLBackup\sqlbackup.exe"
Also, make sure scripts don't have any smart quotes in them(“, ”). Smart quotes are often automatically generated by word processors and some websites. I replaced the smart quotes in the example above.
I need to write a script, which will create task scheduler job, which at pc startup will run executable. In short, script will be something like this:
$trigger = New-JobTrigger -AtStartup -RandomDelay 00:00:10
Register-ScheduledJob -Trigger $trigger -Name FileJob -ScriptBlock {$args[0]}
But it doesn't work. What's wrong?
I use the following Powershell snippet to create Windows Scheduled Tasks.
$action = New-ScheduledTaskAction -Execute 'application.exe' -Argument '-NoProfile -WindowStyle Hidden'
$trigger = New-ScheduledTaskTrigger -AtStartup -RandomDelay 00:00:10
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "FileJob" -Description "FileJob"
This is what I use to create my task:
#The first command uses the New-ScheduledTaskAction cmdlet to assign the action variable $A to the executable file tskmgr.exe
$A = New-ScheduledTaskAction -Execute "C:\WINDOWS\SysWOW64\WindowsPowerShell\v1.0\powershell.exe" -file "C:\directory\powershell.ps1"
#The second command uses the New-ScheduledTaskTrigger cmdlet to assign the trigger variable $T to the value AtLogon
$T = New-ScheduledTaskTrigger -weekly -DaysOfWeek Saturday -At 7am
#The third command uses the New-ScheduledTaskSettingsSet cmdlet to assign the settings variable $S to a task settings object
$S = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -DontStopOnIdleEnd -ExecutionTimeLimit 1:00:00 -MultipleInstances 2
#The fourth command assigns the principal variable to the New-ScheduledTaskPrincipal of the scheduled task, Domain\Username
$P = New-ScheduledTaskPrincipal -UserId domain\gMSAaccount$ -LogonType Password -RunLevel Highest
#The fifth command sets the description varible to $D for the task definition
$D = "Insert your description here"
#Registers the new scheduled task and defines it by using the $D variable
Register-ScheduledTask Name_of_Task -Action $A -Trigger $T -Settings $S -Principal $P -Description $D<p>
I found that using gMSA accounts are the best in an active directory environment. Implement gMSA The requirement is that in order to use gMSA, you must create your task with a powershell script or schtask.exe.
I am installing a scheduled task using a PowerShell script, but I would like it to retry 3 times on failure, how can I add this to my scheduled task registration script?
$dropLocation = "C:\Tasks\"
$Action = New-ScheduledTaskAction -Execute "$dropLocation\Task.exe"
$Trigger = New-ScheduledTaskTrigger -Daily -At 10:15pm
$Settings = New-ScheduledTaskSettingsSet -RestartCount:3
Register-ScheduledTask -Action $Action -Trigger $Trigger -TaskName "$taskName" -Settings $Settings -Description "TaskDescription"
Specifying the retry count is not enough. You have to specify two parameters:
The retry count and the retry interval.
To retry up to three times with an interval of 1 minute, your settings should look like this:
$Settings = New-ScheduledTaskSettingsSet -RestartCount:3 -RestartInterval (New-TimeSpan -Minutes 1)
Can anyone tell me how to create a scheduled task using powershell that runs as the local system or local service?
Everything works great except the call to ITaskFolder.RegisterTaskDefinition().
If I pass in $null, or "", than the call bombs saying invalid username or password.
Any thoughts"
$Rootfolder.RegisterTaskDefinition("Test", $Taskdef, 6, "LOCAL SERVICE", "", 3)
For those who can use PowerShell 3.0 on Windows 8 or Windows Server 2012, new cmdlets will let you do it in a simple way when registering your scheduled task with the cmdlet Register-ScheduledTask and as argument -User "System"
Here is a scheduled task created entirely with PS, its purpose is to restart a service My Service, using the SYSTEM account, 3 minutes after the system has started:
$taskname = "Restart My Service"
$taskdescription = "Restart My Service after startup"
$action = New-ScheduledTaskAction -Execute 'Powershell.exe' `
-Argument '-NoProfile -WindowStyle Hidden -command "& Restart-Service -displayname \"My Service\""'
$trigger = New-ScheduledTaskTrigger -AtStartup -RandomDelay (New-TimeSpan -minutes 3)
$settings = New-ScheduledTaskSettingsSet -ExecutionTimeLimit (New-TimeSpan -Minutes 2) -RestartCount 3 -RestartInterval (New-TimeSpan -Minutes 1)
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $taskname -Description $taskdescription -Settings $settings -User "System"
NB: you will need to run powershell as an administrator for that script.
I think you would need to use "nt authority\localservice" as the user name.
Kindness,
Dan
This code snippet will use the PowerShellPack's Task Scheduler module to schedule a task to run as SYSTEM immediately:
New-Task |
ForEach-Object {
$_.Principal.Id = "NTAuthority\SYSTEM"
$_.Principal.RunLevel = 1
$_
} |
Add-TaskAction -Script {
"SystemTask" > C:\myTest.txt
} |
Add-TaskTrigger -OnRegistration |
Register-ScheduledTask SystemTask