【问题标题】:Powershell script not running on scheduled taskPowershell脚本未在计划任务上运行
【发布时间】:2017-11-08 05:54:02
【问题描述】:

我可以在任务计划程序内外运行脚本。但是计划任务在触发时不会自动运行脚本。我尝试通过更改来配置本地安全策略,用户帐户控制:Built-i 管理员帐户的管理员批准模式(未定义) - 用户帐户控制:在管理员批准模式下运行所有​​管理员(已禁用) - 用户帐户控制:行为管理员批准模式下的管理员提升提示(不提示提升)。这没有用。任何帮助都会很棒。谢谢。

程序/脚本:C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

添加参数:-NoProfile -ExecutionPolicy Bypass -File C:\Users\Andersen\Desktop\moveFiles.ps1

Try
{

$Time = Get-Date

if(Test-Path C:\Users\Andersen\Desktop\src) {
    "Source exists."
} else {
    Write-Output "This script made a read attempt at [$Time]. Source (C:\Users\Andersen\Desktop\src) cannot be found." | out-file C:\Users\Andersen\Desktop\KofaxScriptErrorLog.txt -Append
    Exit
}

$IDsrc = (Get-ChildItem -Path "C:\Users\Andersen\Desktop\src\ID - Sub Invoice").Count
$Jsrc  = (Get-ChildItem -Path "C:\Users\Andersen\Desktop\src\J - Sub Invoice").Count
$ORsrc = (Get-ChildItem -Path "C:\Users\Andersen\Desktop\src\OR - Sub Invoice").Count
$OR2   = (Get-ChildItem -Path "C:\Users\Andersen\Desktop\src\OR2 - Sub Invoice").Count
$SCsrc = (Get-ChildItem -Path "C:\Users\Andersen\Desktop\src\SC Invoice").Count
$WAsrc = (Get-ChildItem -Path "C:\Users\Andersen\Desktop\src\WA - Sub Invoice").Count

# move Kofax ID files to ID folder in Egnyte
If ($IDsrc -ne 0){
Get-ChildItem  -Path "C:\Users\Andersen\Desktop\src\ID - Sub Invoice" -Recurse -ErrorAction Stop |
    Move-Item -Destination "C:\Users\Andersen\Desktop\Current Imaging\ID - Sub Invoice" -Force -ErrorAction Stop
    }
# move Kofax J files to J folder in Egnyte 
If ($Jsrc -ne 0){
Get-ChildItem -Path "C:\Users\Andersen\Desktop\src\J - Sub Invoice" -Recurse -ErrorAction Stop | 
    Move-Item -Destination "C:\Users\Andersen\Desktop\Current Imaging\J - Sub Invoice" -Force -ErrorAction Stop
    }
# move Kofax OR files to OR folder in Egnyte
If ($ORsrc -ne 0){
Get-ChildItem -Path "C:\Users\Andersen\Desktop\src\OR - Sub Invoice" -Recurse -ErrorAction Stop | 
    Move-Item -Destination "C:\Users\Andersen\Desktop\Current Imaging\OR - Sub Invoice" -Force -ErrorAction Stop 
    }
# move Kofax OR2 files to OR2 folder in Egnyte
If ($OR2src -ne 0){
Get-ChildItem -Path "C:\Users\Andersen\Desktop\src\OR2 - Sub Invoice" -Recurse -ErrorAction Stop | 
    Move-Item -Destination "C:\Users\Andersen\Desktop\Current Imaging\OR2 - Sub Invoice" -Force -ErrorAction Stop 
    }
# move Kofax SC(multi) files to SC(multi) folder in Egnyte
If ($SCsrc -ne 0){
Get-ChildItem -Path "C:\Users\Andersen\Desktop\src\SC Invoice" -Recurse -ErrorAction Stop | 
    Move-Item -Destination "C:\Users\Andersen\Desktop\Current Imaging\SC Invoice" -Force -ErrorAction Stop 
                    }
# move Kofax WA files to WA folder in Egnyte
If ($WAsrc -ne 0){
Get-ChildItem -Path "C:\Users\Andersen\Desktop\src\WA - Sub Invoice" -Recurse -ErrorAction Stop | 
    Move-Item -Destination "C:\Users\Andersen\Desktop\Current Imaging\WA - Sub Invoice" -Force -ErrorAction Stop 
    }       

}   


Catch {

$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
    "The script to move files from the Kofax server to Egynte made a run attempt at $Time. Error Details: $ErrorMessage $FailedItem" | out-file C:\Users\Andersen\Desktop\KofaxScriptErrorLog.txt -Append
    Break
}

Finally
{

    "This script made a complete read attempt at $Time" | out-file C:\Users\Andersen\Desktop\KofaxScriptLog.txt -Append
}

【问题讨论】:

  • 批量登录的成员?
  • 你试过-ExecutionPolicy Unrestricted吗?此外,您可以尝试在“程序/脚本:”中使用批处理来启动您的 Powershell 脚本,而不是直接设置您的 ps1 文件以查看行为是否相同?
  • @notjustme 是的,我将自己添加到“作为批处理作业登录”
  • 任务调度器是否说明了上次运行结果?

标签: powershell scheduled-tasks


【解决方案1】:

在您的计划任务中调用 Powershell.exe:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

设置此参数:

-noprofile -executionpolicy unrestricted -noninteractive -file "C:\Path\To\MyScript.ps1"

【讨论】:

  • 对问题中显示的参数的唯一更改是从-ExecutionPolicy Bypass 切换到-ExecutionPolicy Unrestricted,我不希望这会有所作为 - 除了Unrestricted 你仍然可以如果您尝试执行从网络下载的脚本,则会收到警告提示,因此Bypass 是无人值守执行的更好选择,前提是您愿意承担风险。 @DigitalSea,您能解释一下为什么这个答案解决了您的问题,以及为什么您的问题中的论点不起作用?
  • 对我不起作用:(服务器在域中,我是它的 RDP,可以运行独立于脚本但任务调度程序不能,也没有错误:(
【解决方案2】:

确保您的执行不受限制。

Set-ExecutionPolicy Unrestricted 

并将以下操作添加到Program\Script 部分并在出现的提示上单击Yes

PowerShell.exe "& 'C:\LocationFolder\YourScript.ps1'" 

如果您无法将执行策略设置为不受限制,只需将-NoProfile -ExecutionPolicy Bypass 添加到上述命令。

【讨论】:

    猜你喜欢
    • 2015-07-27
    • 2015-07-22
    • 2019-05-02
    • 1970-01-01
    • 2017-08-01
    • 2013-08-18
    • 2020-08-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多