-
Notifications
You must be signed in to change notification settings - Fork 0
/
Listing 2.5.ps1
83 lines (74 loc) · 2.94 KB
/
Listing 2.5.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
function Set-Recyclelogs
{
[CmdletBinding(
SupportsShouldProcess = $true,
ConfirmImpact = 'High')]
param
(
[Parameter(Mandatory = $true,ParameterSetName = 'Local')]
[string]$foldername,
[Parameter(Mandatory = $true,ParameterSetName = 'Local')]
[Parameter(Mandatory = $true,ParameterSetName = 'Path')]
[Parameter(Mandatory = $true,ParameterSetName = 'Remote')]
[int]$limit,
[Parameter(ParameterSetName = 'Local',Position = 0)][switch]$local,
[Parameter(Mandatory = $true,ParameterSetName = 'Remote')]
[string]$ComputerName,
[Parameter(Mandatory = $true,ParameterSetName = 'Remote')]
[string]$DriveName,
[Parameter(Mandatory = $true,ParameterSetName = 'Remote')]
[string]$folderpath,
[Parameter(ParameterSetName = 'Remote',Position = 0)][switch]$Remote,
[Parameter(Mandatory = $true,ParameterSetName = 'Path')]
[ValidateScript({
if(-Not ($_ | Test-Path) ){throw "File or folder does not exist"}
return $true
})]
[string]$folderlocation,
[Parameter(ParameterSetName = 'Path',Position = 0)][switch]$Path
)
switch ($PsCmdlet.ParameterSetName) {
"Local"
{
$path1 = (Get-Location).path + "\" + "$foldername"
if ($PsCmdlet.ShouldProcess($path1 , "Delete"))
{
Write-Host "Path Recycle - $path1 Limit - $limit" -ForegroundColor Green
$limit1 = (Get-Date).AddDays(-"$limit") #for report recycling
$getitems = Get-ChildItem -Path $path1 -recurse -file | Where-Object {$_.CreationTime -lt $limit1}
ForEach($item in $getitems){
Write-Verbose -Message "Deleting item $($item.FullName)"
Remove-Item $item.FullName -Force
}
}
}
"Remote"
{
$path1 = "\\" + $ComputerName + "\" + $DriveName + "$" + "\" + $folderpath
if ($PsCmdlet.ShouldProcess($path1 , "Delete"))
{
Write-Host "Recycle Path - $path1 Limit - $limit" -ForegroundColor Green
$limit1 = (Get-Date).AddDays(-"$limit") #for report recycling
$getitems = Get-ChildItem -Path $path1 -recurse -file | Where-Object {$_.CreationTime -lt $limit1}
ForEach($item in $getitems){
Write-Verbose -Message "Deleting item $($item.FullName)"
Remove-Item $item.FullName -Force
}
}
}
"Path"
{
$path1 = $folderlocation
if ($PsCmdlet.ShouldProcess($path1 , "Delete"))
{
Write-Host "Path Recycle - $path1 Limit - $limit" -ForegroundColor Green
$limit1 = (Get-Date).AddDays(-"$limit") #for report recycling
$getitems = Get-ChildItem -Path $path1 -recurse -file | Where-Object {$_.CreationTime -lt $limit1}
ForEach($item in $getitems){
Write-Verbose -Message "Deleting item $($item.FullName)"
Remove-Item $item.FullName -Force
}
}
}
}
}# Set-Recycle logs