检查目录中的锁定文件并找到锁定应用程序 [英] Check for locked files in Directory and find locking applicaiton

查看:36
本文介绍了检查目录中的锁定文件并找到锁定应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个 PowerShell 脚本,该脚本将查找在提供的目录中锁定的所有文件并返回锁定该文件的应用程序/进程.

I am wanting to create a PowerShell script that will find any files that are locked within a supplied directory and return the application/process that has locked the file.

我发现了一个脚本,它通过一个目录,当它找到一个锁定的文件时,它会停止并报告文件被锁定.

I have found a script which goes through a directory and when it finds a locked file will stop and report the file is locked.

Function Test-Lock{
    param(
    [parameter(ValueFromPipeline=$true)]
    $Path
    )
    Process {
        if($Path.Attributes -ne 'Directory'){
            $oFile = New-Object System.IO.FileInfo $Path.FullName
            $locked=$false
            try{
                $oStream = $oFile.Open([System.IO.FileMode]::Open, [System.IO.FileAccess]::ReadWrite, [System.IO.FileShare]::None)
                $oStream.Close()
                }
            catch{
                write-host $Error[0]
                $locked=$true
                New-Object PSObject -Property @{'Path'=$Path.FullName;'Locked?'=$locked} | Select Path,Locked?
                } 

        }
    }
}
$lockedfiles=(gci C:\temp | Test-Lock)
$lockedfiles | Format-Table -auto

返回:

Path                  Locked?
----                  ------- 
C:\temp\ReportReq.doc    True

我不知道如何找到锁定此文件的应用程序或进程.

I cannot work out how to find the application or proecess that has this file locked.

推荐答案

好吧,您需要使用 sysinternals 中的 handle.exe 来实现此目的.PowerShell MVP Adam Driscoll 编写了 sysinternals handle.exe 的 PowerShell 版本.您可以在以下位置检查:https://github.com/adamdriscoll/PoshInternals/blob/master/Handle.ps1

Well, You need to use handle.exe from sysinternals to achieve this. Adam Driscoll, a PowerShell MVP, wrote a PowerShell version of sysinternals handle.exe. You can check that at: https://github.com/adamdriscoll/PoshInternals/blob/master/Handle.ps1

此外,Keith Hill 在这里回答了一个类似的问题:用于检查锁定文件的应用程序的 PowerShell 脚本?

Also, there is a similar question answered here by Keith Hill: PowerShell script to check an application that's locking a file?

这篇关于检查目录中的锁定文件并找到锁定应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆