Announcement

Collapse
No announcement yet.

mapdrive using Inputbox for a pc name

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • mapdrive using Inputbox for a pc name

    Hi, this is my first post here, long time reader of the site for valuable information though

    I have never done any series scripts, bit of moving files etc, but nothing major

    I need to write a vbscript that wil be avaiable to all my laptop users and they need to be able to map a drive to another laptop while out of the office onto another laptop, part of an application we are running, use a small switch for the network and that works, and doing a manual drive mapping work, since the users will not be out with the same people all the time they need to be able to map a drive and i would like them to use a script, inputbox would pop up, add the laptop name and the script would map the drive

    this is what I have got, but cannot get it to work, it would also be good to use an inputbox for the password as well

    any advice really appreciated on this

    thanks

    Option Explicit
    Dim objNetwork
    Dim strDriveLetter, strRemotePath, strUser, strPassword, strPCName
    ' Values of variables set
    strPCName=InputBox ("Enter Master PC Name")
    strDriveLetter = "V:"
    strPCName="itaoh"
    strRemotePath = "\\strPCName\proacc"
    strUser = "name"
    strPassword = "password"
    'strProfile = "false"
    ' This section creates a network object. (objNetwork)
    ' Then apply MapNetworkDrive method. Result H: drive
    ' Note, this script features 5 arguments on lines 21/22.
    Set objNetwork = WScript.CreateObject("WScript.Network")
    objNetwork.MapNetworkDrive strDriveLetter, strRemotePath, _
    strUser, strPassword, strPCName
    ' Extra code just to add a message box
    WScript.Echo " Launch Explorer, check: "& strDriveLetter
    WScript.Quit

  • #2
    Re: mapdrive using Inputbox for a pc name

    script posted was wrong

    should be

    Code:
    Option Explicit
    Dim objNetwork 
    Dim strDriveLetter, strRemotePath, strUser, strPassword, strPCName
    ' Values of variables set
    strPCName=InputBox ("Enter Master PC Name")
    strDriveLetter = "V:" 
    strRemotePath = \\strPCName\proacc" 
    strUser = "name"
    strPassword = "password"
    'strProfile = "false"
    ' This section creates a network object. (objNetwork)
    ' Then apply MapNetworkDrive method. Result H: drive
    ' Note, this script features 5 arguments on lines 21/22.
    Set objNetwork = WScript.CreateObject("WScript.Network") 
    objNetwork.MapNetworkDrive strDriveLetter, strRemotePath, _
    strUser, strPassword, strPCName
    ' Extra code just to add a message box
    WScript.Echo " Launch Explorer, check: "& strDriveLetter
    WScript.Quit
    - Moderator Edit: Added [code] tags around the code -
    Last edited by Rems; 30th January 2009, 12:01.

    Comment


    • #3
      Re: mapdrive using Inputbox for a pc name

      This is how your sample script will work
      Code:
      Option Explicit
      Dim objNetwork 
      Dim strDriveLetter, strRemotePath, strUser, strPassword, strPCName
      
      ' Values of variables set
      strPCName = InputBox ("Enter Master PC Name")
      
      strDriveLetter = "V:" 
      strRemotePath = "\\" & strPCName & "\proacc"
      ' A default for the MapNetworkDrive method is "persistent = false"
      
      strUser = "name"
      strPassword = "password"
      
      Set objNetwork = WScript.CreateObject("WScript.Network") 
      objNetwork.MapNetworkDrive strDriveLetter, strRemotePath, false, strUser, strPassword
      
      ' Extra code just to add a message box
      WScript.Echo " Launch Explorer, check: "& strDriveLetter
      
      WScript.Quit

      If you also like to enter a Username and Password, you have two options

      1. The script below restart it self with the Cscript host so it can use StdOut and StdIn,
      Code:
      If Instr(1, WScript.FullName, "wscript.exe", vbTextCompare) then
        boxTitle = "Enter the name of the computer to Map drive [V:] to"
        Set WshShell = WScript.CreateObject("WScript.Shell")
        WshShell.run "%comspec% /C @color 6A &Title " & boxTitle _
          & "&%windir%\system32\Cscript.exe //NoLogo """ _
          & WScript.ScriptFullName & """",,FALSE
        Wscript.quit
      End If
      
      WScript.StdOut.Write "Enter Master PC Name: "
      strPCName = WScript.StdIn.ReadLine
      Wscript.Echo
      With CreateObject("ScriptPW.Password")
        WScript.StdOut.Write "Please enter your password: "
        strPassword = .GetPassword()
      End With
      
      
      ' Extra code just to show what was entered
      Wscript.Echo : Wscript.Echo
      Wscript.Echo "Computername is :", """" & strPCName & """", _
           vbNewLine & "Your Password is:", strPassword
      Wscript.Echo : Wscript.Echo
      
      WScript.StdOut.Write "Press Enter to quit" : Wscript.Echo
      WScript.StdIn.ReadLine
      2. You can also write an inputbox in HTML, then adjust the code so it can be used in a vbscript.

      more about the two options:
      http://www.microsoft.com/technet/scr...5/hey0204.mspx

      3. The alternative is to use a hta.
      http://forums.petri.com/showthread.p...8043#post68043


      \Rems
      Last edited by Rems; 6th February 2009, 10:55.

      This posting is provided "AS IS" with no warranties, and confers no rights.

      __________________

      ** Remember to give credit where credit's due **
      and leave Reputation Points for meaningful posts

      Comment


      • #4
        Re: mapdrive using Inputbox for a pc name

        thank you for this, works a treat and as a short term answer I have used an inputbox to request the password, you can see it as you type but I think that is better than hard coding it into the script

        should make life easier for users out on the road now to setup their small network

        once again thank you

        Comment


        • #5
          Re: mapdrive using Inputbox for a pc name

          Hi, everyone!!
          I'm trying to create script that allows to users open their home directory from any company computers. My sample based on Rems script:
          Code:
          Option Explicit
          Dim objNetwork 
          Dim strDriveLetter, strRemotePath, strUser, strPassword, strPCName, strRemoteShare
           
          ' Values of variables set
          strPCName = "London"
          strRemoteShare = InputBox ("Please enter your home share name with $ sign in the end:")
           
          strDriveLetter = "V:" 
          strRemotePath = "\\" & strPCName & "\" & strRemoteShare & ""
          ' A default for the MapNetworkDrive method is "persistent = false"
           
          strUser = InputBox ("Please enter your username:")
          strPassword = InputBox ("Please enter your password:")
           
           
           
          Set objNetwork = WScript.CreateObject("WScript.Network") 
          Err.Clear : objNetwork.RemoveNetworkDrive "P:", True, True 
          objNetwork.MapNetworkDrive strDriveLetter, strRemotePath, false, strUser, strPassword
           
          ' Extra code just to add a message box
          WScript.Echo " Launch Explorer, check: "& strDriveLetter
           
          WScript.Quit
          I need improve it a little...
          1. In our company users home directorys placed on root drive of the server (London) and associated with username, i.e. in aduc it looks like \\London\%username%$. How I can add $ (hidden share) parameter to strRemoteShare automatically without prompt user input it manually?
          2. Before mapping user home directory I need to disconnect already mapped drive (P) on same server. - already found solution (thx, Rems): Err.Clear : objNetwork.RemoveNetworkDrive "P:", True, True
          3. How I can mask user password when user typing it? I am trying to use vbs script example from scripting guy's forum:
          Code:
           
          Set objPassword = CreateObject("ScriptPW.Password")
          WScript.StdOut.Write "Please enter your password:" 
           
          strPassword = objPassword.GetPassword() 
          Wscript.Echo
          Wscript.Echo "Your password is: " & strPassword
          But it returns "The handle is invalid" error??
          Last edited by igor7; 6th February 2009, 03:08.

          Comment


          • #6
            Re: mapdrive using Inputbox for a pc name

            Originally posted by igor7 View Post
            How I can add $ (hidden share) parameter to strRemoteShare automatically without prompt user input it manually?
            2. Before mapping user home directory I need to disconnect already mapped drive (P) on same server. - already found solution (thx, Rems): Err.Clear : objNetwork.RemoveNetworkDrive "P:", True, True
            Code:
            '<...>
            
            Set objNetwork = WScript.CreateObject("WScript.Network") 
            strUserName = objNetwork.UserName 
            
            strRemoteShare = InputBox ("Please enter your home share name:", "Map drive [v:]", strUserName)
            strRemoteShare = strRemoteShare & "$"
            
            strPCName = "London"
            strRemotePath = "\\" & strPCName & "\" & strRemoteShare & ""
            strDriveLetter = "V:"
            
            On Error Resume Next
            objNetwork.MapNetworkDrive strDriveLetter, strRemotePath, false, strUser, strPassword
            If err.number <> vbEmpty then
              wscript.echo "unable to map drive"
              wscript.quit
            End If
            objNetwork.RemoveNetworkDrive "P:", True, True 
            On Error Goto 0
            
            '<...>


            Originally posted by igor7 View Post
            3. How I can mask user password when user typing it? I am trying to use vbs script example from scripting guy's forum
            [COLOR=#555a5f][FONT=Verdana]
            The vbs script example from scripting guy's forum must run with the cscript host!
            Thus start the script with the cscript host or, use the code from my example that will make the script restart itself with the cscript host.

            The three options to mask the password were already mentioned in this thread:
            http://forums.petri.com/showpost.php...40&postcount=3


            \Rems

            This posting is provided "AS IS" with no warranties, and confers no rights.

            __________________

            ** Remember to give credit where credit's due **
            and leave Reputation Points for meaningful posts

            Comment


            • #7
              Re: mapdrive using Inputbox for a pc name

              First of all I'm sorry for late replay, I was busy a little last month...
              The vbs script example from scripting guy's forum must run with the cscript host!
              Can you advise me and show what exact syntax I should use to make example from scripting guy's forum work?

              Rems,
              The code you posted is working only for current logged user, and my goal is a bit different. I give you a situation as an example when the users will use this script.
              As I said all users home directories placed on some server (London) and have hidden attribute ($). So, Mark and Jenny made the pictures from last the company event, for example. Mark downloaded the pictures to his home directory from the camera. Mark's office is in the 4th floor and Jenny is in the 2nd. Mark came to Jenny's office and she asked him to give/send her the pictures. So, Mark needed to go to his office, but instead of doing so he can map his home directory from Jenny's computer (when Jenny already logged in and for technical reason can't log off right now). Firs he disconnected Jenny's home drive and then connects his own by running following command:
              net use P: \\London\Mark$ /User:<MarkUsername> <MarkPassword>. But I'm trying to do this in more intelligent way. I want to create universal script that match to all company users. All what users need to do for mapping their home directories is provide a username and password into the text boxes and script should disconnect already existing home drive that belongs to the current logged user and map the new drive automatically based on the provided username/and password information.
              In a situation I described above Mark can connect to his home folder by running such as script, copy pictures to the Jenny's computer and then Jenny can reconnect her home drive using some script. I'm already wrote a script (see my previous post). But the script doesn't refer to the username and password Input Boxes. I.e. it doesn't meter what username and password user provided, script always map home directory for current logging user (it disconnect and connect it again). I assume that I need to use some other input method such as STDin and STDout. Since I'm bad scripter (I'm just learning...) I need help to make the username and password input boxes work in proper way. And if it possible, password needs to be masked when user input it. Here is the my code for reference:
              Option Explicit
              Dim objNetwork
              Dim strDriveLetter, strRemotePath, strUser, strPassword, strPCName, strRemoteShare

              ' Values of variables set
              strPCName = "London"
              strRemoteShare = InputBox ("Please enter your username")

              strDriveLetter = "V:"
              strRemotePath = "\\" & strPCName & "\" & strRemoteShare & "$" & ""
              ' A default for the MapNetworkDrive method is "persistent = false"

              strUser = strRemoteShare
              strPassword = InputBox ("Please enter your password:")

              Set objNetwork = WScript.CreateObject("WScript.Network")
              Err.Clear : objNetwork.RemoveNetworkDrive "P:", True, True
              objNetwork.MapNetworkDrive strDriveLetter, strRemotePath, false, strUser, strPassword

              ' Extra code just to add a message box
              WScript.Echo " Launch Explorer, check: "& strDriveLetter

              WScript.Quit
              Last edited by igor7; 25th February 2009, 06:35.

              Comment


              • #8
                Re: mapdrive using Inputbox for a pc name

                OK... I worked on this issue and wrote working code. Here is it:

                Code:
                If Instr(1, WScript.FullName, "wscript.exe", vbTextCompare) then
                boxTitle = "Map users home directory as drive [V:]"
                Set WshShell = WScript.CreateObject("WScript.Shell")
                WshShell.run "%windir%\system32\Cscript.exe //NoLogo """ _
                  & WScript.ScriptFullName & """",,FALSE
                Wscript.quit
                End If
                Dim objNetwork 
                Dim strDriveLetter, strRemotePath, strUser, strPassword, strPCName, strRemoteShare
                ' Values of variables set
                strPCName = "London"
                WScript.StdOut.Write "Please enter your username:"
                strRemoteShare = WScript.StdIn.ReadLine
                Wscript.Echo
                strDriveLetter = "P:" 
                strRemotePath = "\\" & strPCName & "\" & strRemoteShare & "$" & ""
                 
                strUser = strRemoteShare
                With CreateObject("ScriptPW.Password")
                WScript.StdOut.Write "Please enter your password:"
                strPassword = .GetPassword()
                End With
                Set objNetwork = WScript.CreateObject("WScript.Network")
                Err.Clear : objNetwork.RemoveNetworkDrive "P:", True, True
                If Err.Number = 0 Then Msgbox "Station P: succesfully disconnected" 
                objNetwork.MapNetworkDrive strDriveLetter, strRemotePath, false, strUser, strPassword
                 
                Wscript.Echo : Wscript.Echo
                WScript.StdOut.Write chr(34) & strUser & """ P drive is connected now!" : Wscript.Echo
                 
                WScript.StdOut.Write "Press Enter to quit" : Wscript.Echo
                WScript.StdIn.ReadLine
                This script disconnect currently logged in user P drive and connect P drive of user, which ran this script and provided his credential. But still it has small limitation... If currently logged in user disconnect his P drive manually (right click on My computer >> Disconnect network drive...) script not working i.e. it not maps new user P drive. My assumption that problem somewhere here:

                Code:
                Set objNetwork = WScript.CreateObject("WScript.Network")
                Err.Clear : objNetwork.RemoveNetworkDrive "P:", True, True
                If Err.Number = 0 Then Msgbox "Station P: succesfully disconnected" 
                objNetwork.MapNetworkDrive strDriveLetter, strRemotePath, false, strUser, strPassword
                But I have no idea how to make this script work even curenly logged in user P drive was disconnected manualy...

                Comment


                • #9
                  Re: mapdrive using Inputbox for a pc name

                  BTW, when I searched for solution I found other topic with MappingWithCredentials.HTA script (thanks Rems) and I adapted it according to my needs, so it doing the same job as previous script. Here is the code:

                  Code:
                  <HTML>
                  <HEAD>
                  <TITLE>Map Your Home Directory As "P" Drive</title>
                  <HTA:APPLICATION
                  ApplicationName="Map N.HTA"
                  SingleInstance="Yes"
                  WindowsState="Normal"
                  Scroll="No"
                  Navigable="Yes"
                  MaximizeButton="No"
                  SysMenu="Yes"
                  Caption="Yes"
                  ></HEAD>
                  <SCRIPT LANGUAGE="VBScript">
                  Sub Window_Onload
                  '# Size Window
                  sHorizontal = 440
                  sVertical = 175
                  Window.resizeTo sHorizontal, sVertical
                  '# Get Monitor Details
                  Set objWMIService = GetObject _
                    ("winmgmts:root\cimv2")
                  intHorizontal = sHorizontal *2
                  intVertical = sVertical *2
                  Set colItems = objWMIService.ExecQuery( _
                    "Select ScreenWidth, ScreenHeight from" _
                    & " Win32_DesktopMonitor", , 48)
                  For Each objItem In colItems
                    sWidth= objItem.ScreenWidth
                    sHeight = objItem.ScreenHeight
                    If sWidth > sHorizontal _
                      then intHorizontal = sWidth
                    If sHeight > sVertical _
                      then intVertical = sHeight
                  Next
                  Set objWMIService = Nothing
                  '# Center window on the screen
                  intLeft = (intHorizontal - sHorizontal) /2
                  intTop = (intVertical - sVertical) /2
                  Window.moveTo intLeft, intTop
                  '# default window content
                  window.location.href="#Top"
                  End Sub
                   
                  Sub RunScript
                  on Error Resume Next
                   
                  ' *** variables
                   
                  mDrive = "P:"
                  strUsr = UsrnameArea.Value
                  strRemoteShare = strUsr & "$"
                  strRemoteShare = "\\London\" & strRemoteShare & ""
                  strDriveAlias = "My Home Drive"
                   
                  minUSRnamelength = 2
                  minPASSwrdlength = 7
                   
                  ' *** Map drive using the entered credentials
                  strUsr = UsrnameArea.Value
                  strPas = PasswordArea.Value
                  Set objNetwork = CreateObject("WScript.Network")
                  Set oShell = CreateObject("Shell.Application")
                  Err.Clear : objNetwork.RemoveNetworkDrive "P:", True, True
                  If Err.Number = 0 Then Msgbox "Station P: succesfully disconnected"
                  If Len(strUsr) >= minUSRnamelength then
                  If Len(strPas) >= minPASSwrdlength then
                  Err.Clear
                  objNetwork.MapNetworkDrive mDrive, strRemoteShare, FALSE, strUsr, strPas
                  If Err.Number = 0 Then
                  oShell.NameSpace(mDrive).Self.Name = strDriveAlias
                  Msgbox chr(34) & strUsr & """ Home directory is connected now!"
                  End If
                  ELSE 
                  Msgbox "You typed an incorrect password !"
                  Exit Sub
                  End If
                  ELSE 
                  Msgbox chr(34) & strUsr & """ is an incorrect Username !"
                  Exit Sub
                  End If
                  Set oShell = Nothing
                  Set objNetwork = Nothing
                  Self.Close()
                  End Sub
                   
                  Sub CancelScript
                  Set oShell = Nothing
                  Set objNetwork = Nothing
                  Self.Close()
                  End Sub
                   
                  </SCRIPT>
                   
                  <BODY STYLE="font:14 pt arial; color:white; filter:progid:DXImageTransform.Microsoft.Gradient(GradientType=1, StartColorStr='#000000', EndColorStr='#0000FF')">
                  <a name="Top"></a><CENTER>
                  <table border="0" cellpadding="0" cellspacing="0"><font size="2" color="black" face="Arial">
                  <tr>
                  <td height="30">
                  <p align="right">Your Username</p>
                  </td>
                  <td height="30">&nbsp;&nbsp; <input type="text" name="UsrnameArea" size="30"></td></tr>
                  <tr>
                  <td height="30">
                  <p align="right">Company Password</p>
                  </td>
                  <td height="30">&nbsp;&nbsp; <input type="password" name="PasswordArea" size="30"></td></tr>
                  </table><BR>
                  <HR color="#0000FF">
                  <Input id=runbutton class="button" type="button" value=" Create Drive " name="run_button" onClick="RunScript">
                  &nbsp;&nbsp;&nbsp; 
                  <Input id=runbutton class="button" type="button" value="Cancel" name="cancel_button" onClick="CancelScript">
                  </CENTER>
                  </BODY>
                  </HTML>
                  This script work good, but always return error message after I hit OK on this message box:
                  Code:
                  Msgbox chr(34) & strUsr & """ Home directory is connected now!"


                  Here the error code:

                  ---------------------------
                  mshta.exe - Application Error
                  ---------------------------
                  The instruction at "0x026c1c7d" referenced memory at "0x026d5000". The required data was not placed into memory because of an I/O error status of "0xc00000c4".

                  Click on OK to terminate the program
                  ---------------------------
                  OK
                  ---------------------------
                  But if I remove acknowledge message about disconnected P drive:

                  Code:
                  If Err.Number = 0 Then Msgbox "Station P: succesfully disconnected"

                  Script working perfectly. How I can avoid this error message pop up?
                  Last edited by igor7; 1st March 2009, 04:40.

                  Comment


                  • #10
                    Re: mapdrive using Inputbox for a pc name

                    Originally posted by igor7 View Post
                    2. Before mapping user home directory I need to disconnect already mapped drive (P) on same server. - already found solution (thx, Rems): Err.Clear : objNetwork.RemoveNetworkDrive "P:", True, True
                    To call the Err.Clear statement is not sufficient here.

                    You only can actually clear an error when an error has been raised already in the script.
                    In order to enable error handling this statement should be on top of the section: On Error Resume Next. Without this, the script will terminate when an error occour and a popup with the error description appears - instead of just raising an error and the script continuous.

                    You only need to use Err.Clear when you want to clear the previous error value in order to trap and use conditional actions for the next error.
                    Instead of "Err.Clear" you should add the line "On Error Resume Next" in before you try to RemoveNetworkDrive. Now when P: does not exist, and error raise but the script will continue.


                    \Rems

                    This posting is provided "AS IS" with no warranties, and confers no rights.

                    __________________

                    ** Remember to give credit where credit's due **
                    and leave Reputation Points for meaningful posts

                    Comment


                    • #11
                      Re: mapdrive using Inputbox for a pc name

                      Originally posted by igor7 View Post
                      Hi, everyone!!
                      I'm trying to create script that allows to users open their home directory from any company computers. My sample based on Rems script:
                      Code:
                      Option Explicit
                      Dim objNetwork 
                      Dim strDriveLetter, strRemotePath, strUser, strPassword, strPCName, strRemoteShare
                       
                      ' Values of variables set
                      strPCName = "London"
                      strRemoteShare = InputBox ("Please enter your home share name with $ sign in the end:")
                       
                      strDriveLetter = "V:" 
                      strRemotePath = "\\" & strPCName & "\" & strRemoteShare & ""
                      ' A default for the MapNetworkDrive method is "persistent = false"
                       
                      strUser = InputBox ("Please enter your username:")
                      strPassword = InputBox ("Please enter your password:")
                       
                       
                       
                      Set objNetwork = WScript.CreateObject("WScript.Network") 
                      Err.Clear : objNetwork.RemoveNetworkDrive "P:", True, True 
                      objNetwork.MapNetworkDrive strDriveLetter, strRemotePath, false, strUser, strPassword
                       
                      ' Extra code just to add a message box
                      WScript.Echo " Launch Explorer, check: "& strDriveLetter
                       
                      WScript.Quit
                      I need improve it a little...
                      1. In our company users home directorys placed on root drive of the server (London) and associated with username, i.e. in aduc it looks like \\London\%username%$. How I can add $ (hidden share) parameter to strRemoteShare automatically without prompt user input it manually?
                      2. Before mapping user home directory I need to disconnect already mapped drive (P) on same server. - already found solution (thx, Rems): Err.Clear : objNetwork.RemoveNetworkDrive "P:", True, True
                      3. How I can mask user password when user typing it? I am trying to use vbs script example from scripting guy's forum:
                      Code:
                       
                      Set objPassword = CreateObject("ScriptPW.Password")
                      WScript.StdOut.Write "Please enter your password:" 
                       
                      strPassword = objPassword.GetPassword() 
                      Wscript.Echo
                      Wscript.Echo "Your password is: " & strPassword
                      But it returns "The handle is invalid" error??
                      How come everyone uses Static scripts? I'm just getting back into programming for the first time in over 20 years but... Whenever I write my scripts I write them to be dynamic using input boxes.

                      Wouldn't it be more helpful if you wrote them to be dynamic?

                      Comment


                      • #12
                        Re: mapdrive using Inputbox for a pc name

                        Rems,
                        Thank you very much!! Now both scripts works perfect!!
                        Originally posted by 1RealTruth View Post
                        How come everyone uses Static scripts?

                        I put this script on the web, so everyone can download and use it (I also wrote a note how, when and why it can be helpful.)

                        Comment

                        Working...
                        X