0

使用 powershell,我想识别放置给定 DLL 的任何进程锁。

解决了。见下文。

4

2 回答 2

0
function IsDLLFree()
{

# The list of DLLs to check for locks by running processes.
$DllsToCheckForLocks = "C:\mydll1.dll","C:\mydll2.dll";

# Assume true, then check all process dependencies
$result = $true;

# Iterate through each process and check module dependencies
foreach ($p in Get-Process) 
{
    # Iterate through each dll used in a given process
    foreach ($m in Get-Process -Name $p.ProcessName -Module -ErrorAction SilentlyContinue)
    {
        # Check if dll dependency matches any DLLs in list
        foreach ($dll in $DllsToCheckForLocks)
        {
            # Compare the fully-qualified file paths, 
            # if there's a match then a lock exists.
            if ( ($m.FileName.CompareTo($dll) -eq 0) )
            {
                $pName = $p.ProcessName.ToString()
                Write-Error "$dll is locked by $pName. Stop this service to release this lock on $m1."
                $result = $false; 
            }
        }
    }
}

return $result;
}
于 2013-10-17T21:54:21.340 回答
0

如果您正在评估当前应用程序域中加载的 dll 文件,则此方法有效。如果您传入 dll 文件的路径,它将返回是否在当前应用程序域中加载了该程序集。即使您不知道 .dll 文件(仍然适用),但想知道一般区域是否有带锁的 .dll 文件,这也特别有用。

function Get-IsPathUsed()
{
    param([string]$Path)
    $isUsed = $false

    [System.AppDomain]::CurrentDomain.GetAssemblies() |? {$_.Location -like "*$Path*"} |% {
        $isUsed = $true;
    }

    $isUsed;
}
于 2014-04-21T19:07:20.173 回答