Powershell脚本未在计划任务上运行

时间:2017-06-07 03:48:05

标签: powershell scheduled-tasks

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

enter image description here

enter image description here

enter image description here

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

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

enter image description here

enter image description here

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
}

2 个答案:

答案 0 :(得分:0)

Ensure you have your execution to unrestricted.

Set-ExecutionPolicy Unrestricted 

And add the following action into the Program\Script section and click Yes on the prompt that appears.

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

If you can not set your execution policy to unrestricted just add -NoProfile -ExecutionPolicy Bypass to the above command.

答案 1 :(得分:0)

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

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

设置此参数:

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