-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_Delete.ps1
More file actions
82 lines (74 loc) · 2.78 KB
/
_Delete.ps1
File metadata and controls
82 lines (74 loc) · 2.78 KB
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
param([string]$operationTypeToDelete="")
$unhandledItems = @()
$operationType = "Delete"
$operationTypes = @("SendZip", "SendCopy", "ReceiveCopy", "ReceiveUnzip")
if($operationTypes.Contains($operationTypeToDelete)){
$errorsPath = ($PSScriptRoot + "\" + "log_" + $operationType + "_" + $operationTypeToDelete + ".txt")
$progressFilePath = ($PSScriptRoot + "\" + "InProgress_" + $operationType + "_" + $operationTypeToDelete + ".txt")
$deletionReadyPath = ($PSScriptRoot + "\" + "deletion_ready_" + $operationTypeToDelete + ".txt")
$progressStartText = "## Begin ##"
$progressEndText = "## End ##"
### Get last file to not be fully processed ###
$beginFile = ""
$beginIndex = "1"
if(Test-Path $progressFilePath){
$progressFileContent = Get-Content -Path $progressFilePath
$fileInProgressSwitch = $false
foreach($line in $progressFileContent){
if($line -eq $progressStartText){
$fileInProgressSwitch = $true
} elseif($line -eq $progressEndText){
$fileInProgressSwitch = $false
$beginFile = ""
} elseif($fileInProgressSwitch -eq $true -and !($beginFile)){
$beginFile = $line
}
}
}
$items = Get-Content -Path $deletionReadyPath
$i = [int]$beginIndex
foreach ($item in $items){
### Skip file if already processed ###
if($beginFile){
if($beginFile -ne $item.Name){
Write-Output ("Skipping file: " + $item.Name)
continue
}
}
$beginFile = ""
if(Test-Path $item){
try
{
Write-Output ("Deleting file: " + $item)
Add-Content -Path $progressFilePath -Value $progressStartText
Add-Content -Path $progressFilePath -Value $item
Add-Content -Path $progressFilePath -Value $i
Remove-Item -Path $item -Verbose -Confirm:$false -Recurse
Add-Content -Path $progressFilePath -Value $progressEndText
}
catch
{
Write-Output ((Get-Date -Format "yyyy-MM-dd HH:mm:ss") + " -> Was not able to delete file: " + $item)
$unhandledItems += ((Get-Date -Format "yyyy-MM-dd HH:mm:ss") + " -> Was not able to delete file: " + $item)
}
$i = $i + 1
}
}
Remove-Item -Path $progressFilePath
Remove-Item -Path $deletionReadyPath
Write-Output "Script finished!"
if($unhandledItems.Count -gt 0){
Write-Output "- - - - - E R R O R S U M M A R Y - - - - -"
foreach ($unhandledItem in $unhandledItems){ Write-Output ($unhandledItem) }
Write-Output "- - - - - E R R O R S U M M A R Y - - - - -"
Add-Content -Path $errorsPath -Value "- - - - - E R R O R S U M M A R Y - - - - -"
foreach ($unhandledItem in $unhandledItems){ Add-Content -Path $errorsPath -Value ($unhandledItem) }
Add-Content -Path $errorsPath -Value "- - - - - E R R O R S U M M A R Y - - - - -"
Write-Output ("Error logs saved to file: " + $errorsPath)
} else {
Write-Output "No errors when running script!"
}
} else {
Write-Output "Bad operationTypeToDelete parameter value passed to PowerShell Script"
}
[System.GC]::Collect()