Solved

EnumNetworkDrives Method in batch script

Posted on 2009-05-18
7
327 Views
Last Modified: 2012-05-07
hey,

is there any way to check above in a batch file ?
my idea was to play with net use, but how can i capture specific drive UNC ?

one last thing after i got the unc how can i play with the parts ?
what i mean for example :

\\serverA\public

how to "pull" serverA and lets say ping it ?
or check if the folder name it's public and if not remap another folder ?

if i asking to much in one question just say :-)

thanks
0
Comment
Question by:yehudaha
  • 4
  • 3
7 Comments
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
If you have a UNC, you can treat it like a file name, hence
for %U in (\\serverA\public) do @echo %~nU
will echo the share name only (in a batch file, you need to use %%U and %%~nU).

To process the net use output, for /F is useful:
for /F "tokens=2-3 delims=\ "  %S in ('net use ^| find "\"') do @echo Server: %S    Share:  %T

Now, the last does not make sense. It is not necessary to check if a share already is used on a specific server, but others do. If you need the share, just map it with net use. If it exists already, the command will silently do nothing.

0
 
LVL 14

Author Comment

by:yehudaha
Comment Utility
thanks Qlemo for your respone

the output i'm getting is the drive letter and the host name.

my direction in this script is to pull the host name, ping it, pull the subnet of the host in the share
and check if it's match the local subnet if not remove the non matching share.

we have branches with local file servers, but the problem that the users is traveling between the branches
and getting the wrong drive mapping from there original branches, using it and causing big slow down.
0
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
Comment Utility
That makes sense. But do your folks need to use persistent network drives? It would make things easier if drive mappings wouldn't survive a reboot.

Having said that, let's make a first attempt.
It's not easy to decide the subnet thing - how should that work? Should it be a decision based
  1. on IP address  -  dangerous if you use subnet masks differing from Classes A,B,C, or different subnet masks in the branches,
  2. or DNS names - difficult, as the full qualified name might not appear in net use, and a DNS query will not help.
The following snippet is based on
  • each server is reachable via ping
  • we are using Class C only
It does not matter if a drive mapping or only UNC is used. The net use /d is only echoed, for test reasons. You can remove the echo (word only) to execute it.

@echo off

setlocal EnableDelayedExpansion
 

REM Assuming Class C, 192.168.x.y => net 192.168.x

for /F "tokens=2 delims=[]" %%C in ('ping -n 1 %computername% ^| find "["') do set net=%%~nC
 

for /F "tokens=2-4 delims=\ " %%S in ('net use ^| find "\"') do (

  set srv=%%S

  if "!srv:~1,1!" == ":" (

    set srv=%%T

    set share=%%S  

  ) else (

    set share=\\%%S\%%T

  )

  for /F "tokens=2 delims=[]" %%C in ('ping -n 1 !srv! ^| find "["') do (

    if not %net% == %%~nC echo net use !share! /d

  )

)

Open in new window

0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 14

Author Comment

by:yehudaha
Comment Utility
mm nice one

batch scripting very intresting,
do you have a suggestion how can i learn batch ?
even better how did you learn ? :-)
0
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
I'm a dinosaur, we had nothing else in the early days before the World War ;-)
Seriously, I don't know of a good tutorial. It might exist, however.

A good start is to read the help comming from
if /?
set /?
for /?
cmd /?
help

All stuff used in above script can be derived out of those help pages.

0
 
LVL 14

Author Closing Comment

by:yehudaha
Comment Utility
Thanks For The Help !
0
 
LVL 14

Author Comment

by:yehudaha
Comment Utility
>> I'm a dinosaur, we had nothing else in the early days before the World War

:-)
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
disabled AD accounts info 3 59
how to run a batch process 16 38
Internet Connection Util 8 68
regedit.exe REG ADD problems 2 13
If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now