VBScript Mapped Drive

I have created a script that maps a drive to a file share by using the next available drive.  That part works fine.

For the second part of the script, I would like to run an executable at the location of the mapped drive.

This is my script so far, which works to perform the mapping:

strUNC = "\\castsrv02\WinINSTALL\Packages\SOL\PMTest"

Set objNetwork = CreateObject("WScript.Network")
objNetwork.MapNetworkDrive AvailableDriveLetter, strUNC


'-------------------------------------------------
Function AvailableDriveLetter
   With CreateObject("Scripting.FileSystemObject")
      For i = Asc("D") To Asc("Z")
         If Not .DriveExists(Chr(i)) Then
            AvailableDriveLetter = Chr(i) & ":"
            Exit For
         End If
      Next
   End With
End Function
ResoneticsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

akalyan911Technical ConsultantCommented:
Hi Resonetics,

For Mapping share drive you script looks good, for mapping the drive letter please use the below info.

A <useletter> attribute value equaling false (zero [0]) determines the desired configuration is to replace a mapped drive using the first available letter starting with the <letter> attribute value.

Find the below link suggested by Microsoft article for the same..
http://technet.microsoft.com/en-us/library/cc938537.aspx
&
http://www.computerperformance.co.uk/Logon/logon_mapnetworkdrive_first.htm
0
Steven CarnahanNetwork ManagerCommented:
It sounds like you are wanting to concatenate the drive letter with the program

strCommandtoRun = AvailableDriveLetter & " " & "<program>"

And then execute the command to run the program.

objShell.run strCommandtoRun
0
ResoneticsAuthor Commented:
@pony10us...I believe that is exactly what I am trying to do.  This is what I have so far, and I am getting an error stating:  "The system cannot fine the drive specified.".

I also declared objShell.

strUNC = "\\castsrv02\WinINSTALL\Packages\SOL\PMTest"

Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )

Set objNetwork = CreateObject("WScript.Network")
objNetwork.MapNetworkDrive AvailableDriveLetter, strUNC

'-------------------------------------------------
Function AvailableDriveLetter
   With CreateObject("Scripting.FileSystemObject")
      For i = Asc("D") To Asc("Z")
         If Not .DriveExists(Chr(i)) Then
            AvailableDriveLetter = Chr(i) & ":"
            Exit For
         End If
      Next
   End With
End Function

strCommandtoRun = AvailableDriveLetter & "\" & "SilentInstaller.exe"

objShell.run strCommandtoRun
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

Steven CarnahanNetwork ManagerCommented:
add these lines before the strCommandtoRun= line to see if we are on the right track:

WScript.Echo "Drive should be:" & AvailableDriveLetter
WScript.Sleep 2000
0
ResoneticsAuthor Commented:
OK.  Added the two lines, and receive "(null): The system cannot find the drive specified"

It also echos "Drive Should be:F:

I ram the script several times, and If I go check my mapped network drives, I am always one letter behind the echo "Drive Should be:"
0
Steven CarnahanNetwork ManagerCommented:
Okay, that is because it is running the function a second time.

I have been in meetings and have to return some calls but will get back to this shortly unless someone else is able to jump in here.

:)
0
Steven CarnahanNetwork ManagerCommented:
Try this:

strUNC = "\\castsrv02\WinINSTALL\Packages\SOL\PMTest" 

 Dim objShell
 Set objShell = WScript.CreateObject( "WScript.Shell" )

 Set objNetwork = CreateObject("WScript.Network") 

 objNetwork.MapNetworkDrive AvailableDriveLetter, strUNC

 '-------------------------------------------------
 Function AvailableDriveLetter
    With CreateObject("Scripting.FileSystemObject")
       For i = Asc("D") To Asc("Z") 
          If Not .DriveExists(Chr(i)) Then 
             AvailableDriveLetter = Chr(i) & ":" 
             Exit For 
          End If 
       Next
    End With
DLetter = Chr(i)
 End Function

WScript.Echo "Drive should be:" & DLetter
 WScript.Sleep 2000 

Open in new window

0
ResoneticsAuthor Commented:
OK....so it appears to be mapping the drive, but the command window says "Drive Should be" and it's blank.

My colleague and I are looking at it also since he is more of a scriptor than I am.
0
Steven CarnahanNetwork ManagerCommented:
Okay, I think I got it.  Try this one:

strUNC = "\\castsrv02\WinINSTALL\Packages\SOL\PMTest" 
 Dim DLetter
 Dim objShell
 Set objShell = WScript.CreateObject( "WScript.Shell" )

 Set objNetwork = CreateObject("WScript.Network") 

 objNetwork.MapNetworkDrive AvailableDriveLetter, strUNC

 '-------------------------------------------------
 Function AvailableDriveLetter
    With CreateObject("Scripting.FileSystemObject")
       For i = Asc("D") To Asc("Z") 
          If Not .DriveExists(Chr(i)) Then 
             AvailableDriveLetter = Chr(i) & ":" 
             Exit For 
          End If 
       Next
    End With
DLetter = AvailableDriveLetter
 End Function

WScript.Echo "Drive should be:" & " " & DLetter
 WScript.Sleep 2000 

Open in new window


If you are satisfied then you can add back this:

strCommandtoRun = DLetter & "\" & "SilentInstaller.exe"
 objShell.run strCommandtoRun

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ResoneticsAuthor Commented:
I added back the two lines above, but the exe doesn't run.

The echo back reads:  "Drive should be \SilentInstaller.exe"

It appers to be missing the letter.  I would think it should echo back "Drive letter should be Q:\SilentInstaller.exe"
0
ResoneticsAuthor Commented:
This is my latest attempt.  It appears the for statement is running twice because the drive that is actually mapped is one letter ahead of what it should be:

strUNC = "\\castsrv02\WinINSTALL\Packages\SOL\PMTest"

 Dim objShell
 Set objShell = WScript.CreateObject( "WScript.Shell" )

 Set objNetwork = CreateObject("WScript.Network")

 objNetwork.MapNetworkDrive AvailableDriveLetter, strUNC

 '-------------------------------------------------
 Function AvailableDriveLetter
    With CreateObject("Scripting.FileSystemObject")
       For i = Asc("D") To Asc("Z")
          If Not .DriveExists(Chr(i)) Then
             AvailableDriveLetter = Chr(i) & ":"
             Exit For
          End If
       Next
    End With
 End Function

DLetter = AvailableDriveLetter
WScript.Echo "Drive should be:" & "" & DLetter
 WScript.Sleep 2000

strCommandtoRun = DLetter & "\" & "SilentInstaller.exe"
objShell.run strCommandtoRun
0
Steven CarnahanNetwork ManagerCommented:
Did you try the last code I posted?  

What you have is from the first attempt which does cause the function to run twice resulting in the drive being incorrect.
0
ResoneticsAuthor Commented:
My mistake.  It appears to work as it should now.  

Thanks for all of your assistance.
0
ResoneticsAuthor Commented:
Thanks for a working solution to a strange requirement we have.
0
Steven CarnahanNetwork ManagerCommented:
Glad I could be of assistance.  :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Applications

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.