简体   繁体   English

PowerShell PowerShell 创建的任务中的脚本未运行

[英]PowerShell script within PowerShell-created task doesn't run

I'm working on a test script to get the idea of working with the Task Scheduler in PowerShell.我正在编写一个测试脚本,以了解在 PowerShell 中使用任务计划程序的想法。

I don't want to have any credentials within the script for security matters.我不想在脚本中有任何安全问题的凭据。

My idea was to create a task in PowerShell which runs a script but for some reason it won't execute properly and I don't get why.我的想法是在 PowerShell 中创建一个运行脚本的任务,但由于某种原因它无法正确执行,我不明白为什么。

My task is created as following:我的任务创建如下:

$taskName = "WeeklyMaintance"
$User = "NT AUTHORITY\SYSTEM"
$Trigger = New-ScheduledTaskTrigger –Daily -At "08:14"
$Action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "D:\Some SW\_Scripts\testing.ps1"
Register-ScheduledTask -TaskName $taskName -Trigger $Trigger -User $User -Action $Action -RunLevel Highest

and the script looks like this脚本看起来像这样

$Logpath = "D:\Some SW\_Scripts"
$logname = "Log.txt"
function Write-Log {
    Param ([string]$logstring)
    $timestamp = Get-Date -Format "dd.MM.yyyy HH:mm:ss"
    $log = "[$timestamp]: $logstring"
    Add-Content -Value $log -Path "$Logpath\$logname"
}
Write-Log "My Test for research worked"

The Logfile is already created and this works perfectly fine when executed normaly but for some reasen as I create the task and run it with the task it won't start.日志文件已经创建,正常执行时它工作得很好,但在我创建任务并使用它不会启动的任务运行它时有些放松。

Did I something wrong with the creation of the task or using the system here?我在这里创建任务或使用系统有问题吗?

Edit:编辑:

I found out it was only the path which must have been the "bad guy".我发现那一定是“坏人”的唯一路径。

As I changed the path from "D:\Some SW_Scripts" to "D:\Test"当我将路径从“D:\Some SW_Scripts”更改为“D:\Test”时

I have had issues with this in the past and I've needed to add the '-File' parameter to the action. 过去我对此有疑问,需要在操作中添加“ -File”参数。

$taskName = "WeeklyMaintance"
$User= "NT AUTHORITY\SYSTEM"
$Trigger= New-ScheduledTaskTrigger –Daily -At "08:14"
$Action= New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-File `"D:\Some SW\_Scripts\testing.ps1`""
Register-ScheduledTask -TaskName $taskName -Trigger $Trigger -User $User -Action $Action -RunLevel Highest

I had the same problem.我有同样的问题。

  • I had an executer.ps1 file which was trying to execute backup.ps1 file.我有一个executer.ps1文件试图执行backup.ps1文件。
  • When I tried to run the backup.ps1 file directly, it was perfectly fine;当我尝试直接运行backup.ps1文件时,一切正常; however, when I tried to execute it within the executer.ps1 , the New-ScheduledTaskAction was unable to execute it.但是,当我尝试在executer.ps1中执行它时, New-ScheduledTaskAction无法执行它。
  • As @Ash mentioned, I added -File to the line and explicitly wrote the path between the quotes but didn't work for me.正如-File提到的,我在该行中添加了 -File 并明确写下了引号之间的路径,但对我不起作用。
  • Finally, I modified the line as: New-ScheduledTaskAction -Execute $powerShellExe -Argument "-executionpolicy bypass -noprofile -file $scriptDirectory" and it worked for me.最后,我将该行修改为: New-ScheduledTaskAction -Execute $powerShellExe -Argument "-executionpolicy bypass -noprofile -file $scriptDirectory"它对我有用。
[string]$scriptDirectory = "C:\Users\<username>\Desktop\PS\backup.ps1"
[string]$powerShellExe = "PowerShell.exe"
[string]$taskName = "BackupTask"
[string]$description = "Test backup task"
[string]$userId = "NT AUTHORITY\SYSTEM"

$trigger = New-ScheduledTaskTrigger -Daily -DaysInterval 3 -At 12:58:05pm
$principal = New-ScheduledTaskPrincipal -UserID $userId -LogonType S4U
$settings = New-ScheduledTaskSettingsSet -MultipleInstances Parallel -DontStopIfGoingOnBatteries
$action = New-ScheduledTaskAction -Execute $powerShellExe -Argument "-executionpolicy bypass -noprofile -file $scriptDirectory"  # Specify what program to run and with its parameters

Register-ScheduledTask -TaskName $taskName -Trigger $trigger -Action $action -Description $description -Settings $settings -Principal $principal

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PowerShell 计划任务:打开 PowerShell 和要运行的脚本但不执行脚本 - PowerShell scheduled task: opens PowerShell and script to be run but doesn't execute script 在Powershell中创建的计划任务未出现在Task Scheduler中 - Scheduled task created in Powershell doesn't appear in Task Scheduler 从Powershell脚本中运行PowerShell脚本 - Run powershell script from within a powershell script 使用任务计划程序计划的Powershell脚本无法运行…脚本运行正常 - Powershell script scheduled using Task scheduler doesn't run…script runs fine Powershell作为脚本运行不起作用? - Powershell Run as Script Doesn't work? Powershell 脚本在第一次执行时不运行 - Powershell script doesn't run on first execution PowerShell添加任务以使用参数运行PowerShell脚本 - PowerShell add Task to run PowerShell script with parameters 无法在 powershell 中运行 powershell 脚本 - Unable to run powershell script within powershell powershell脚本可在run.exe中运行,但不能作为计划任务运行 - powershell script works in run.exe but doesn't work as scheduled task 从任务计划程序运行时,PowerShell 脚本将文件上传到 SharePoint 不起作用 - PowerShell Script uploading file to SharePoint doesn't work when run from Task Scheduler
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM