简体   繁体   中英

Scheduled Powershell Script wont run

I have a ps1 script like below, it filters files and outputs a formatted HTML file.

$a = "<style>"
$a = $a + "BODY{background-color:peachpuff;}"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$a = $a + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:thistle}"
$a = $a + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:PaleGoldenrod}"
$a = $a + "</style>"

$b = Get-Date -Format u    

Get-ChildItem -Recurse K:\AppData\*.* -Filter *.CATPart | Where{$_.LastWriteTime -gt (Get-Date).AddDays(-6)} | sort LastWriteTime -descending | select name,LastWriteTime,Directory | convertto-html -head $a -body "<H2>CATIA PAST 7 DAYS -- $b </H2>" | out-file C:\aaa\catia_result.htm

I can run this script manually with no problem at all. but when I schedule it to run, it only gives me the formatted htm file without any filtered data in there. This is arguments I used in task scheduler:

powershell.exe -ExecutionPolicy Bypass -Command "C:\aaa\RLTP_HTML_FINAL.ps1"

I tried change executionpolicy to Unrestricted , it still wont work. The task history shows the task completed, but there is no data in the HTML file.

I also tried to use a batch file call up powershell to run the script, it is the same result that it only works with manual operation but task scheduler.

Most likely, when you schedule the script to execute, it may not have a mapping to the K:\\ drive. Make sure that:

  1. The K drive is mapped in the script, using the Get-PSDrive cmdlet
  2. Your credentials that the scheduled task is set to use have access to the K:\\ drive

Alternatively , you could simply specify a UNC path, instead of referencing the K:\\ drive.

ps. Good job using -ExecutionPolicy Bypass . That helps to avoid any issues with the execution policy! :) It doesn't matter what the execution policy is set to, as long as you use that parameter.

If you want to capture any errors in your script, make this your last line:

Add-Content -Path $PSScriptRoot\error.log -Value $error;

You might see something about the K:\\ drive missing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM