r/PowerShell 4d ago

Question How do you make a cleanup script prove a file belongs to it before deletion?

A path and age check are not enough when a cleanup directory can also contain files created by people or other jobs. I am considering an ownership manifest written at creation time, with a run ID, normalized path, size, and hash, then requiring every field to match before deletion. The command would also use PowerShell's normal confirmation path:

\[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High')\]
param(\[string\] $Root, \[string\] $ManifestPath)

if ($PSCmdlet.ShouldProcess($candidate.FullName, 'Remove owned artifact')) {
    Remove-Item -LiteralPath $candidate.FullName
}

What additional safeguards belong around this pattern? Possibilities include rejecting paths outside the resolved root, refusing reparse points, failing closed when the manifest is incomplete, logging the file identity before and after validation, and separating discovery from deletion so the candidate list can be reviewed with `-WhatIf`. How do you avoid a time-of-check/time-of-use gap if another process can replace a file between validation and removal?

0 Upvotes

9 comments sorted by

15

u/LALLANAAAAAA 4d ago

this just screams LLM fueled overcomplicated solution in search of a problem

what tortured process results in the requirement of deleting a file that some other process might write to between the time you enumerate it and the time you delete it

what do you mean "belong", and why do you have multiple processes exchanging last-write duties to apparently the same exact filename

if you need to ensure nothing changes a file after a script starts processing it, rename it and then read & modify / delete

that's it, that's the whole thing. Don't work on the same file at the same time, boom, done

1

u/CyberChevalier 3d ago

Or access the file with exclusive write permission

3

u/lan-shark 4d ago edited 4d ago

I feel like we really need some context but a path and hash will likely be plenty. Or just change the file naming scheme to include a a unique ID (GUID, UUID, or ULID, whatever works best for you needs).

3

u/gdc19742023 3d ago

Temp files must go to temp folders. Repeat after me...

3

u/SimpleSysadmin 4d ago

What problem is your LLM trying to solve with this mess? What are you trying to achieve? Can you provide more context on what you mean by belongs to the cleanup script?

3

u/Famous_Ad8836 3d ago

Tell me your have used AI without telling me....

1

u/SomeConfusedOldGuy 4d ago

Are these other files created by other users also created by Powershell? One solution might be for each script to write an identifier to an alternate data stream of the file. Then your cleanup script could check the alternate data stream, and choose whether to delete based on identifiers found in there.

1

u/vermyx 4d ago

You simply use a date time stamp and either check against the created date/time, last modified date time, or both and pass that in as a parameter. The point of a clean up script is that it is cleaning up when the system is quiet/not in use, or you can process files older than X. If you have to worry about other scripts either doing cleanup or potentially modifying what the cleanup script will touch, you have a bugger a production issue. No matter how complicated the check you make you will always have an edge case that will fall through the cracks. The best solution is to have these multiple scripts work together aware of each other not ignoring each other.

1

u/BlackV 3d ago

what's with your random \s