Reading the return value from Main() in VbScript,Batch File,PowerShell Script

by jask2002 18. October 2012 01:50

Main() method in C# can have either (void or int) as the return value

Void:

namespace TestSolution
{
    class Program
    {
        static void Main(string[] args)
        {

        }
    }
}

int:

namespace TestSolution
{
    class Program
    {
        static int Main(string[] args)
        {
            return 1001;
        }
    }
}

At this point the question arises in my mind is how to read the return value from the Main() when called from any script VBscript or batch file or PowerShell?

Here is the solution

VbScript

iRetVal = 0
SPath = "\Scripts\TestSolution.exe"
iRetVal = oShell.Run (sPath,0,TRUE)
msgbox iRetVal
If iRetVal = 1001 then
          ' DO something
Else
          ' Do something else
End if

 

Batch File

rem test.bat
@echo off
TestSolution
@if "%ERRORLEVEL%" == "1001" goto good

:good
    echo Execution Succeded
    echo return value = %ERRORLEVEL%
    goto end

:end

PowerShell

PS C:\Users\jaskis\Documents\Visual Studio 2010\Projects\TestSolution\TestSolution\bin\Debug> .\TestSolution.exe
PS C:\Users\jaskis\Documents\Visual Studio 2010\Projects\TestSolution\TestSolution\bin\Debug> $lastexitcode
1001


PayPal — The safer, easier way to pay online. Has this post helped you? Saved you? If you'd like to show your appreciation. Please buy me a coffee or make a small contribution toward blog's maintenance(to keep it Ads free )

Tags: , , ,

.Net | C#

Pingbacks and trackbacks (1)+

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

About me

Hi there,

My name is  Jas and I'm currently working with Microsoft IIS/ASP.Net Escalation services.  Services

 

Tag cloud

Month List

RecentComments

Comment RSS

TextBox

 

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.