Создание вашей задачи в порядке.Поскольку вы не вызываете powershell.exe для вызова .ps1, он технически вызывает только .ps1, как файл блокнота.
$TaskName = "Backup DB"
$TaskDescr = "Automated Backup DB"
$TaskCommand = "c:\windows\system32\WindowsPowerShell\v1.0\powershell.exe"
$TaskScript = '"C:\Users\mine\Desktop\LOL\test.ps1"+'"'
$TaskArg = "-Executionpolicy unrestricted -file $TaskScript"
$service = new-object -ComObject("Schedule.Service")
# connect to the local machine.
$service.Connect()
$rootFolder = $service.GetFolder("\")
$TaskDefinition = $service.NewTask(0)
$TaskDefinition.RegistrationInfo.Description = "$TaskDescr"
$TaskDefinition.Settings.Enabled = $true
$TaskDefinition.Settings.AllowDemandStart = $true
$TaskDefinition.Settings.StartWhenAvailable = $true
$TaskDefinition.Settings.StopIfGoingOnBatteries=$false
$TaskDefinition.Settings.DisallowStartIfOnBatteries=$false
$TaskDefinition.Settings.MultipleInstances=2
$taskdefinition.Settings.WakeToRun=$true
$triggers = $TaskDefinition.Triggers
$trigger = $triggers.Create(1) # Creates a "One time" trigger
$trigger.StartBoundary = $TaskStartTime.ToString("yyyy-MM-dd'T'HH:mm:ss")
$time_interval=New-TimeSpan -Minutes $interval
$time_interval=$time_interval.TotalSeconds
$trigger.Repetition.Interval= "PT"+"$time_interval"+"S"
$trigger.Enabled = $true
$TaskDefinition.Principal.RunLevel =1
$Action = $TaskDefinition.Actions.Create(0)
$action.Path = "$TaskCommand"
$action.Arguments = "$TaskArg"
# In Task Definition,
# 6 indicates "the task will not execute when it is registered unless a time-based trigger causes it to execute on registration."
# 5 indicates "Indicates that a Local System, Local Service, or Network Service account is being used as a security context to run the task.In this case, its the SYSTEM"
$rootFolder.RegisterTaskDefinition("$TaskName",$TaskDefinition,6,"System",$null,5) | Out-Null
Я добавил соответствующие комментарии, чтобы создать задачу и триггер соответственно.
Альтернативный метод заключается в прямом использовании Powershell следующим образом:
Import-Module TaskScheduler $task = New-Task
$task.Settings.Hidden = $true
Add-TaskAction -Task $task -Path C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe –Arguments “-File C:\Users\mine\Desktop\LOL\test.ps1”
Add-TaskTrigger -Task $task -Daily -At “10:06”
Register-ScheduledJob –Name ”Monitor Group Management” -Task $task
Прямой подход:
C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -NoLogo -NonInteractive -File "C:\Users\mine\Desktop\LOL\test.ps1"
Надеждаэто помогает.