I have a direct delete function (deletes the files direct) implemented in my script, and a trashbin delete function (moving it to the trashbin first).
The problem is that the trashbin delete doesn't work. I've tried this suggested post already but it doesn't seem to work.
My full script :
## Top of the script
param(
[Parameter(Mandatory=$true)]
[ValidateRange(0,99999)]
[int]$minutes,
[Parameter(Mandatory=$true)]
[ValidateScript({Test-Path $_})]
[string]$maplocation,
[Parameter(Mandatory=$true)]
[ValidateSet("Direct", "TrashBin")]
[string]$consequence
)
## error notifications
## Variables
$file = Get-ChildItem -Path $maplocation | Get-Date
$time = Get-Date
$minutesconvert = (New-Timespan -Start $file -End $time).TotalMinutes
foreach ($file in $files)
{
if ($minutes -lt $minutesconvert -and $consequence -eq "direct")
{
Write-Verbose "File Found $file" -Verbose
Write-Verbose "Deleting $file" -Verbose
Remove-Item $file.FullName
}
elseif ($minutes -lt $minutesconvert -and $consequence -eq "trashbin")
{
Add-Type -AssemblyName Microsoft.VisualBasic
Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile($maplocation, 'OnlyErrorDialogs', 'SendToRecycleBin')
}
else
{
Write-Verbose -message "txt" -verbose
}
}
Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile($maplocation, 'OnlyErrorDialogs', 'SendToRecycleBin')
Error code in PowerShell console:
New-TimeSpan : Cannot convert 'System.Object[]' to the type 'System.DateTime' required by parameter 'Start'. The method is not supported. At C:\Users\david\Desktop\nieuw.ps1:21 char:39 + $minutesconvert = (New-TimeSpan -Start <<<< $file -End $time).TotalMinutes + CategoryInfo : InvalidArgument: (:) [New-TimeSpan], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.NewTimeSpanCommand