@if not defined debugbat echo off

    : Turn on "delayed expansion" so we can manipulate variables properly in for loops.
    setlocal ENABLEDELAYEDEXPANSION

    : Defaults
    set create=0
    set start=0
    set stop=0
    set delete=0
    set show=0
    set name="WSFCCluster"
    set out="c:\WSFCCluster"
    set filter="Cluster"

    set hosts=

    if "%1%" == "" goto Usage

:argloop

    if "%1%" == "" goto endargloop

    if "%1%" == "/?" goto Usage
    if "%1%" == "-?" goto Usage

    if "%1%" == "-create" (
        set create=1
    ) else if "%1%" == "-c" (
        set create=1
    ) else if "%1%" == "-start" (
        set start=1
    ) else if "%1%" == "-stop" (
        set stop=1
    ) else if "%1%" == "-delete" (
        set delete=1
    ) else if "%1%" == "-d" (
        set delete=1
    ) else if "%1%" == "-show" (
        set show=1
    ) else if "%1%" == "-q" (
        set show=1
    ) else if "%1%" == "-query" (
        set show=1
    ) else if "%1%" == "-name" (
        set name=%2%
        shift
    ) else if "%1%" == "-n" (
        set name=%2%
        shift
    ) else if "%1%" == "-out" (
        set out=%2%
        shift
    ) else if "%1%" == "-o" (
        set out=%2%
        shift
    ) else if "%1%" == "-filter" (
        set filter=%2%
        shift
    ) else if "%1%" == "-flt" (
        set filter=%2%
        shift
    ) else if "%1%" == "-f" (
        set filter=%2%
        shift
    ) else (
        set hosts=%hosts% %1%
    )

    shift
    goto argloop

:endargloop

    : Use either MYNLBNODES or NLBNODES
    if "%hosts%" == "" set hosts=%myclusnodes%
    if "%hosts%" == "" set hosts=%clusnodes%
    if "%hosts%" == "" (
        if "%create%" == "1" goto Usage
    )
    if DEFINED CLUSPERFNAME (
       set name=%CLUSPERFNAME%
    )
    if DEFINED CLUSPERFPATH (
       set out=%CLUSPERFPATH%
    )

    if "%create%" == "1" set delete=1
    if "%delete%" == "1" set stop=1

    if "%stop%" == "1" call :StopSession
    if "%delete%" == "1" call :DeleteSession
    if "%create%" == "1" call :CreateSession
    if "%start%" == "1" call :StartSession
    if "%show%" == "1" call :ShowSession

    goto :end

:
:ShowSession
:

    echo Showing Log Session: %name%

    logman query %name%

    dir %out%*.*

    goto :eof

:
:CreateSession
:

    echo Creating Log Session: %name% at %out% using filter %filter%
    echo For nodes: %hosts%

    if exist %temp%\counters_tmp.txt (del %temp%\counters_tmp.txt)

    for %%h in ( %hosts% ) do (
        echo Generating counters list for the host %%h
        typeperf -s %%h -q | findstr %filter% >> %temp%\counters_tmp.txt
    )
    echo Creating log session
    logman create counter %name% -o %out% -cf %temp%\counters_tmp.txt

    if exist %temp%\counters_tmp.txt (del %temp%\counters_tmp.txt)

    goto :eof

:
:StartSession
:

    echo Starting Log Session: %name%

    logman start %name%

    goto :eof

:
:StopSession
:

    echo Stopping Log Session: %name%

    logman stop %name%

    goto :eof

:
:DeleteSession
:

    echo Deleting Log Session: %name%

    logman delete %name%

    goto :eof


:Usage

    echo.
    echo.%0 [-c] [-stop] [-start] [-d] [-show] [-q] [-n {session name}] [-o {path to the log files}] [-f {counters filter}] [{list of nodes}]
    echo.
    echo -c           - cerate new session.
    echo                aliases: create.
    echo -start       - starts session.
    echo -stop        - stops session.
    echo -d           - deletes session session.
    echo                aliases: delete.
    echo -show        - prints session details.
    echo                aliases: q, query.
    echo -q           - same as show.
    echo                aliases: show, query.
    echo -n {name}    - Session name.
    echo                aliases: name.
    echo                default: WSFCCluster.
    echo -o {path}    - path to the trace files.
    echo                aliases: out.
    echo                default: c:\WSFCCluster.
    echo -f {str}     - filter for the counters we want to monitor.
    echo                aliases: flt, filter.
    echo                default: Cluster.
    echo.
    echo You can specify multiple commands and they will be executed in the following order
    echo    1.stop
    echo    2.dlete
    echo    3.create
    echo    4.start
    echo    5.show
    echo.
    echo Note that if you specify create then it already implies stop and delete.
    echo Note that if you specify delete then it already implies stop.
    echo.
    echo You can provide list of nodes using clusnodes or myclusnodes environment variables
    echo.
    goto :eof

:end
    endlocal