Using WMI task to query for list of processes

Sep 21, 2009 at 7:02 PM

Hello,
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!

Michael

Build file: 

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

  <Target Name="All">
    <PropertyGroup>
      <AppMachine>192.168.5.1</AppMachine>
      <AppUser>myuser</AppUser>
      <AppPassword>mypwd</AppPassword>
    </PropertyGroup>

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

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

    <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!" />
  </Target>

</Project>

Output:

[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!
Done Building Project "C:\MSBuildTesting\KillProcess3.proj" (default targets).


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

Time Elapsed 00:00:02.50

C:\MSBuildTesting>

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 end...so it only ever adds one.  Shouldn't that be inside the loop, when it is iterating through the ManagementObjects that the query found?

Coordinator
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.
Coordinator
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 (http://msbuildextensionpack.codeplex.com/WorkItem/View.aspx?WorkItemId=1399) which hopefully does not affect you. This is scheduled for fixing in the 3.5.4.0 release which will hopefully ship soon.

Mike