[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 178
  • Last Modified:

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
0
Resonetics
Asked:
Resonetics
  • 7
  • 7
1 Solution
 
akalyan911Commented:
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
 
pony10usCommented:
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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
pony10usCommented:
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
 
pony10usCommented:
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
 
pony10usCommented:
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
 
pony10usCommented:
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
 
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
 
pony10usCommented:
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
 
pony10usCommented:
Glad I could be of assistance.  :)
0

Featured Post

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

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