I have a number of remote machines whose temporary directories get full. (The are Selenium / webdriver grid remotes). I have a powershell script that identifies the files and directories that need to be cleaned. The command in use looks something like this (excluding complexities of the various machines and directories):
gci $env:TEMP -Recurse| Remove-Item -ErrorAction Continue -Recurse
The problem is that this takes far too long when some files are in use. Locally, I could join to the output of handles (parsing would be a little ugly), but that would be more complicated on a remote machine. Among other things, I'd need to verify that WinRM was configured correctly, handles was in path, etc.
Is there a simpler way to identify that a file is in use? Ideally one that can be filtered on via Powershell (which includes .NET). I'm familiar with a variety of other scripting languages (ruby, python, perl).
The best tool I've found for listing open files is the SysInternals tool handle.exe e.g.:
$openFiles = @(handle $env:TEMP | Foreach {($_ -split ": ")[3]} | Select -Unique)
} return $handleOutput.SubString($start - 1) - Precipitous 2012-04-06 05:53