Solved

Passing username and Password to a batch file

Posted on 2010-09-18
8
890 Views
Last Modified: 2012-06-27
I have a windows form in C# that I would like to use to pass the username and password to a batch file. Does anyone know how to do this?
the batch file code is
@echo off
pushd "%programfiles%\checkpoint\securemote\bin"
if not exist scc.exe goto scc_missing
echo #### Setting command-line mode
scc.exe setmode cli
echo #### Setting login information
scc.exe userpass YOURLOGIN YOURPASSWORD
echo #### Starting VPN connection
scc.exe connect -p Home
echo -
echo #### Testing connection
ping -w 1000 -n 4 192.168.0.0> NUL
if errorlevel 1 echo Connection test failed!
if errorlevel 1 goto disconnect
if errorlevel 0 echo Connection tested OK!
:disconnect
echo -
echo #### Disconnecting..
scc.exe disconnect -p
echo #### Erasing login information
scc.exe erasecreds
echo #### Setting GUI (connect) mode
scc.exe setmode con
goto end
:scc_missing
echo #### SCC.EXE appears to be missing! Aborting..
goto end
:end
popd
echo #### Done!
pause

Open in new window


The form code is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace VPNTest
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
            ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();

            //specify the name and the arguements you want to pass to the command prompt
            psi.FileName = @"c:\Program Files\Nomad\VPN.bat";
            psi.Arguments = "'+ txtBxUser +'", "'+ txtBxPass +'";

            //if you don't want a console window popping up then set this property
            pi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;

            //Create new process and set the starting information
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo = psi;

            //Set this so that you can tell when the process has completed
            p.EnableRaisingEvents = true;
            p.Start();
            //wait until the process has completed
            while (!p.HasExited)
            {
                System.Threading.Thread.Sleep(1000);
            }
            //check to see what the exit code was
            if (p.ExitCode != 0)
            {
                //some error occurred
            }
        }
    }
}

Open in new window

0
Comment
Question by:r3nder
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 65

Expert Comment

by:rockiroads
ID: 33708530
In your Process object p there is a property within StartInfo called Arguments, see if you can make use of that

p.StartInfo.Arguments=fred
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 33708536
I think multiple arguments are delimited by a space so build a string with your username and password delimited by a space

in dos I think its %1 for first argument, %2 for second argument
0
 
LVL 6

Author Comment

by:r3nder
ID: 33709070
I tried those suggestions but that didnt work - I have been searching the net for an example but nothing yet
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 65

Expert Comment

by:rockiroads
ID: 33709189
maybe you need to run the cmd.exe and use your batch script as args

c:\\windows\\system32\\cmd.exe  should be the program to run

arguments are  /c  batchscriptname  username  password

/c means run command and exit
u can use /k to test, this runs and leaves command window open - useful for determing whats happening
0
 
LVL 65

Accepted Solution

by:
rockiroads earned 500 total points
ID: 33709199
ok, just gave it a test and is working fine this way
this is my test code

            string userName = "fred";
            String password = "flintstone";
            string args = "/k c:\\temp\\csharp.bat " + userName + " " + password;
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.Arguments = args;
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.FileName = "c:\\windows\\system32\\cmd.exe";
            p.Start();

remember to change /k to /c
0
 
LVL 6

Author Comment

by:r3nder
ID: 33709271
That did it - thanks man - I have never had to pass parameters to a batch file - I appreciate your knowledge :)
0
 
LVL 6

Author Closing Comment

by:r3nder
ID: 33709274
Excellent to have had the privilege to learn from you
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 33709321
No probs and thanks. It didn't click initially that it was a batch file. Only when I started thinking about the test that it dawned one me lol.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
"lblTime is not declared" 3 65
pVarablies are clearing but do not think i am reloading page 1 20
VLC command 34 80
How does this modal gets closed? 6 42
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question