Link to home
Start Free TrialLog in
Avatar of jmohan0302
jmohan0302

asked on

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
Avatar of Bill Prew
Bill Prew

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
Avatar of jmohan0302

ASKER

Hi Could you please explain me the script as I need to know do we need to give an input file. Thanks
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
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
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
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
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
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
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
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
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
Hi Thanks. But I am getting the following error while executing:

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

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
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
Can you rename it to a TXT file and upload it as an attachment here please.

~bp
Hi I have enclosed the file:



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

)>>"%LogFile%"

~bp
Still getting the same error.
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


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.
Hi I am checking this in Windows7
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
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
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
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.
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
==> Yes I need to disconnect the shared drives and has to remap them again

What purpose does this serve?

~bp
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
Hi In the new script it is disconnecting the drives but not remapping
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
At a command line, does the following work for you without error?

wmic netuse get localname,remotepath /format:list

Open in new window

~bp
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Good one