Jump to content

Wscript closing login session ?


Recommended Posts

Hi there I've made a couple of scripts that are working perfectly in Win7, however problems arise in WinXP.Here is the first script. ** Connect script ** Const TIMEOUT = 3On Error Resume NextSet objShell = CreateObject("WScript.Shell")Set objNetwork = CreateObject("WScript.Network")Set objFSO = CreateObject("Scripting.FileSystemObject")Set CheckDrive = objNetwork.EnumNetworkDrives()'declare variables'strDesktop = objShell.SpecialFolders("Desktop")strPath = "\\SERVER\D"strPath2 = "\\SERVER\E"strDriveLetter = "Z:"strDriveLetter2 = "X:"strProfile = TruestrUser = "*****"strPassword = "*****"AlreadyConnected = False'check if drives are connected'For intDrive = 0 To CheckDrive.Count - 1 Step 2If CheckDrive.Item(intDrive) = strDriveLetter _Then AlreadyConnected = TrueobjShell.Popup "Server Drives are Already Connected", TIMEOUTWscript.quitNext'check if drives are connected, if not connect'objShell.Popup "Connecting Server Drives" , TIMEOUTobjNetwork.MapNetworkDrive strDriveLetter , strPath , strProfile, strUser , strPasswordobjNetwork.MapNetworkDrive strDriveLetter2 , strPath2 , strProfile, strUser , strPassword'if no errors'If Err = 0 Then'create shortcut after connection established'IF Not objFSO.FileExists(objShortcutLnk) Then _Set objShortcutLnk = objShell.CreateShortcut(strDesktop & "\Server Drive.lnk")objShortcutLnk.TargetPath = strPathobjShortcutLnk.IconLocation = "%SystemRoot%\system32\SHELL32.dll,9"objShortcutLnk.SaveSet objShortcutLnk2 = objShell.CreateShortcut(strDesktop & "\Server Drive Images.lnk")objShortcutLnk2.TargetPath = strPath2objShortcutLnk2.IconLocation = "%SystemRoot%\system32\SHELL32.dll,9"objShortcutLnk2.SaveobjShell.Popup "Connection Success", TIMEOUTWscript.quit'if there are errors attempt reconnect'Else If Err <> 0 ThenobjShell.Popup "Connection Failed: " & Err.Description & vbCrLf & _ "Attempting Reconnection", TIMEOUT Err.ClearobjNetwork.MapNetworkDrive strDriveLetter , strPath , strProfile, strUser , strPasswordobjNetwork.MapNetworkDrive strDriveLetter2 , strPath2 , strProfile, strUser , strPasswordIF Not objFSO.FileExists(objShortcutLnk) Then _objShortcutLnk.TargetPath = strPathobjShortcutLnk.IconLocation = "%SystemRoot%\system32\SHELL32.dll,9"objShortcutLnk.SaveobjShortcutLnk2.TargetPath = strPath2objShortcutLnk2.IconLocation = "%SystemRoot%\system32\SHELL32.dll,9"objShortcutLnk2.SaveobjShell.Popup "Connection Success", TIMEOUTWscript.quit'check reconnection'If Err <> 0 Then _objShell.Popup "Reconnection Failed: " & Err.Description, TIMEOUT _Else _objShell.Popup "Connection Success", TIMEOUT End IfEnd If--------------------------------------------------------------------------------------- I have created this disconnect script in the event that there is an error with the connection script, you can run the disconnect and it clears the drives / shortcuts and then you can try connect again.I did this because sometimes when the connect script is run on startup if the server is lagging or whatever, it may not execute the script correctly and I've noticed it can sometimes map the drives without logging in theuser to the server. So when you try access the mapped drives, you can't login. As I say both these scripts work fine in Win7, and even when I remove On Error Resume Next it still does not come up with an error. Now the problem is in XP if you run the disconnect script after the user has clickedon the Shortcut that is created by the connect script, it somehow keeps the session open between the client and the server. So when you run the disconnect script, even though it works correctly and gets rid of themapped drives and shortcuts without any errors, when you try run the connect script again it says that the drives are still connected. This only seems to happen if the user has opened the shortcut. If you just run the connect script then the disconnect it works fine. What I want to know, is there a CreateObject("WScript.Network") object you can use with this in order to close the login session to the server, because if the session is closed when the user runs the disconnect scriptthat should prevent the session from remaining open, thus forcing the script to reconnect ? ** Disconnect Script ** Const TIMEOUT = 3On Error Resume NextSet objShell = CreateObject("WScript.Shell")Set objNetwork = CreateObject("WScript.Network")Set objFSO = CreateObject("Scripting.FileSystemObject")Set CheckDrive = objNetwork.EnumNetworkDrives()'declare variables'strDesktop = objShell.SpecialFolders("Desktop")strPath = "\\SERVER\D"strPath2 = "\\SERVER\E"strDriveLetter = "Z:"strDriveLetter2 = "X:"strProfile = TruestrUser = "*****"strPassword = "*****"AlreadyConnected = False'check for shortcut and delete'IF objFSO.FileExists(strDesktop & "\Server Drive.lnk") then _Set objShortcutLnk = objShell.CreateShortcut(strDesktop & "\Server Drive.lnk")Set objShortcutLnk2 = objShell.CreateShortcut(strDesktop & "\Server Drive Images.lnk")objFSO.DeleteFile(objShortcutLnk)objFSO.DeleteFile(objShortcutLnk2)'check if drives are connected and disconnect'For intDrive = 0 To CheckDrive.Count - 1 Step 2If CheckDrive.Item(intDrive) = strDriveLetter _Then AlreadyConnected = TrueobjNetwork.RemoveNetworkDrive strDriveLetter, True, TrueobjNetwork.RemoveNetworkDrive strDriveLetter2, True, TrueobjShell.Popup "Server Drives Disconnected", TIMEOUTWscript.quitNext'check if drives already disconnected'If AlreadyConnected = False thenobjShell.Popup "Server Drives Already Disconnected", TIMEOUTWscript.quit End If

Edited by foxhound187
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...