Powershell script not running on scheduled task

柔情痞子 提交于 2020-03-16 07:39:11

问题


I can run the script outside and inside the Task Scheduler. But the scheduled tasks will not run the script automatically when triggered. I have tried configuring the Local Security Policies by changing, User Account Control: Admin Approval Mode for the Built-i Administrator account (Not Defined) - User Account Control: Run all administrators in Admin Approval Mode (Disabled) - User Account Control: Behavior of the elevation prompt for administrators in Admin Approval Mode (Elevate without prompting). This did not work. Any help would be great. Thanks.

Program/script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Add arguments: -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
}

回答1:


Call the Powershell.exe in your schedulded task:

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

Set this arguments:

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



回答2:


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.



来源:https://stackoverflow.com/questions/44403144/powershell-script-not-running-on-scheduled-task

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!