Results 1 to 3 of 3
  1. #1
    Lounger
    Join Date
    Oct 2013
    Posts
    37
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Run Batch files in Powershell script

    Hello,

    I want to run the batch files from powershell script and i searched the net & according to me this is good: [ & 'C:\somefolder\batchfile.bat' ]
    I have 3 batch which i want to run consecutively.

    My question is that how to make the powershell to hold on, it all the three batch files complete there task one after the other and each exit the window.

    And if failure exit the powershell script and if successfu then ping the google site to Test-Connection and if connection is available start sending email.
    I have the mail sending script.
    Awaiting for favourable reply.

  2. #2
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,436
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    ZMagic,

    What you need is the $LastExitCode variable in PS.
    Code:
    Clear-Host
    cmd.exe "/c G:\BEKDocs\Batch\Batch1.cmd" 
    Write-Host "The Exit code from Batch1 is " $LastExitCode
    cmd.exe "/c G:\BEKDocs\Batch\Batch2.cmd" 
    Write-Host "The Exit code from Batch2 is " $LastExitCode
    Output:
    Hello there
    Hello there
    Hello there
    Hello there
    Hello there
    Hello there
    Hello there
    Hello there
    Hello there
    Hello there
    The Exit code from Batch1 is 200
    The Exit code from Batch2 is 400

    Batch1.cmd
    Code:
    @Echo off
    for %%i in (1,2,3,4,5,6,7,8,9,10) DO Echo. Hello there
    exit /B 200
    Batch2.cmd
    Code:
    @Echo off
    exit /B 400
    HTH
    Last edited by RetiredGeek; 2014-02-01 at 17:02.
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  3. #3
    Lounger
    Join Date
    Oct 2013
    Posts
    37
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Hello,

    RG, Thanks for Help, Solved.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •