home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 February / CHIP_2_98.iso / software / pelne / optionp / wsh.cab / network.vbs < prev    next >
Text File  |  1997-09-16  |  5KB  |  149 lines

  1. ' Windows Script Host Sample Script
  2. '
  3. ' ------------------------------------------------------------------------
  4. '               Copyright (C) 1996 Microsoft Corporation
  5. '
  6. ' You have a royalty-free right to use, modify, reproduce and distribute
  7. ' the Sample Application Files (and/or any modified version) in any way
  8. ' you find useful, provided that you agree that Microsoft has no warranty,
  9. ' obligations or liability for any Sample Application Files.
  10. ' ------------------------------------------------------------------------
  11. '
  12. ' This sample demonstrates how to use the WSHNetwork object.
  13. ' It reads network properties (username and computername), 
  14. ' connects, disconnects, and enumerates network drives.
  15.  
  16.  
  17. L_Welcome_MsgBox_Message_Text   = "This script demonstrates how to use the WSHNetwork object."
  18. L_Welcome_MsgBox_Title_Text     = "Windows Scripting Host Sample"
  19. Call Welcome()
  20.  
  21. ' ********************************************************************************
  22. ' *
  23. ' * WSH Network Object.
  24. ' *
  25.  
  26. Dim WSHNetwork
  27. Dim colDrives, SharePoint
  28. Dim CRLF
  29.  
  30. CRLF = Chr(13) & Chr(10)
  31. Set WSHNetwork = WScript.CreateObject("WScript.Network")
  32.  
  33.  
  34. Function Ask(strAction)
  35.  
  36.    ' This function asks the user whether to perform a specific "Action"
  37.    ' and sets a return code or quits script execution depending on the 
  38.    ' button that the user presses.  This function is called at various
  39.    ' points in the script below.
  40.  
  41.     Dim intButton
  42.     intButton = MsgBox(strAction,                   _
  43.                        vbQuestion + vbYesNo,        _
  44.                        L_Welcome_MsgBox_Title_Text )
  45.     Ask = intButton = vbYes
  46. End Function
  47.  
  48. ' **************************************************
  49. ' *
  50. ' * Show WSHNetwork object properties
  51. ' *
  52. ' *
  53. MsgBox "UserDomain"     & Chr(9) & "= " & WSHNetwork.UserDomain  & CRLF &   _
  54.        "UserName"       & Chr(9) & "= " & WSHNetwork.UserName    & CRLF &   _
  55.        "ComputerName"   & Chr(9) & "= " & WSHNetwork.ComputerName,          _
  56.        vbInformation + vbOKOnly,                                            _
  57.        "WSHNetwork Properties"
  58.  
  59. ' **************************************************
  60. ' *
  61. ' * WSHNetwork.AddNetworkDrive
  62. ' *
  63. ' *
  64.  
  65. Function TryMapDrive(intDrive, strShare)
  66.     Dim strDrive
  67.     strDrive = Chr(intDrive + 64) & ":"
  68.     On Error Resume Next
  69.     WSHNetwork.MapNetworkDrive strDrive, strShare
  70.     TryMapDrive = Err.Number = 0
  71. End Function
  72.  
  73. If Ask("Do you want to connect a network drive?") Then
  74.     strShare = InputBox("Enter network share you want to connect to ")
  75.     For intDrive = 26 To 5 Step -1
  76.         If TryMapDrive(intDrive, strShare) Then Exit For
  77.     Next
  78.  
  79.     If intDrive <= 5 Then
  80.         MsgBox "Unable to connect to network share. "                        & _
  81.                "There are currently no drive letters available for use. "    & _
  82.                CRLF                                                          & _
  83.                "Please disconnect one of your existing network connections " & _
  84.                "and try this script again. ",                                  _
  85.                vbExclamation + vbOkOnly,        _
  86.                L_Welcome_MsgBox_Title_Text
  87.     Else
  88.         strDrive = Chr(intDrive + 64) & ":"
  89.         MsgBox "Connected " & strShare & " to drive " & strDrive,   _
  90.                vbInformation + vbOkOnly,                            _
  91.                L_Welcome_MsgBox_Title_Text
  92.  
  93.         If Ask("Do you want to disconnect the network drive you just created?") Then
  94.             WSHNetwork.RemoveNetworkDrive strDrive
  95.  
  96.             MsgBox "Disconnected drive " & strDrive,        _
  97.                    vbInformation + vbOkOnly,                _
  98.                    L_Welcome_MsgBox_Title_Text
  99.         End If
  100.     End If
  101. End If
  102.  
  103.  
  104. ' **************************************************
  105. ' *
  106. ' * WSHNetwork.EnumNetworkDrive
  107. ' *
  108. ' *
  109. 'Ask user whether to enumerate network drives
  110. If Ask("Do you want to enumerate connected network drives?") Then
  111.     'Enumerate network drives into a collection object of type WshCollection
  112.     Set colDrives = WSHNetwork.EnumNetworkDrives
  113.  
  114.     'If no network drives were enumerated, then inform user, else display 
  115.     'enumerated drives
  116.     If colDrives.Count = 0 Then
  117.         MsgBox "There are no drives to enumerate.",     _
  118.                vbInformation + vbOkOnly,                _
  119.                L_Welcome_MsgBox_Title_Text
  120.     Else
  121.         strMsg = "Current network drive connections: " & CRLF
  122.         For i = 0 To colDrives.Count - 1 Step 2
  123.             strMsg = strMsg & CRLF & colDrives(i) & Chr(9) & colDrives(i + 1)
  124.         Next
  125.         
  126.         MsgBox strMsg,                                  _
  127.                vbInformation + vbOkOnly,                _
  128.                L_Welcome_MsgBox_Title_Text
  129.  
  130.     End If
  131. End If
  132.  
  133. ' ********************************************************************************
  134. ' *
  135. ' * Welcome
  136. ' *
  137. Sub Welcome()
  138.     Dim intDoIt
  139.  
  140.     intDoIt =  MsgBox(L_Welcome_MsgBox_Message_Text, _
  141.                       vbOKCancel + vbInformation,    _
  142.                       L_Welcome_MsgBox_Title_Text )
  143.     If intDoIt = vbCancel Then
  144.         WScript.Quit
  145.     End If
  146. End Sub
  147.  
  148.  
  149.