Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Batch File error handling using Net Use  (Read 23616 times)

0 Members and 1 Guest are viewing this topic.

getintanked

  • Guest
Batch File error handling using Net Use
« on: August 17, 2007, 05:25:36 PM »
I am creating a batch file that allows users to re-map their Home network share drive after a vpn connection has been made. Since the mapping is based on their profile, this won't push unless they logon while connected to our network.

We have many offsite locations so, I made it so they have to choose their location and it will map based on their local domain controller.

However, when using NET USE there are a few instances when errors can occur (always user error...)

For example. If they haven't vpn'd in yet, its going to throw an error stating that the network path cannot be found - I want to catch this error and replace it with something like "make sure you have a live internet connection and you can reach the intranet home page first, and try again"
Same goes for if they are allready mapped and they are running the batch file again, it will say something about the drive letter is already in use... and i would like to replace that error with "Your drive is already mapped, open your my computer and access your H:/ Drive"

Code: [Select]
title ~ AVI Information Technology ~ Map My Home Drive

@ECHO OFF
CLS
ECHO ------------------------------
ECHO Name of the Company
ECHO ------------------------------
ECHO Please select your home office location...

:LOOP
ECHO A. Altamonte
ECHO B. Atlanta
ECHO C. Cleveland
ECHO D. Columbia
ECHO E. Cypress
ECHO F. Dallas
ECHO G. Denver
ECHO H. Detroit
ECHO I. Ft. Lauderdale
ECHO J. Jacksonville
ECHO K. Lanham
ECHO L. Las Vegas
ECHO M. Nashville
ECHO N. New York
ECHO O. Orlando
ECHO P. Pittsburgh
ECHO Q. Salt Lake City
ECHO R. Tampa
ECHO Z. Quit

SET Choice=
SET /P Choice=Type the letter and press Enter:
IF NOT '%Choice%'=='' SET Choice=%Choice:~0,1%
ECHO.
:: /I makes the IF comparison case-insensitive
IF /I '%Choice%'=='A' GOTO ALT
IF /I '%Choice%'=='B' GOTO ATL
IF /I '%Choice%'=='C' GOTO CLE
IF /I '%Choice%'=='D' GOTO COL
IF /I '%Choice%'=='E' GOTO CYP
IF /I '%Choice%'=='F' GOTO DAL
IF /I '%Choice%'=='G' GOTO DEN
IF /I '%Choice%'=='H' GOTO DET
IF /I '%Choice%'=='I' GOTO FTL
IF /I '%Choice%'=='J' GOTO JAX
IF /I '%Choice%'=='K' GOTO LAN
IF /I '%Choice%'=='L' GOTO LSV
IF /I '%Choice%'=='M' GOTO NSV
IF /I '%Choice%'=='N' GOTO NYC
IF /I '%Choice%'=='O' GOTO ORL
IF /I '%Choice%'=='P' GOTO PIT
IF /I '%Choice%'=='Q' GOTO SLC
IF /I '%Choice%'=='R' GOTO TPA
IF /I '%Choice%'=='Z' GOTO End
ECHO "%Choice%" is not valid. Please try again.
ECHO.
GOTO Loop
:ALT
net use h: \\ALTDC1\users\%USERNAME% /persistent:yes
GOTO Again
:ATL
net use h: \\atldc1\users\%USERNAME% /persistent:yes
GOTO Again
:CLE
net use h: \\CLEdc1\users\%USERNAME% /persistent:yes
GOTO Again
:COL
net use h: \\COLdc1\users\%USERNAME% /persistent:yes
GOTO Again
:CYP
net use h: \\CYPdc1\users\%USERNAME% /persistent:yes
GOTO Again
:DAL
net use h: \\DALdc1\users\%USERNAME% /persistent:yes
GOTO Again
:DEN
net use h: \\DENdc1\users\%USERNAME% /persistent:yes
GOTO Again
:DET
net use h: \\DETdc1\users\%USERNAME% /persistent:yes
GOTO Again
:FTL
net use h: \\FTLdc1\users\%USERNAME% /persistent:yes
GOTO Again
:JAX
net use h: \\JAXdc1\users\%USERNAME% /persistent:yes
GOTO Again
:LAN
net use h: \\LANdc1\users\%USERNAME% /persistent:yes
GOTO Again
:LSV
net use h: \\LSVdc1\users\%USERNAME% /persistent:yes
GOTO Again
:NSV
net use h: \\NSVdc1\users\%USERNAME% /persistent:yes
GOTO Again
:NYC
net use h: \\NYCdc1\users\%USERNAME% /persistent:yes
GOTO Again
:ORL
net use h: \\ORLdc1\users\%USERNAME% /persistent:yes
GOTO Again
:PIT
net use h: \\PITdc1\users\%USERNAME% /persistent:yes
GOTO Again
:SLC
net use h: \\SLCdc1\users\%USERNAME% /persistent:yes
GOTO Again
:TPA
net use h: \\sid\TPA-Users\%USERNAME% /persistent:yes
GOTO Again
:Again
PAUSE
CLS
:End

I've been searching and searching, and can't find any error handling within batch files...
is there?

Thanks in advance

DeltaSlaya



    Apprentice
  • Google
    Re: Batch File error handling using Net Use
    « Reply #1 on: August 17, 2007, 05:31:06 PM »
    Yes there is, I'm not use about the 'net' command but try immediately on the line after a net send operation enter "echo %errorlevel% & pause >nul".

    Intentionally cause all the errors you can think of and see what errorlevel codes, if any are created. '0' generally means it worked.
    System specs:
    Intel Core 2 Duo E6600 (up to 3.3 stock V and air)
    ASUS Striker Extreme
    XFX 8600GT XXX Edition
    2x 1gB Corsair XMS2 DDR2-800
    Seagate Barracuda 320gB SATA
    Raidmax Ninja 918 (520W ATXV2.0 PSU)
    -

    getintanked

    • Guest
    Re: Batch File error handling using Net Use
    « Reply #2 on: August 17, 2007, 05:51:38 PM »
    DeltaSlaya - u da man. :)

    Problem tho.

    I was able to duplicate many errors as when i vpn in from home my username isn't right - so i'm able to re-create lots of stuff that i normally wouldn't. So this is cool.

    However, I was getting error code number '2' for all three of my major errors:
    "incorrect user name and or password"
    "network path not found"
    "local device name is already in use"

    They all return number 2...

    Am i stuck?

    Maybe if... is there a way to do a test to see if drive h is being used? and/or to test connectivity to the domain controller?


    DeltaSlaya



      Apprentice
    • Google
      Re: Batch File error handling using Net Use
      « Reply #3 on: August 17, 2007, 06:04:30 PM »
      Well you could see if the H: drive exists.

      "if not exist "H:\" echo Not exist H: & pause >nul"

      For testing the connectivity you could try pinging it. Though I don't think there are errorlevel codes for ping. If you supply more information regarding if thats possible I'll write some code to do it.
      System specs:
      Intel Core 2 Duo E6600 (up to 3.3 stock V and air)
      ASUS Striker Extreme
      XFX 8600GT XXX Edition
      2x 1gB Corsair XMS2 DDR2-800
      Seagate Barracuda 320gB SATA
      Raidmax Ninja 918 (520W ATXV2.0 PSU)
      -

      getintanked

      • Guest
      Re: Batch File error handling using Net Use
      « Reply #4 on: August 17, 2007, 06:15:46 PM »
      I dont get how that works?

      Code: [Select]
      if not exist "H:\" echo Not exist H: & pause >nul


      If i could atelast get rid of one type of error, that would be cool. :)

      DeltaSlaya



        Apprentice
      • Google
        Re: Batch File error handling using Net Use
        « Reply #5 on: August 17, 2007, 06:19:16 PM »
        Well if there is no H: drive on the person's computer it echos "Not exist H:".
        System specs:
        Intel Core 2 Duo E6600 (up to 3.3 stock V and air)
        ASUS Striker Extreme
        XFX 8600GT XXX Edition
        2x 1gB Corsair XMS2 DDR2-800
        Seagate Barracuda 320gB SATA
        Raidmax Ninja 918 (520W ATXV2.0 PSU)
        -

        getintanked

        • Guest
        Re: Batch File error handling using Net Use
        « Reply #6 on: August 17, 2007, 06:24:44 PM »
        dude. my bad.  ::)
        You have helped out alot! Thanks man.

        Enjoy ur weekend.

        DeltaSlaya



          Apprentice
        • Google
          Re: Batch File error handling using Net Use
          « Reply #7 on: August 17, 2007, 06:25:44 PM »
          No problem.
          System specs:
          Intel Core 2 Duo E6600 (up to 3.3 stock V and air)
          ASUS Striker Extreme
          XFX 8600GT XXX Edition
          2x 1gB Corsair XMS2 DDR2-800
          Seagate Barracuda 320gB SATA
          Raidmax Ninja 918 (520W ATXV2.0 PSU)
          -

          getintanked

          • Guest
          Re: Batch File error handling using Net Use
          « Reply #8 on: August 17, 2007, 07:00:42 PM »
          One thing I should add.. which is what threw me off.

          If the drive you're mapping is an empty cd rom drive - it will test out as "not exist" when infact it does.

          just fyi.




          DeltaSlaya



            Apprentice
          • Google
            Re: Batch File error handling using Net Use
            « Reply #9 on: August 17, 2007, 07:16:00 PM »
            Well that is logical. If there is no disc in the drive it means there is no storage, and if there is no storage it means nothing can be read or written to. Therefore there is no drive, it is essentially a placeholder until it is able to access data.
            System specs:
            Intel Core 2 Duo E6600 (up to 3.3 stock V and air)
            ASUS Striker Extreme
            XFX 8600GT XXX Edition
            2x 1gB Corsair XMS2 DDR2-800
            Seagate Barracuda 320gB SATA
            Raidmax Ninja 918 (520W ATXV2.0 PSU)
            -