VMware Cloud Community
pamiller21
Enthusiast
Enthusiast
Jump to solution

Deleting Old Reports

Alright so this isn't related to vmware or powercli, but I have all my scripts output to a temp folder then ftp them to a webserver.  I then have a script run daily to delete any temp file that is older than 60 Days, well just realized sometime ago the script has been failing.  This is the script:

Get-ChildItem "C:\util\scripts\Temp" -Recurse | where {$_.LastWriteTime -lt (Get-Date).AddDays(-60)} | Remove-Item

And I have a scheduled task run daily with the highest privileges and a local admin creds. I am now getting a 0x103 error saying its taking to long before I have the task scheduler term the attempt.

Any ideas?

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

A couple of things:

  1. Are you sure there is no interactivity in the script? Did you suppress all warnings?
  2. Perhaps try the following instead of your original code

$d = (Get-Date).AddDays(-60)

Get-ChildItem -Path C:\Temp | where{$_.Name -match ".csv$|.htm$" -and $_.LastWriteTime -lt $d} | Remove-Item -Confirm:$false


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
5 Replies
DZ1
Hot Shot
Hot Shot
Jump to solution

I don't have the answer, but have you ran the script at the PowerShell prompt, without running it through task scheduler?  Maybe the account that it runs under has an issue, or maybe some other scripts are running causing that one to hang. 

0 Kudos
pamiller21
Enthusiast
Enthusiast
Jump to solution

I moved the time that it runs but still no change.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Do you actually need the Recurse switch on the Get-ChildItem?

Are these reports organised in subfolders under C:\util\scripts\Temp?

Do these reports have similar file types (.csv, .html...)?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
pamiller21
Enthusiast
Enthusiast
Jump to solution

There are no subfolders but there are both .csv and .htm files.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

A couple of things:

  1. Are you sure there is no interactivity in the script? Did you suppress all warnings?
  2. Perhaps try the following instead of your original code

$d = (Get-Date).AddDays(-60)

Get-ChildItem -Path C:\Temp | where{$_.Name -match ".csv$|.htm$" -and $_.LastWriteTime -lt $d} | Remove-Item -Confirm:$false


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos