forked from deka/vsts-remote-retention-cleaner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RemoteRetentionCleaner.ps1
146 lines (118 loc) · 3.82 KB
/
RemoteRetentionCleaner.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
[CmdletBinding()]
param (
)
Trace-VstsEnteringInvocation $MyInvocation
[string]$folderPath = Get-VstsInput -Name "folderPath" -Require
[int]$retentionDays = Get-VstsInput -Name "retentionDays" -Require
[int]$minimumToKeep = Get-VstsInput -Name "minimumToKeep" -Require
[string]$remoteComputer = Get-VstsInput -Name "remoteComputer" -Require
[string]$userName = Get-VstsInput -Name "userName" -Require
[string]$password = Get-VstsInput -Name "password" -Require
[string]$excludes = (Get-VstsInput -Name "excludes").Trim().Replace("`n",",")
[string]$winRMProtocol = Get-VstsInput -Name "winRMProtocol"
Write-Host "Version 1.0.8"
Write-Host "remoteComputer = $remoteComputer"
Write-Host "folderPath = $folderPath"
Write-Host "retentionDays = $retentionDays"
Write-Host "minimumToKeep = $minimumToKeep"
Write-Host "userName = $userName"
Write-Host "password = $password"
Write-Host "excludes = $excludes"
Write-Host "winRMProtocol = $winRMProtocol"
$portNumber = 5985;
$sslArg = $false;
if([string]::IsNullOrWhiteSpace($folderPath) )
{
throw [System.ArgumentException] "Folder path required"
exit
}
if([string]::IsNullOrWhiteSpace($retentionDays) )
{
throw [System.ArgumentException] "Rentention days required"
exit
}
if([string]::IsNullOrWhiteSpace($minimumToKeep) )
{
throw [System.ArgumentException] "Minimum to keep is required"
exit
}
if([string]::IsNullOrWhiteSpace($remoteComputer) )
{
throw [System.ArgumentException] "Remote computer is required"
exit
}
if([string]::IsNullOrWhiteSpace($userName) )
{
throw [System.ArgumentException] "Username is required"
exit
}
if([string]::IsNullOrWhiteSpace($password) )
{
throw [System.ArgumentException] "Password is required"
exit
}
if(-not [string]::IsNullOrWhiteSpace($winRMProtocol) )
{
if ($winRMProtocol -eq "Https")
{
$portNumber = 5986;
$sslArg = $true;
}
}
$script = {
$folderPath = $args[0]
$retentionDays = $args[1]
$minimumToKeep = $args[2]
$excludes = $args[3]
$folderlist = Get-ChildItem $folderPath -Exclude $excludes.Split(",") | sort @{Expression={$_.LastWriteTime}; Ascending=$false}
Write-Host "Item list : "
Write-Host $folderlist
$keepDate = (Get-Date).AddDays(-$retentionDays)
$keeping = 0
$deleting = 0
ForEach ($folder In $folderlist) {
if($folder.LastWriteTime -lt $keepDate -And $minimumToKeep -le $keeping)
{
Write-Host "Deleting $($folder.FullName)"
Remove-Item $folder.FullName -Recurse -Force
$deleting++
}
else
{
$keeping++
}
}
Write-Host "Deleting $($deleting) files/folders."
Write-Host "Keeping $($keeping) files/folders."
}
$computers = $remoteComputer.split(',',[System.StringSplitOptions]::RemoveEmptyEntries)
if($computers.Count -eq 0)
{ Write-Error "No remote computer"}
$errors =@()
foreach($computer in $computers)
{
Try
{
Write-Host "Start process. Computer name : $($computer)."
Write-Host " - PSCredential"
$credential = New-Object System.Management.Automation.PSCredential($userName, (ConvertTo-SecureString -String $password -AsPlainText -Force));
Write-Host " - Initialize New-PSSession"
$session = New-PSSession -ComputerName $computer -Credential $credential -Port $portNumber -UseSSL:$sslArg;
Write-Host " - Invoke-Command"
Invoke-Command -Session $session -ScriptBlock $script -ArgumentList $folderPath, $retentionDays ,$minimumToKeep, $excludes
Write-Host " - Remove-PSSession"
Remove-PSSession -Session $session
}
Catch
{
$errors += @($computer + " " + $_.Exception.Message)
}
}
if($errors.Count -ne 0)
{
foreach( $er in $errors)
{
Write-Warning $er
}
throw "$($errors.Count) errors in process. See logs"
}