• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 367
  • Last Modified:

Need a script to provide an output of command to input of a command

I am looking for a script or a batch file:
The real requirement is that I will run a command ,net use * /d > out.txt. This will result me prompting to say Y /N along with list of mapped drives and their respective paths. I need to use the content of the out.txt(which will contain the mapped drives and their respective paths) to map the same shares again. Any help or ideas are highly appreciated. Thanks
0
jmohan0302
Asked:
jmohan0302
  • 17
  • 16
1 Solution
 
Bill PrewCommented:
Give this a try, it will iterate through each share, disconnect it, and then reconnect it.  It also displays the shares as it goes, you may want to delete that once it works well.

@echo off
for /F "skip=2 tokens=2,3 delims=," %%A in ('wmic netuse get remotepath^,localname^,status /format:csv') do (
  net use %%A /DELETE /Y
  net use %%A "%%~B" /PERSISTENT:YES
  echo Remapped drive [%%A] to share [%%~B]
)

Open in new window

~bp
0
 
jmohan0302Author Commented:
Hi Could you please explain me the script as I need to know do we need to give an input file. Thanks
0
 
Bill PrewCommented:
No input file.  The FOR loop processes the output of the WMIC command, which will list all current mapped drives (same basic info as NET USE but in a format that easier to work with in the script).  Inside the FOR loop, the loop variable %%A will be assigned to the drive letter, and %%B will be assigned to the sharename that drive was connected to.  We then delete the current share, and remap it.

~bp
0
Industry Leaders: 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!

 
jmohan0302Author Commented:
Hi ,
I saved it in a .bat file and when I tried to run I am getting the following error:

C:\Users\jayabalm\Desktop>share.bat
Invalid XSL format (or) file name.

C:\Users\jayabalm\Desktop>

The share.bat contains your code. Kindly help. Thanks
0
 
jmohan0302Author Commented:
Hi I found that the code in the notepad is enabled for Wordwrap, that's y I am getting the error. Now I removed that and its working fine. But I could not really see what's happening. Is there any way to create a log file of what it is doing? or what are all map drives that is disconnecting and re-mapping? Thanks
0
 
Bill PrewCommented:
Okay, this should fix that.

@echo off
for /F "skip=2 tokens=2,3 delims=," %%A in ('wmic netuse get remotepath^,localname^,status /format:csv.xsl') do (
  net use %%A /DELETE /Y
  net use %%A "%%~B" /PERSISTENT:YES
  echo Remapped drive [%%A] to share [%%~B]
)

Open in new window

~bp
0
 
Bill PrewCommented:
Okay, in that case stick with the original working file.

It does log all the reconnects done, try adding a PAUSE line to the end if you aren't seeing those before the window closes.

If you want to log them then you could adjust the ECHO line to:


  echo Remapped drive [%%A] to share [%%~B] >>"c:\dir\logfile.txt"

~bp
0
 
jmohan0302Author Commented:
Hi I tried, I am not getting the log file created. Could you adjust the code and give it to me so that I can understand wer I am  going wrong. Thanks
0
 
Bill PrewCommented:
Actually, it looks like you named the BAT file SHARE.BAT.  That's a bad choice since there is a windows built in program SHARE.EXE that will execute instead.  Rename the BAT to something that doesn't already exist, like maybe REMAP.BAT.

~bp
0
 
jmohan0302Author Commented:
Ok. Its working but I could not get the log files. So I am not knowing what is happening? Help me to get the log file. THanks
0
 
Bill PrewCommented:
Okay, here's a version that should log to a file, set the name of the file for logging near the top as you need.  I also added a few comments to help further explain the script, hope this helps.

@echo off

REM Define filename to log activity to
set LogFile=c:\ee\ee27379643.txt

REM Delete logfile if it exists
if exist "%LogFile%" del "%LogFile%"

REM Loop through all current defined mapped drives
REM Inside loop %%A will be the drive letter, %%B will be the sharename it is mapped to
for /F "skip=2 tokens=2,3 delims=," %%A in ('wmic netuse get remotepath^,localname^,status /format:csv') do (

  REM Log a message about remapping this share
  echo ***** Remapping drive [%%A] to share [%%~B] *****

  REM First, delete the existing mapping for this drive
  net use %%A /DELETE /Y

  REM Then remap using the same drive and sharename
  net use %%A "%%~B" /PERSISTENT:YES

)>>"%LogFile%"

Open in new window

~bp
0
 
jmohan0302Author Commented:
Hi Thanks. But I am getting the following error while executing:

C:\Users\jayabalm\Desktop>map1.bat
Invalid XSL format (or) file name.

0
 
Bill PrewCommented:
Could that be the same problem you mentioned earlier:

"Hi I found that the code in the notepad is enabled for Wordwrap, that's y I am getting the error. Now I removed that and its working fine."


If not then try changing the

/format:csv

to

/format:csv.xsl

~bp
0
 
jmohan0302Author Commented:
Hi I checked for the Wordwrap and that is not enabled and I tried changing the /format:csv to /format:csv.xsl but still getting the same error:

The code in the map1.bat is :

@echo off

REM Define filename to log activity to
set LogFile=c:\ee\ee27379643.txt

REM Delete logfile if it exists
if exist "%LogFile%" del "%LogFile%"

REM Loop through all current defined mapped drives
for /F "skip=2 tokens=2,3 delims=," %%A in ('wmic netuse get remotepath^,localname^,status /format:csv.xsl') do (
REM Log a message about remapping this share
  echo ***** Remapping drive [%%A] to share [%%~B] *****

  REM First, delete the existing mapping for this drive
  net use %%A /DELETE /Y

  REM Then remap using the same drive and sharename
  net use %%A "%%~B" /PERSISTENT:YES

)>>"%LogFile%

Please check did I have missed some thing while pasting. I really appreciate your help and inputs. Thanks
0
 
Bill PrewCommented:
Can you rename it to a TXT file and upload it as an attachment here please.

~bp
0
 
jmohan0302Author Commented:
Hi I have enclosed the file:



Map.txt
0
 
Bill PrewCommented:
It looks like you are missing a double quote off the end of the last line.

)>>"%LogFile%"

~bp
0
 
jmohan0302Author Commented:
Still getting the same error.
0
 
Bill PrewCommented:
From a DOS window command prompt, can you try each of these commands and let me know what they say:

wmic netuse get remotepath,localname,status /format:csv

Open in new window

wmic netuse get remotepath,localname,status /format:csv.xsl

Open in new window

~bp

0
 
jmohan0302Author Commented:

C:\> wmic netuse get remotepath,localname,status /format:csv
Invalid XSL format (or) file name.

C:\> wmic netuse get remotepath,localname,status /format:csv.xsl
Invalid XSL format (or) file name.
0
 
jmohan0302Author Commented:
Hi I am checking this in Windows7
0
 
jmohan0302Author Commented:
Hi Bill,

I tried it on a WIN2k3 the command is working fine. I think the switch /Format.CSV is not supporting in WINdows 7. Is there any way to get rid of this error in Windows7
0
 
Bill PrewCommented:
Yes, there were some issues with the format:csv in Win7, but typically the additon of the .XSL extension resolves that.  Oddly in your case it doesn't.

Okay, we'll need to try and work around that.  If you just do the following can you paste up the output please.

wmic netuse get remotepath,localname

~bp
0
 
Bill PrewCommented:
At the risk of backing up a bit, what are you trying to accomplish by remapping the existing mapped drives to the same share?

~bp
0
 
jmohan0302Author Commented:
Yes I need to disconnect the shared drives and has to remap them again

But in Windows7 If I delete the /Fomat switch it is working in command line.

But when I add the same line the batch file is executing but the log file is not created. However the same is working fine WINXP and WIN2k3. Its really strange.
0
 
Bill PrewCommented:
Okay, I think there may be some different issues with this approach, but let's give it a try.

@echo off

REM Define filename to log activity to
set LogFile=c:\ee\ee27379643.txt

REM Delete logfile if it exists
if exist "%LogFile%" del "%LogFile%"

REM Loop through all current defined mapped drives
REM Inside loop %%A will be the drive letter, %%B will be the sharename it is mapped to
for /F "skip=1 tokens=1*" %%A in ('wmic netuse get remotepath^,localname') do (

  REM Log a message about remapping this share
  echo ***** Remapping drive [%%A] to share [%%~B] *****

  REM First, delete the existing mapping for this drive
  net use %%A /DELETE /Y

  REM Then remap using the same drive and sharename
  net use %%A "%%~B" /PERSISTENT:YES

)>>"%LogFile%"

Open in new window

~bp
0
 
Bill PrewCommented:
==> Yes I need to disconnect the shared drives and has to remap them again

What purpose does this serve?

~bp
0
 
jmohan0302Author Commented:
I am writting a script. The real problem is AD account locking out. We are getting frequent account lockout issues. We found that the accounts were getting locked out for a lot of reasons out of which is Mapping of shared drives and we found many reasons for that. So I am writing a script which will resolves the lockout issues for all the reasons.I got strucked up in the middle for remapping the same drives what user has mapped earlier. You really helped me in doing that.

-MJ
0
 
jmohan0302Author Commented:
Hi In the new script it is disconnecting the drives but not remapping
0
 
Bill PrewCommented:
Yeah, the problem with WMIC without the CSV is that it has a few funny characters in it's output that confuse BAT scripts.

Looking for a work around...

~bp
0
 
Bill PrewCommented:
At a command line, does the following work for you without error?

wmic netuse get localname,remotepath /format:list

Open in new window

~bp
0
 
Bill PrewCommented:
Also, as one other test, can you place this file in the folder that is the current directory when you start a DOS command line session, and then try both of these commands again:

wmic netuse get remotepath,localname,status /format:csv

Open in new window

wmic netuse get remotepath,localname,status /format:csv.xsl

Open in new window


 csv.xsl
0
 
jmohan0302Author Commented:
Good one
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 17
  • 16
Tackle projects and never again get stuck behind a technical roadblock.
Join Now