Joben.Rara.blogs(".NET")

There are two ways to write error-free programs; only the third one works.

How To: Check if there are processes that are running using .NET (using PowerShell)

Keith posted a console app he created to determine the processes that are running .NET applications.

I got thinking that this type of "utility" apps are perfect for PowerShell. So I took Keith's code and made an equivalent powershell script.

$dotnet = @()

$procs = get-process

foreach ($proc in $procs)
{   
   $mods = $proc.Modules
  
   foreach ($mod in $mods)
   {
      if ($mod.ModuleName -ieq "mscorwks.dll")
      {
          $info = new-object -typename System.Object
          $info | add-member -membertype noteProperty -name ProcessName -value $proc.ProcessName
          [void] $foreach.MoveNext()
          $info | add-member -membertype noteProperty -name VersionInfo -value $mod.FileVersionInfo.ProductVersion
       
          $dotnet += $info       
      }
   }
}
  
$dotnet | format-table * -auto
"Processes found : {0} | Processes running .NET : {1}" -f $procs.Length, $dotnet.Length

And here's a one-liner version for the script. 

gps | % { $procname = $_.ProcessName; $_.Modules | % { if($_.ModuleName -ieq "mscorwks.dll
") { "{0} {1}" -f $procname,$_.FileVersionInfo.ProductVersion }}}

...well, a partial one. ;-)

Source: Keith Rull : How To: Check if there are processes that are running using .NET

Posted: 03-16-2007 8:12 PM by velocity | with 1 comment(s)
Filed under: ,

Comments

keithrull said:

great sample Joben!

# March 16, 2007 2:46 PM