1
我遇到了一個問題,我的腳本,每當我做一個垃圾桶刪除和地圖位置有一個文件夾中的文件夾中有一個文件必須刪除,它不會工作。添加遞歸+靜默地繼續回收垃圾桶刪除
的問題是,我需要添加一個遞歸和-ErrorAction默默地繼續。但我不知道我該怎麼做,因爲將文件移動到垃圾箱的方法在我看來很難。
你們能幫我嗎?
我的代碼:
## 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
)
## Variables
$maxAge = (Get-Date).AddMinutes(-$minutes)
$files = Get-ChildItem $maplocation -Recurse
$time = get-date
$fortrashbin = $maplocation + '\' + $file
##
foreach ($file in $files)
{
if ($file.lastwritetime -lt $maxage)
{
switch ($consequence)
{
"direct"
{
write-verbose "File Found $file" -verbose
remove-item $file.fullname -recurse -erroraction silentlycontinue
write-verbose "Deleting $file" -verbose
}
"trashbin" {
write-verbose "File Found $file" -verbose
write-verbose "moving $file to trashbin" -verbose
Add-Type -AssemblyName Microsoft.VisualBasic
[Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile($fortrashbin,'OnlyErrorDialogs','SendToRecycleBin')
}
}
}
}