用于检查锁定文件的应用程序的 PowerShell 脚本?
在 PowerShell 中使用,如何检查应用程序是否锁定文件?
我喜欢检查哪个进程/应用程序正在使用该文件,以便我可以关闭它。
Using in PowerShell, how can I check if an application is locking a file?
I like to check which process/application is using the file, so that I can close it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
您可以使用SysInternals 工具handle.exe 来执行此操作。 尝试这样的事情:
You can do this with the SysInternals tool handle.exe. Try something like this:
这可以帮助您: 使用 PowerShell 找出哪个进程锁定了文件。 它解析每个进程的 System.Diagnostics.ProcessModuleCollection Modules 属性,并查找锁定文件的文件路径:
简化:
This could help you: Use PowerShell to find out which process locks a file. It parses the System.Diagnostics.ProcessModuleCollection Modules property of each process and it looks for the file path of the locked file:
Simplified:
您应该能够使用 openfiles 命令常规命令行或 PowerShell。
openfiles 内置工具可用于文件共享或本地文件。 对于本地文件,您必须打开该工具并重新启动计算机(同样,仅限第一次使用)。 我相信打开此功能的命令是:
例如(适用于 Windows Vista x64):
成功返回与 Chrome 关联的文件句柄。 您还可以传入文件名来查看当前访问该文件的进程。
You should be able to use the openfiles command from either the regular command line or from PowerShell.
The openfiles built-in tool can be used for file shares or for local files. For local files, you must turn on the tool and restart the machine (again, just for first time use). I believe the command to turn this feature on is:
For example (works on Windows Vista x64):
That successfully returns file handles associated with Chrome. You can also pass in a file name to see the process currently accessing that file.
我也在寻找解决方案,但遇到了一些问题。
经过广泛的搜索我发现了。
https://github.com/pldmgg/misc -powershell/blob/master/MyFunctions/PowerShellCore_Compatible/Get-FileLockProcess.ps1
感谢 Paul DiMaggio
这似乎是纯 powershell 和 .net / C#
I was looking for a solution to this as well and hit some hiccups.
After extensive searching I found.
https://github.com/pldmgg/misc-powershell/blob/master/MyFunctions/PowerShellCore_Compatible/Get-FileLockProcess.ps1
Thanks to Paul DiMaggio
This seems to be pure powershell and .net / C#
您可以使用 Sysinternal 的 句柄实用程序。
我必须(稍微)修改代码才能使用 PowerShell 2.0:
示例:
You can find a solution using Sysinternal's Handle utility.
I had to modify the code (slightly) to work with PowerShell 2.0:
Example:
您可以在handle.exe 上找到您的路径。
我使用了 PowerShell,但您可以使用其他命令行工具。
具有管理权限:
向下搜索“Thread: ...”,您应该会看到使用您的路径的进程的名称。
You can find for your path on handle.exe.
I've used PowerShell but you can do with another command line tool.
With administrative privileges:
Down the lines and search for "Thread: ...", you should see there the name of the process using your path.
在 PsGallery 中发布了一个 PowerShell 模块来发现和使用 杀死打开文件或文件夹句柄的进程。
它公开了以下功能:1) 查找锁定进程,2) 终止锁定进程。
该模块在第一次使用时会自动下载handle.exe。
Find-LockingProcess()
检索具有打开指定路径的文件句柄的进程信息。
示例:Find-LockingProcess -Path $Env:LOCALAPPDATA
示例:Find-LockingProcess -Path $Env:LOCALAPPDATA | Get-Process
Stop-LockingProcess()
终止所有打开指定路径的文件句柄的进程。
示例:Stop-LockingProcess -Path $Home\Documents
PsGallery 链接:https:/ /www.powershellgallery.com/packages/LockingProcessKiller
要安装,请运行:
安装模块-名称 LockingProcessKiller
Posted a PowerShell module in PsGallery to discover & kill processes that have open handles to a file or folder.
It exposes functions to: 1) find the locking process, and 2) kill the locking process.
The module automatically downloads handle.exe on first usage.
Find-LockingProcess()
Retrieves process information that has a file handle open to the specified path.
Example: Find-LockingProcess -Path $Env:LOCALAPPDATA
Example: Find-LockingProcess -Path $Env:LOCALAPPDATA | Get-Process
Stop-LockingProcess()
Kills all processes that have a file handle open to the specified path.
Example: Stop-LockingProcess -Path $Home\Documents
PsGallery Link: https://www.powershellgallery.com/packages/LockingProcessKiller
To install run:
Install-Module -Name LockingProcessKiller
我遇到了这个问题并编写了一个完全独立的脚本,因为我不想依赖 SysInternals。 在进行完整的递归复制之前,脚本将识别并终止锁定文件的任何进程。
https://github.com/Tikinsin/ForceCopy.ps1/blob/ main/ForceCopy.ps1
这利用了 Zachery Fischer 和 Paul DiMaggio 的 Github 解决方案的答案。
I ran into this issue and wrote an entirely self contained script because I didn't want to depend on SysInternals. Script will identify and kill any process locking a file before making a full recursive copy.
https://github.com/Tikinsin/ForceCopy.ps1/blob/main/ForceCopy.ps1
This leverages the answer by Zachery Fischer and Paul DiMaggio's Github solution.
这是一个完全工作的独立 Powershell 解决方案,无需任何外部工具/依赖项。
它使用函数“NtQueryInformationFile" 带有参数 FileInformationClass=47 获取 FILE_PROCESS_IDS_ USING_FILE_INFORMATION 数据。
Here is a fully working standalone Powershell solution without any external tools/dependencies.
It uses the function "NtQueryInformationFile" with parameter FileInformationClass=47 to get FILE_PROCESS_IDS_USING_FILE_INFORMATION data.
我喜欢命令提示符 (CMD) 的功能,它也可以在 PowerShell 中使用:
请注意,您无法输入 DLL 文件的完整路径。 只要名字就足够了。
I like what the command prompt (CMD) has, and it can be used in PowerShell as well:
Just note that you can't enter the full path of the DLL file. Just the name is good enough.
我在 锁定文件检测< 中看到了一个很好的解决方案/a> 仅使用 PowerShell 和 .NET 框架类:
I've seen a nice solution at Locked file detection that uses only PowerShell and .NET framework classes:
如果你稍微修改上面的函数,如下所示,它将返回 True 或 False
(您需要以完全管理员权限执行)
例如用法:
If you modify the above function slightly like below it will return True or False
(you will need to execute with full admin rights)
e.g. Usage: