0

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.

1 Answers1

0

It has to be a problem with "$Env:DeletePathIfEmpty", since that's the only positional parameter you use in those statements. Based on the other places you call that function I'm guessing you need to change this:

"$Env:DeletePathIfEmpty"

to this:

-DeletePathIfEmpty "$Env:DeletePathIfEmpty"

It looks like that change needs to be made for both the "Modified" and "Accessed" checktype.

Based on your comment pointing out that the -DeletePathIfEmpty parameter is a [switch] it looks like you either need to just call out the -DeletePathIfEmpty switch or modify the function to a [bool] type and pass it either a $true or $false.

Tim Ferrill
  • 1,648
  • 1
  • 12
  • 15
  • The functions script doesn't declare a -DeletePathIfEmpty parameter so when I add those conditions to the script it gives me an error that states "A parameter cannot be found that matches parameter name 'DeletePathIfEmpty'. Could it be the [switch] on the functions document? – user2280319 Jun 23 '14 at 13:47
  • Ah, yes that could be an issue as well. A switch parameter is `$true` if it's passed and `$false` if it's not, so you don't typically set a value for it. – Tim Ferrill Jun 23 '14 at 14:06
  • So I removed the `'DeletePathIfEmpty'` variable but left the `-DeletePathIfEmpty`. Well it deleted things but I'm not entirely sure it deleted the right things. Back to testing! Thanks for your help! – user2280319 Jun 23 '14 at 15:12