We have moved to www.dataGenX.net, Keep Learning with us.

Thursday, July 05, 2012

BAT Script to execute a DataStage Job from Command line in Windows


Hi Guys,
Here I am sharing a BAT script for executing the DS job on Windows Server.
Please go through with it and let me know if caught in any issue or problem
  


@echo off
:: -----------------------------------------------------------------
:: Name: CommonProviderExtract.bat
:: -----------------------------------------------------------------
:: Description:
:: Run the DataStage job from command line using the parameters.
:: -----------------------------------------------------------------
:: Created by: Sreeram Makam, April 29,2010
:: -----------------------------------------------------------------
:: Todo:
::    Add more functionality like job reports.
:: -----------------------------------------------------------------
:: Required Components:
::     dsjob.exe (Windows Version)
:: -----------------------------------------------------------------
:: Command Line Parameters:
:: 1. Host
:: 2. User
:: 3. Password
:: 4. Project
:: -----------------------------------------------------------------
:: Ensure that everything that are set here are not permanent.
:: -----------------------------------------------------------------
    SETLOCAL
:: -----------------------------------------------------------------
:: Test for command line parameters.
:: -----------------------------------------------------------------
    IF "%1"=="" GOTO Syntax
    IF "%2"=="" GOTO Syntax
    IF "%3"=="" GOTO Syntax
    IF "%4"=="" GOTO Syntax
:: -----------------------------------------------------------------
:: Set paramters.
:: -----------------------------------------------------------------
    SET Host=%1
    SET User=%2
    SET Password=%3
    SET Project=%4
 SET TGTSSODSDSN=<DSN_Name>
 SET TGTSODSUserID=<UserName>
 SET TGTSODSPassword=<Password>
 SET EnterpriseInterfacesDSN=<Target DSN>
 SET EnterpriseInterfacesUID=<Username>
 SET EnterpriseInterfacesPWD=<Password>
 SET JobName=Common_ProviderExtract
:: -----------------------------------------------------------------
:: Hard-coded values.  Dependent on each computer.
:: -----------------------------------------------------------------
    SET Designer=C:\Progra~1\Ascential\DataStage\dsdesign.exe
    SET DsJob=C:\IBM\InformationServer\Clients\Classic\dsjob
    SET JobList=DsJobReportList.txt
    SET ProjectList=ProjectList.txt
    SET DSLog=DsJobReportLog
    SET BackupDir=C:\tmp\log
:: -----------------------------------------------------------------
:: Get the current Date
:: -----------------------------------------------------------------
    FOR /f "tokens=2-4 delims=/ " %%a in ('DATE/T') do SET DsxDate=%%c%%a%%b
:: -----------------------------------------------------------------
:: Get the current Time
:: -----------------------------------------------------------------
    FOR /f "tokens=1* delims=:" %%a in ('ECHO.^|TIME^|FINDSTR "[0-9]"') do (SET DsxTime=%%b)
:: -----------------------------------------------------------------
:: Set delimeters so that current time can be broken down into components
:: then execute FOR loop to parse the DsxTime variable into Hr/Min/Sec/Hun.
:: -----------------------------------------------------------------
    SET delim1=%DsxTime:~3,1%
    SET delim2=%DsxTime:~9,1%
    FOR /f "tokens=1-4 delims=%delim1%%delim2% " %%a in ('echo %DsxTime%') do (
        set DsxHr=%%a
        set DsxMin=%%b
        set DsxSec=%%c
        set DsxHun=%%d
    )
:: -----------------------------------------------------------------
:: If provided directory is missing an ending \, append it.
:: Validate %BackupDir%'s existance.
:: -----------------------------------------------------------------
    if exist %BackupDir%\ set BackupDir=%BackupDir%\
    if NOT exist %BackupDir% GOTO BadMain
:: -----------------------------------------------------------------
:: Set the log file name to improve readability of code.
    SET LogFileName=%BackupDir%%DSLog%-%DsxDate%-%DsxHr%_%DsxMin%_%DsxSec%.log
:: -----------------------------------------------------------------
:: Announce to log of this program's run.
:: -----------------------------------------------------------------
    ECHO. > %LogFileName%
    ECHO DsJobReport ran on %DsxDate% %DsxHr%:%DsxMin%:%DsxSec% with the following parameters >> %LogFileName%
    ECHO Host=%Host% >> %LogFileName%
    ECHO User=%User% >> %LogFileName%
    ECHO BackupDir=%BackupDir%%DsxDate%\ >> %LogFileName%
    ECHO Designer=%Designer% >> %LogFileName%
    ECHO DsJob=%DsJob% >> %LogFileName%
    ECHO ProjectList=%ProjectList% >> %LogFileName%
    ECHO JobList=%JobList% >> %LogFileName%
    ECHO DSLog=%DSLog% >> %LogFileName%
    ECHO. >> %LogFileName%
:: -----------------------------------------------------------------
:: Begin Job Execution
::
:: NOTE:  %ERRORLEVEL% does not work for some reason.
:: -----------------------------------------------------------------
        ECHO %DsJob% -server %Host% -user %User% -password %Password% -run -wait -jobstatus -param $TGTSSODSDSN=%TGTSSODSDSN% -param $TGTSODSUserID=%TGTSODSUserID% -param $TGTSODSPassword=%TGTSODSPassword% %Project% %JobName% >> %LogFileName%
    %DsJob% -server %Host% -user %User% -password %Password% -run -wait -jobstatus -param $TGTSSODSDSN=%TGTSSODSDSN% -param $TGTSODSUserID=%TGTSODSUserID% -param $TGTSODSPassword=%TGTSODSPassword% %Project% %JobName%
        IF NOT %ERRORLEVEL%==0 GOTO ProjFail
        ECHO. >> %LogFileName%
        ECHO *** Completed Job execution for Job: %%i on Host: %Host% Project: %Project% >> %LogFileName%
        ECHO     to File: %BackupDir%%DsxDate%\%%i.html >> %LogFileName%
        ECHO. >> %LogFileName%
    )
:: -----------------------------------------------------------------
::    ECHO *** Export completed successfully for projects:
::    type %TempFile%
:: -----------------------------------------------------------------
    GOTO EXITPT
:: -----------------------------------------------------------------
:: a job failed to be exported.
:: -----------------------------------------------------------------
:ProjFail
    ECHO.
    ECHO *** ERROR:  Errorlevel did not return a value 0: %%i on Host: %Host% on Project: %Project%
    ECHO.
    ECHO. >> %LogFileName%
    ECHO *** ERROR:  Errorlevel did not return a value 0: %%i on Host: %Host% on Project: %Project% >> %LogFileName%
    ECHO. >> %LogFileName%
GOTO EXITPT
:: -----------------------------------------------------------------
:: Report that paramters are not valid on screen and log file.
:: Note: Password are not reported for better security.
:: -----------------------------------------------------------------
:BadParam
    echo.
    echo Invalid parameters - Unable to access Server.
    echo.
    echo. >> %LogFileName%
    echo Invalid parameters - Unable to access Server. >> %LogFileName%
    echo. >> %LogFileName%
GOTO Syntax
:: -----------------------------------------------------------------
:: Report that directory is non-existant.
:: -----------------------------------------------------------------
:BadMain
    echo.
    echo Bad/Non-existing directory: %BackupDir%
    echo.
    echo Please ensure that you have permission to access/create directories
    echo and files.  Also ensure that directory listed exists.
    echo.
    echo. >> %LogFileName%
    echo Bad/Non-existing directory: %BackupDir% >> %LogFileName%
    echo. >> %LogFileName%
GOTO EXITPT
:: -----------------------------------------------------------------
:: Report that program does not have privs to create directory.
:: -----------------------------------------------------------------
:BadDir
    echo.
    echo Unable to create subdirectory: %BackupDir%%DsxDate%\
    echo.
    echo Please ensure that you have permission to access/create directories
    echo and files.
    echo.
    echo. >> %LogFileName%
    echo Unable to create subdirectory: %BackupDir%%DsxDate%\ >> %LogFileName%
    echo. >> %LogFileName%
GOTO EXITPT
:: -----------------------------------------------------------------
:: Report proper syntax usage.
:: -----------------------------------------------------------------
:Syntax
    echo.
    echo DataStage DsJobReport Routine
    echo.
    echo Based on design by Sreeram Makam
    echo.
    echo Usage: CommonProviderExtract Server User Password Project
    echo.
GOTO ENDPOINT
:: -----------------------------------------------------------------
:EXITPT
    ECHO. >> %LogFileName%
:: -----------------------------------------------------------------
:ENDPOINT
    ENDLOCAL




njoy the simplicity.......
Atul Singh

No comments :

Post a Comment