65

PowerShell で使用して、アプリケーションがファイルをロックしているかどうかを確認するにはどうすればよいですか?

ファイルを閉じることができるように、どのプロセス/アプリケーションがファイルを使用しているかを確認するのが好きです。

4

10 に答える 10

47

これは、SysInternals ツールの handle.exeで実行できます。次のようなことを試してください:

PS> $handleOut = handle
PS> foreach ($line in $handleOut) { 
        if ($line -match '\S+\spid:') {
            $exe = $line
        } 
        elseif ($line -match 'C:\\Windows\\Fonts\\segoeui\.ttf')  { 
            "$exe - $line"
        }
     }
MSASCui.exe pid: 5608 ACME\hillr -   568: File  (---)   C:\Windows\Fonts\segoeui.ttf
...
于 2009-06-05T23:35:56.417 に答える
20

通常のコマンド ラインまたは PowerShell からopenfiles コマンドを使用できるはずです。

openfiles 組み込みツールは、ファイル共有またはローカル ファイルに使用できます。ローカル ファイルの場合は、ツールをオンにしてマシンを再起動する必要があります (これも、初めて使用する場合のみです)。この機能をオンにするコマンドは次のとおりだと思います。

openfiles /local on

例 (Windows Vista x64 で動作):

openfiles /query | find "chrome.exe"

これにより、Chrome に関連付けられたファイル ハンドルが正常に返されます。ファイル名を渡して、現在そのファイルにアクセスしているプロセスを確認することもできます。

于 2009-06-05T21:11:56.537 に答える
16

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:

$lockedFile="C:\Windows\System32\wshtcpip.dll"
Get-Process | foreach{$processVar = $_;$_.Modules | foreach{if($_.FileName -eq $lockedFile){$processVar.Name + " PID:" + $processVar.id}}}
于 2012-11-22T08:39:21.773 に答える
10

SysinternalHandleユーティリティを使用して解決策を見つけることができます。

PowerShell 2.0 で動作するように、コードを (少し) 変更する必要がありました。

#/* http://jdhitsolutions.com/blog/powershell/3744/friday-fun-find-file-locking-process-with-powershell/ */
Function Get-LockingProcess {

    [cmdletbinding()]
    Param(
        [Parameter(Position=0, Mandatory=$True,
        HelpMessage="What is the path or filename? You can enter a partial name without wildcards")]
        [Alias("name")]
        [ValidateNotNullorEmpty()]
        [string]$Path
    )

    # Define the path to Handle.exe
    # //$Handle = "G:\Sysinternals\handle.exe"
    $Handle = "C:\tmp\handle.exe"

    # //[regex]$matchPattern = "(?<Name>\w+\.\w+)\s+pid:\s+(?<PID>\b(\d+)\b)\s+type:\s+(?<Type>\w+)\s+\w+:\s+(?<Path>.*)"
    # //[regex]$matchPattern = "(?<Name>\w+\.\w+)\s+pid:\s+(?<PID>\d+)\s+type:\s+(?<Type>\w+)\s+\w+:\s+(?<Path>.*)"
    # (?m) for multiline matching.
    # It must be . (not \.) for user group.
    [regex]$matchPattern = "(?m)^(?<Name>\w+\.\w+)\s+pid:\s+(?<PID>\d+)\s+type:\s+(?<Type>\w+)\s+(?<User>.+)\s+\w+:\s+(?<Path>.*)$"

    # skip processing banner
    $data = &$handle -u $path -nobanner
    # join output for multi-line matching
    $data = $data -join "`n"
    $MyMatches = $matchPattern.Matches( $data )

    # //if ($MyMatches.value) {
    if ($MyMatches.count) {

        $MyMatches | foreach {
            [pscustomobject]@{
                FullName = $_.groups["Name"].value
                Name = $_.groups["Name"].value.split(".")[0]
                ID = $_.groups["PID"].value
                Type = $_.groups["Type"].value
                User = $_.groups["User"].value.trim()
                Path = $_.groups["Path"].value
                toString = "pid: $($_.groups["PID"].value), user: $($_.groups["User"].value), image: $($_.groups["Name"].value)"
            } #hashtable
        } #foreach
    } #if data
    else {
        Write-Warning "No matching handles found"
    }
} #end function

例:

PS C:\tmp> . .\Get-LockingProcess.ps1
PS C:\tmp> Get-LockingProcess C:\tmp\foo.txt

Name                           Value
----                           -----
ID                             2140
FullName                       WINWORD.EXE
toString                       pid: 2140, user: J17\Administrator, image: WINWORD.EXE
Path                           C:\tmp\foo.txt
Type                           File
User                           J17\Administrator
Name                           WINWORD

PS C:\tmp>
于 2015-07-24T17:25:31.680 に答える
7

私もこれに対する解決策を探していて、いくつかの問題にぶつかりました。

  1. 外部アプリを使いたくない
  2. Open Files にはローカルの ON 属性が必要です。つまり、実行前にそれを使用するようにシステムを構成する必要がありました。

大規模な検索の後、私は見つけました。

https://github.com/pldmgg/misc-powershell/blob/master/MyFunctions/PowerShellCore_Compatible/Get-FileLockProcess.ps1

ポール・ディマジオに感謝

これは純粋なpowershellと.net / C#のようです

于 2020-05-19T13:20:43.157 に答える
0

PowerShell と .NET フレームワーク クラスのみを使用する、ロックされたファイルの検出に関する優れたソリューションを見てきました。

function TestFileLock {
    ## Attempts to open a file and trap the resulting error if the file is already open/locked
    param ([string]$filePath )
    $filelocked = $false
    $fileInfo = New-Object System.IO.FileInfo $filePath
    trap {
        Set-Variable -name filelocked -value $true -scope 1
        continue
    }
    $fileStream = $fileInfo.Open( [System.IO.FileMode]::OpenOrCreate,[System.IO.FileAccess]::ReadWrite, [System.IO.FileShare]::None )
    if ($fileStream) {
        $fileStream.Close()
    }
    $obj = New-Object Object
    $obj | Add-Member Noteproperty FilePath -value $filePath
    $obj | Add-Member Noteproperty IsLocked -value $filelocked
    $obj
}
于 2012-09-15T18:20:59.067 に答える
-1

コマンド プロンプト (CMD) の機能が気に入っており、PowerShell でも使用できます。

tasklist /m <dllName>

DLL ファイルのフル パスを入力できないことに注意してください。名前だけで十分です。

于 2015-04-01T23:39:07.620 に答える
-5

上記の関数を以下のように少し変更すると、TrueまたはFalseが返されます(完全な管理者権限で実行する必要があります)。例:使用法:

PS> TestFileLock "c:\ pagefile.sys"

function TestFileLock {
    ## Attempts to open a file and trap the resulting error if the file is already open/locked
    param ([string]$filePath )
    $filelocked = $false
    $fileInfo = New-Object System.IO.FileInfo $filePath
    trap {
        Set-Variable -name Filelocked -value $true -scope 1
        continue
    }
    $fileStream = $fileInfo.Open( [System.IO.FileMode]::OpenOrCreate, [System.IO.FileAccess]::ReadWrite, [System.IO.FileShare]::None )
    if ($fileStream) {
        $fileStream.Close()
    }
    $filelocked
}
于 2012-09-18T11:40:57.110 に答える