I've been looking over this script for several days. Worked through a majority of the errors but can't seem to catch what is causing this error. The script is:
. $Env:JobDir\Scripts\DeleteMethods1.ps1
#Purge old logs
Remove-FilesCreatedBeforeDate -Path "$Env:JobDir\Logs\" -DateTime ((Get- Date).AddDays(-30))
#Backup log from last run
if(Test-Path $Env:JobDir\log_*.txt)
{
Move-Item -force $Env:JobDir\log_*.txt $Env:JobDir\Logs\
}
$logFile = $Env:JobDir + "\log_$(Get-Date -Format ""yyyyMMdd-HHmm"").txt"
echo "Removing files older than $Env:Days days" >> $logFile
if($Env:CheckType -eq "Created")
{
# Remove all files created more than 30 days ago
# Optionally use -DeletePathIfEmpty to remove empty directories
Remove-FilesCreatedBeforeDate -Path $Env:PathToCleanUp -DateTime ((Get- Date).AddDays($Env:Days)) -DeletePathIfEmpty "$Env:DeletePathIfEmpty" >> $logFile
}
if($Env:CheckType -eq "Modified")
{
# Delete all files that have not been updated in 30 days
Remove-FilesNotModifiedAfterDate -Path $Env:PathToCleanUp -DateTime ((Get- Date).AddDays($Env:Days)) "$Env:DeletePathIfEmpty" >> $logFile
}
if($Env:CheckType -eq "Accessed")
{
# Delete all files that have not been accessed in 30 days
Remove-FilesNotAccessedAfterDate -Path $Env:PathToCleanUp -DateTime ((Get-Date).AddDays($Env:Days)) "$Env:DeletePathIfEmpty" >> $logFile
}
echo "Done removing files older than $Env:Days days" >> $logFile
cat "$Env:JobDir\log.txt" >> $logFile
The script pulls functions from the following file:
function Remove-EmptyDirectories([parameter(Mandatory=$true)][ValidateScript({Test-Path $_})][string] $Path, [switch] $DeletePathIfEmpty,
[DateTime] $OnlyDeleteDirectoriesCreatedBeforeDate = [DateTime]::MaxValue,
[DateTime] $OnlyDeleteDirectoriesNotModifiedAfterDate = [DateTime]::MaxValue,
[DateTime] $OnlyDeleteDirectoriesNotAccessedAfterDate = [DateTime]::MaxValue)
{
Get-ChildItem -Path $Path -Recurse -Force -Directory | Where-Object { (Get- ChildItem -Path $_.FullName -Recurse -Force -File) -eq $null } |
Where-Object { $_.CreationTime -lt $OnlyDeleteDirectoriesCreatedBeforeDate -and $_.LastWriteTime -lt $OnlyDeleteDirectoriesNotModifiedAfterDate
-and $_.LastAccessedTime -lt $OnlyDeleteDirectoriesNotAccessedAfterDate } |
Remove-Item -Force -Recurse
# If we should delete the given path when it is empty, and it is a directory, and it is empty, and it meets the date requirements, then delete it.
if ($DeletePathIfEmpty -and (Test-Path -Path $Path -PathType Container) -and (Get-ChildItem -Path $Path -Force) -eq $null -and
((Get-Item $Path).CreationTime -lt $OnlyDeleteDirectoriesCreatedBeforeDate) -and
((Get-Item $Path).LastWriteTime -lt $OnlyDeleteDirectoriesNotModifiedAfterDate) -and
((Get-Item $Path).LastWriteTime -lt $OnlyDeleteDirectoriesNotAccessedAfterDate))
{ Remove-Item -Path $Path -Force }
}
# Function to remove all files in the given Path that were created before the given date, as well as any empty directories that may be left behind.
function Remove-FilesCreatedBeforeDate([parameter(Mandatory)][ValidateScript({Test-Path $_})][string] $Path,[parameter(Mandatory)][DateTime] $DateTime,[switch] $DeletePathIfEmpty)
{
Get-ChildItem -Path $Path -Recurse -Force -File | Where-Object { $_.CreationTime -lt $DateTime } | Remove-Item -Force
Remove-EmptyDirectories -Path $Path -DeletePathIfEmpty $DeletePathIfEmpty -OnlyDeleteDirectoriesCreatedBeforeDate $DateTime -OnlyDeleteDirectoriesNotModifiedAfterDate $DateTime -OnlyDeleteDirectoriesNotAccessedAfterDate $DateTime
}
# Function to remove all files in the given Path that have not been modified after the given date, as well as any empty directories that may be left behind.
function Remove-FilesNotModifiedAfterDate([parameter(Mandatory)][ValidateScript({Test-Path $_})][string] $Path, [parameter(Mandatory)][DateTime] $OnlyDeleteDirectoriesNotModifiedAfterDate, [switch] $DeletePathIfEmpty)
{
Get-ChildItem -Path $Path -Recurse -Force -File | Where-Object { $_.LastWriteTime -lt $OnlyDeleteDirectoriesNotModifiedAfterDate } | Remove-Item -Force
Remove-EmptyDirectories -Path $Path -OnlyDeleteDirectoriesNotModifiedAfterDate $OnlyDeleteDirectoriesNotModifiedAfterDate -DeletePathIfEmpty $DeletePathIfEmpty
}
# Function to remove all files in the given Path that were created before the given date, as well as any empty directories that may be left behind.
function Remove-FilesNotAccessedAfterDate([parameter(Mandatory)][ValidateScript({Test-Path $_})][string] $Path, [parameter(Mandatory)][DateTime] $OnlyDeleteDirectoriesNotAccessedAfterDate, [switch] $DeletePathIfEmpty)
{
Get-ChildItem -Path $Path -Recurse -Force -File | Where-Object { $_.LastAccessTime -lt $OnlyDeleteDirectoriesNotAccessedAfterDate } | Remove-Item -Force
Remove-EmptyDirectories -Path $Path -OnlyDeleteDirectoriesNotAccessedAfterDate $OnlyDeleteDirectoriesNotAccessedAfterDate -DeletePathIfEmpty $DeletePathIfEmpty
}
Now the object of this script is to crawl through directories and delete old items. A lot of the variables in here are from environmentally set items. Now when I run this I get an error that states:
Remove-FilesCreatedBeforeDate : A positional parameter cannot be found that
accepts argument ''.
+ Remove-FilesCreatedBeforeDate -Path $Env:PathToCleanUp -DateTime
((Get-Date) ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~
+ CategoryInfo : InvalidArgument: (:) [Remove-FilesCreatedBeforeD
ate], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Remove-FilesCreatedB
eforeDate
I figure that I've been looking at this too long and I can't see the error so if it's something simple be kind. Anything as far as advice would be fantastic. Thanks in advance!!!
*Note I am not the one who wrote the script - a co-worker has me on error correction duty.