Advertisement
reidca

Get Locking Processes in Powershell

Mar 17th, 2014
519
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function Get-LockingProcesses()
  2. {
  3.     [cmdletbinding()]
  4.     Param(
  5.      [Parameter(Position=0,Mandatory=$True,
  6.      HelpMessage="What is the path or filename? You can enter a partial name without wildcards")]
  7.      [Alias("name")]
  8.      [ValidateNotNullorEmpty()]
  9.      [string]$Path
  10.     )
  11.  
  12.     #define the path to Handle.exe, uses the PSScriptRoot variable to get the current path of the executing code
  13.     $handleUtilityFilePath = "$PSScriptRoot\handle.exe"
  14.  
  15.     #Ensure the file is present
  16.     If (Test-Path -Path $handleUtilityFilePath -PathType Leaf)
  17.     {
  18.         #Arguments to handle specifying to accept license and search for usernames
  19.         $data = &$handleUtilityFilePath /accepteula $path -u
  20.  
  21.         #Define regex including capture groups for each column we are interested in, throw away the code before the path
  22.         [string]$matchPattern = '^(?<Name>\w+\.\w+)\s+pid:\s+(?<PID>\b(\d+)\b)\s+type:\s+(?<Type>\w+)\s+(?<Username>\S+)\s+\w+:\s+(?<Path>\S+)$'
  23.        
  24.         $lockingProcesses = @()
  25.         #Iterate ove the lines in the output of handle and try to match each one usign the reg ex
  26.         foreach ($line in $data)
  27.         {
  28.             $myMatch = [RegEx]::Match($line, $matchPattern)
  29.             if ($myMatch.Value)
  30.             {
  31.                 #Match found so add a cusom object using the capture groups to our array
  32.                 $lockingProcesses += [PSCustomObject] @{
  33.                     FullName = $myMatch.groups["Name"].value
  34.                     Name = $myMatch.groups["Name"].value.split(".")[0]
  35.                     ID = $myMatch.groups["PID"].value
  36.                     Type = $myMatch.groups["Type"].value
  37.                     Username = $myMatch.groups["Username"].value
  38.                     Path = $myMatch.groups["Path"].value
  39.                     CommandLine = (Get-WmiObject Win32_Process -Filter "ProcessId = $($myMatch.groups["PID"].value)").CommandLine #Include the command line of the process incase of multiple entries or generic process names
  40.                 }
  41.             }
  42.         }
  43.  
  44.         if ($lockingProcesses.Count -eq 0)
  45.         {
  46.             Write-Warning "No matching handles found"      
  47.         }
  48.  
  49.         #Remove duplicate entries
  50.         $lockingProcesses = $lockingProcesses | Select-Object -Unique
  51.  
  52.         return ,[array]$lockingProcesses  #unravel when sending pack to ensure it is kept as an array
  53.     }
  54.     else
  55.     {
  56.         Throw "Cannot find required utility ""handle.exe"" at path ""$handleUtilityFilePath"""
  57.     }
  58. } #end function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement