This project is read-only.

Using WMI task to query for list of processes

Sep 21, 2009 at 7:02 PM

I am trying to use the Wmi task to run a query to return the Name and ProcessID properties for any running instances of notepad (for example).  So far, I am only able to get the Message task to ourput the first process it finds.  My build file and results are below.  Hopefully I'm just missing something obvious. :)  Thanks!


Build file: 

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="3.5" DefaultTargets="All"  xmlns="">
  <Import Project="C:\Program Files\MSBuild\ExtensionPack\MSBuild.ExtensionPack.tasks"/>

  <Target Name="All">

    <Message Text="Checking to see if any notepad processes are running."/>
      <WmiProps Include="Name"/>
      <WmiProps Include="ProcessID"/>

    <MSBuild.ExtensionPack.Management.Wmi TaskAction="Query"
                                          Namespace="\root\CIMV2" Properties="@(WmiProps)" MachineName="$(AppMachine)"
                                          UserName="$(AppUser)" UserPassword="$(AppPassword)" ContinueOnError="true">
      <Output TaskParameter="Info" ItemName="WmiInfo" />

    <Message Text="Processes found: Name: %(WmiInfo.Name), ProcessID: %(WmiInfo.ProcessID)" ContinueOnError="true"/>

    <!--<Message Text="Processes found: Name: @(WmiInfo->'%(Name)'), ProcessID: @(WmiInfo->'%(ProcessID)')" ContinueOnError="true"/>-->

    <Message Text="DONE!" />



[Microsoft .NET Framework, Version 2.0.50727.4005]
Copyright (C) Microsoft Corporation 2007. All rights reserved.

Build started 9/21/2009 1:46:54 PM.
Project "C:\MSBuildTesting\KillProcess3.proj" on node 0 (default targets).
  Checking to see if any notepad processes are running.
  Executing WMI query: SELECT * FROM Win32_Process WHERE Name LIKE 'notep%'
  Extracting Property: Name
  Extracting Property: ProcessID
  Extracting Property: Name
  Extracting Property: ProcessID
  Extracting Property: Name
  Extracting Property: ProcessID
  Processes found: Name: notepad.exe, ProcessID: 1256
Done Building Project "C:\MSBuildTesting\KillProcess3.proj" (default targets).

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:02.50


Sep 21, 2009 at 7:37 PM

I was peeking in the source with Reflector.  It looks like the Wmi.Query() method adds an ITaskItem object to the info array only at the it only ever adds one.  Shouldn't that be inside the loop, when it is iterating through the ManagementObjects that the query found?

Sep 22, 2009 at 9:01 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Sep 22, 2009 at 9:20 AM
Edited Sep 22, 2009 at 9:21 AM

Hi Mike

Thanks for logging this. I've just checked in a fix.

Note we have an open bug against this wmi task ( which hopefully does not affect you. This is scheduled for fixing in the release which will hopefully ship soon.