Jaa


Lesson 5: Tracking the Workflow with Windows PowerShell

Time to complete: 25 minutes

Objective: To learn how to configure a tracking profile to monitor variables defined in a workflow with Windows PowerShell.

Purpose: The purpose of this lesson is to show you how to use the Microsoft AppFabric 1.1 for Windows Server cmdlets to configure a custom tracking profile for the Order Service. You will then report the custom tracking information from the monitoring store by using a script function.

Prerequisites

Note the following requirements before beginning this lesson:

Procedure

You will perform the following steps in this lesson:

  1. Create a new custom tracking profile to represent the configurations made with Windows PowerShell.

  2. Report the current tracking configuration with Windows PowerShell.

  3. Use Windows PowerShell to configure the OrderWorkflowService to use the new custom tracking profile.

  4. Report custom tracking information from the monitoring store by using Windows PowerShell.

Creating a New Custom tracking Profile

In this section you will create a new custom tracking profile, which is actually a copy of the one used in Lesson 5: Tracking the Workflow with Windows PowerShell of the Order Service Tutorial. This new tracking profile will have a name with the “_PS” suffix so that you will be able to associate the profile with your configurations by using the AppFabric cmdlets.

  1. In Windows PowerShell, enter the following command.

    Notepad OrderService_PS.tp
    

    Click Yes when Notepad prompts you to create the new file.

  2. Copy and paste the following XML elements into Notepad.

    <trackingProfile name="CustomOrderServiceProfile - Windows PowerShell Config">
       <workflow activityDefinitionId="*">
          <workflowInstanceQueries>
             <workflowInstanceQuery>
                <states>
                   <state name="*" />
                </states>
             </workflowInstanceQuery>
          </workflowInstanceQueries>
    
          <activityStateQueries>
             <activityStateQuery activityName="Wait for order">
                <states>
                   <state name="Closed" />
                </states>
                <variables>
                   <variable name="product"/>
                   <variable name="quantity"/>
                </variables>
             </activityStateQuery>
          </activityStateQueries>
    
          <faultPropagationQueries>
             <faultPropagationQuery faultSourceActivityName="*" faultHandlerActivityName="*" />
          </faultPropagationQueries>
    
          <bookmarkResumptionQueries>
             <bookmarkResumptionQuery name="*" />
          </bookmarkResumptionQueries>
    
          <customTrackingQueries>
             <customTrackingQuery name="*" activityName="*" />
          </customTrackingQueries>
       </workflow>
    </trackingProfile>
    

    This tracking profile enables tracking the product and quantity workflow variables in the Wait for order activity in OrderWorkflow.xamlx.

  3. Close Notepad and click Save when prompted to save changes to OrderService_PS.tp.

Reporting the Tracking Configuration from Windows PowerShell

In this section you will use the AppFabric cmdlets for Windows PowerShell to view the current tracking configuration for the OrderWorkflowService.

  1. Execute the following command in Windows PowerShell to see if tracking is currently enabled for the OrderWorkflowService.

    Get-ASAppServiceTracking -SiteName OrderService_PS -VirtualPath "/OrderWorkflowService/OrderWorkflow.xamlx" | fl *
    

    Your results should look similar to the following.

    IsTrackingEnabled : True
    ProfileName       : HealthMonitoring Tracking Profile
    IsLocal           : True
    BehaviorName      :
    
  2. Execute the following command in Windows PowerShell to see the available tracking profiles for OrderWorkflowService.

    Get-ASAppServiceTrackingProfile -SiteName OrderService_PS -VirtualPath "/OrderWorkflowService/OrderWorkflow.xamlx" | fl Name
    

    Your results should look similar to the following output, which displays the tracking profiles currently available for OrderWorkflowService.

    Name :
    
    Name : ErrorsOnly Tracking Profile
    
    Name : HealthMonitoring Tracking Profile
    
    Name : EndToEndMonitoring Tracking Profile
    
    Name : Troubleshooting Tracking Profile
    

Configuring Custom Tracking by Using Windows PowerShell

In this section you will use the AppFabric cmdlets for Windows PowerShell to configure the OrderWorkflowService to use the new custom tracking profile.

  1. Execute the following command to add OrderService_PS.tp to the list of tracking profiles available for OrderWorkflowService.

    Import-ASAppServiceTrackingProfile -SiteName OrderService_PS -VirtualPath "/OrderWorkflowService/OrderWorkflow.xamlx" -FilePath $(Get-Item ".\OrderService_PS.tp").FullName | fl *
    

    Note that OrderService_PS.tp must be in the current Windows PowerShell directory for this command to succeed.

    Your results from adding the tracking profile should look similar to the following.

    Name        : CustomOrderServiceProfile - Windows PowerShell Config
    SiteName    : OrderService_PS
    VirtualPath : /OrderWorkflowService/OrderWorkflow.xamlx
    IsLocal     : True
    
  2. Execute the following command in Windows PowerShell to see the new list of available tracking profiles for OrderWorkflowService.

    Get-ASAppServiceTrackingProfile -SiteName OrderService_PS -VirtualPath "/OrderWorkflowService/OrderWorkflow.xamlx" | fl Name
    

    Your results should look similar to the following, which includes the profile named “CustomOrderServiceProfile - Windows PowerShell Config”.

    Name :
    
    Name : ErrorsOnly Tracking Profile
    
    Name : HealthMonitoring Tracking Profile
    
    Name : EndToEndMonitoring Tracking Profile
    
    Name : Troubleshooting Tracking Profile
    
    Name : CustomOrderServiceProfile - Windows PowerShell Config
    
  3. To configure the OrderWorkflowService to use the new profile named “CustomOrderServiceProfile - Windows PowerShell Config” execute the following command in Windows PowerShell.

    Set-ASAppServiceTracking -SiteName OrderService_PS -VirtualPath "/OrderWorkflowService/OrderWorkflow.xamlx" -ProfileName "CustomOrderServiceProfile - Windows PowerShell Config" | fl *
    

    Your results should look similar the following.

    IsTrackingEnabled : True
    ProfileName       : CustomOrderServiceProfile - Windows PowerShell Config
    IsLocal           : True
    BehaviorName      :
    
  4. Execute the following command in Windows PowerShell to see the new tracking configuration for the OrderWorkflowService.

    Get-ASAppServiceTracking -SiteName OrderService_PS -VirtualPath "/OrderWorkflowService/OrderWorkflow.xamlx" | fl *
    

    The tracking configuration should be reported as follows.

    IsTrackingEnabled : True
    ProfileName       : CustomOrderServiceProfile - Windows PowerShell Config
    IsLocal           : True
    BehaviorName      :
    

Using Windows PowerShell to Report the Custom Tracking Data

In this section you will use a script function to report workflow events for the tracked workflow variables you configured in the previous section. To report the new tracking information, you must run a new workflow instance and generate the tracking data with the new tracking configuration.

Adding a new script function to report tracking information

Perform the following steps to add a new script function to Utility.ps1 to report the custom tracking information.

  1. In Windows PowerShell, enter the following command.

    Notepad .\Utility.ps1
    
  2. Copy and paste the following script code at the bottom of Utility.ps1 in Notepad.

    #============================================================================================#
    #===                                                                                      ===#
    #=== Retrieves Tracked WF Variable Events for the given workflow and variable from the    ===#
    #=== specified monitoring store.                                                       ===#
    #===                                                                                      ===#
    #============================================================================================#
    
    
    Function GetTrackedWFVariableEvents($FullWorkflowName,$trackedVariable,$database)
    {
    
      $SQL = "SELECT  EventSources.Name AS WorkflowName, " +
                      "EventSources.Computer, " +
                      "EventSources.Site, " +
                      "EventSources.VirtualPath, " + 
                      "WfEvents.Id AS EventID, " + 
                      "WfEvents.WorkflowInstanceId, " +
                      "WfEvents.TimeCreated, " + 
                      "WfEvents.Name AS EventName, " + 
                      "WfEvents.State, " + 
                      "WfEvents.ActivityName, " + 
                      "WfEvents.Exception, " +
                      "WfEventProperties.Name AS TrackedVariableName, " + 
                      "WfEventProperties.Value AS TrackedVariableValue " +
    
              "FROM    EventSources INNER JOIN " +
                      "WfEvents ON EventSources.Id = WfEvents.EventSourceId INNER JOIN " +
                      "WfEventProperties ON WfEvents.Id = WfEventProperties.EventId " +
              "WHERE   EventSources.Name = `'$FullWorkflowName`' AND WfEventProperties.Name = `'$trackedVariable`'"
    
    
      Invoke-Sqlcmd -Query $SQL -Database $database
    }
    

    This new GetTrackedWFVariableEvents function is only a slight modification to the GetWFEvents function. It adds one additional INNER JOIN with the WfEventProperties view to report tracked variable names and their values. It also filters the SQL query by the workflow name and tracked variable name instead of by the InstanceId. This allows you to identify the workflow instance by the Quantity value you set when you place an order. Realistically an order ID should be exposed for tracking.

  3. Close Notepad and click Save to save the changes to Utility.ps1.

  4. Enter the following command in Windows PowerShell to import the new changes to Utility.ps1 for the current Windows PowerShell session.

    Import-Module .\Utility.ps1
    

Generating and reporting the new tracking data

  1. Run OrderClient.exe. This file should have been deployed in the C:\DublinTutorial\OrderClient directory as documented in Lesson 1: Getting Started with AppFabric Cmdlets for Windows PowerShell for the Order Service Tutorial.

  2. Enter 88 for the Quantity value.

  3. Click Submit on the Contoso.com Order Form. Verify that the status section at the bottom of the form shows Your order has been received before continuing. After this is displayed by the form, a new instance of the order workflow is running simulating the processing of an order.

  4. Wait about 30 seconds to give time for the recording of the tracking information.

  5. In the Windows PowerShell window, enter the following command to report the tracking events.

    GetTrackedWFVariableEvents "Microsoft.Samples.Dublin.Tutorials.OrderService.OrderWorkflowService.OrderWorkflow" quantity OrderService_PS
    

    Your results should be similar the following report. Notice the TrackedVariableName and TrackedVariableValue that were tracked for the workflow instance.

    WorkflowName         : Microsoft.Samples.Dublin.Tutorials.OrderService.OrderWorkflowService.OrderWorkflow
    Computer             : server1
    Site                 : OrderService_PS
    VirtualPath          : /OrderWorkflowService/OrderWorkflow.xamlx
    EventID              : 1439
    WorkflowInstanceId   : ac0fd7fb-d463-4683-9c75-80d90a8b75ed
    TimeCreated          : 2009-11-02 10:09:21.3108822
    EventName            : Wait for order
    State                : Closed
    ActivityName         :
    Exception            :
    TrackedVariableName  : quantity
    TrackedVariableValue : 88
    

What Did I Just Do?

In this lesson you created a new tracking profile and used the AppFabric cmdlets for Windows PowerShell to configure the OrderWorkflowService to use the new profile. Then you generated the tracking information by creating a new instance of the workflow and reported the tracking data by using a custom Windows PowerShell script function.

See Also

Concepts

Lesson 1: Getting Started with AppFabric Cmdlets for Windows PowerShell
Lesson 2: Deploying the Order Service Application with Windows PowerShell
Lesson 3: Configuring the Order Service with Windows PowerShell
Lesson 4: Monitoring the Order Service with Windows PowerShell
Lesson 5: Tracking the Workflow with Windows PowerShell

  2012-09-12