Powershell finding services using a cmdlet dll

Posted by bartonm on Stack Overflow See other posts from Stack Overflow or by bartonm
Published on 2013-10-17T19:58:42Z Indexed on 2013/10/17 21:54 UTC
Read the original article Hit count: 239

Filed under:
|
|
|

I need to upgrade a dll assemblies, written in C#, in our installation. Before I replace the DLL file, I want to check if the file has a lock and if so display a message. How do I implement this in powershell?

I was thinking iterate through Get-Process checking dependencies.

Solved. I iterated through list looking a file path match.

function IsCaradigmPowershellDLLFree()
{

# The list of DLLs to check for locks by running processes.
$DllsToCheckForLocks = "$env:ProgramFiles\Caradigm Platform\System 3.0\Platform\PowerShell\Caradigm.Platform.Powershell.dll",
                       "$env:ProgramFiles\Caradigm Platform\System 3.0\Platform\PowerShell\Caradigm.Platform.Powershell.InternalPlatformSetup.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 match 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. This dll must be have zero locked prior to upgrade. Stop this service to release this lock on $m1."
                $result = $false; 
            }
        }
    }
}

return $result;

}

© Stack Overflow or respective owner

Related posts about c#

Related posts about powershell