home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 Special / chip-cd_2001_spec_05.zip / spec_05 / ras.cab / netconnections.vbs < prev    next >
Text File  |  1999-11-04  |  22KB  |  642 lines

  1. '********************************************************************
  2. '*
  3. '* File:           NetConnections.vbs 
  4. '* Created:        March 1999
  5. '* Version:        1.0
  6. '*
  7. '*  Main Function:  Lists Selected Properties of Network Connections.
  8. '*
  9. '*  NetConnections.vbs [/S <server>] [/U <username>] [/W <password>] 
  10. '*              [/O <outputfile>]
  11. '*
  12. '* Copyright (C) 1999 Microsoft Corporation
  13. '*
  14. '********************************************************************
  15.  
  16.  
  17. OPTION EXPLICIT
  18.  
  19.     'Define constants
  20.     CONST CONST_ERROR                   = 0
  21.     CONST CONST_WSCRIPT                 = 1
  22.     CONST CONST_CSCRIPT                 = 2
  23.     CONST CONST_SHOW_USAGE              = 3
  24.     CONST CONST_PROCEED                 = 4
  25.  
  26.     'Declare variables
  27.     Dim intOpMode, i
  28.     Dim strServer, strUserName, strPassword, strOutputFile
  29.  
  30.     'Make sure the host is csript, if not then abort
  31.     VerifyHostIsCscript()
  32.  
  33.     'Parse the command line
  34.     intOpMode = intParseCmdLine(strServer     ,  _
  35.                                 strUserName   ,  _
  36.                                 strPassword   ,  _
  37.                                 strOutputFile    )
  38.  
  39.  
  40.     Select Case intOpMode
  41.  
  42.         Case CONST_SHOW_USAGE
  43.             Call ShowUsage()
  44.  
  45.         Case CONST_PROCEED                 
  46.             Call NetConnections(strServer     , _
  47.                                 strOutputFile , _
  48.                                 strUserName   , _
  49.                                 strPassword     )
  50.  
  51.         Case CONST_ERROR
  52.             'Do Nothing
  53.  
  54.         Case Else                    'Default -- should never happen
  55.             Call Wscript.Echo("Error occurred in passing parameters.")
  56.  
  57.     End Select
  58.  
  59. '********************************************************************
  60. '* End of Script
  61. '********************************************************************
  62.  
  63. '********************************************************************
  64. '*
  65. '* Sub NetConnections()
  66. '*
  67. '* Purpose: Outputs Information on CDRomDrives.
  68. '*
  69. '* Input:   strServer           a machine name
  70. '*          strOutputFile       an output file name
  71. '*          strUserName         the current user's name
  72. '*          strPassword         the current user's password
  73. '*
  74. '* Output:  Results are either printed on screen or saved in strOutputFile.
  75. '*
  76. '********************************************************************
  77.  
  78. Private Sub NetConnections(strServer, strOutputFile, strUserName, strPassword)
  79.  
  80.     ON ERROR RESUME NEXT
  81.  
  82.     Dim objFileSystem, objOutputFile, objService, objNetSet, obj, objInst
  83.     Dim strLine, strWbemClass
  84.  
  85.     strWbemClass = "Win32_SystemNetworkConnections"
  86.  
  87.     'Open a text file for output if the file is requested
  88.     If Not IsEmpty(strOutputFile) Then
  89.         If (NOT blnOpenFile(strOutputFile, objOutputFile)) Then
  90.             Call Wscript.Echo ("Could not open an output file.")
  91.             Exit Sub
  92.         End If
  93.     End If
  94.  
  95.     'Establish a connection with the server.
  96.     If blnConnect("root\cimv2" , _
  97.                    strUserName , _
  98.                    strPassword , _
  99.                    strServer   , _
  100.                    objService  ) Then
  101.         Call Wscript.Echo("")
  102.         Call Wscript.Echo("Please check the server name, " _
  103.                         & "credentials and WBEM Core.")
  104.         Exit Sub
  105.     End If
  106.  
  107.  
  108.     'Get the set
  109.     Set objNetSet = objService.InstancesOf(strWBEMClass)
  110.     If blnErrorOccurred("Could not obtain " & strWBEMClass & " instance.") Then
  111.         Exit Sub
  112.     End If
  113.  
  114.     If objNetSet.Count = 0 Then
  115.         Call WriteLine("No Network Connection information is available." _
  116.                      , objOutputFile)    
  117.         Exit Sub
  118.     End If
  119.  
  120.     Call WriteLine("Network Connections for Machine " & strServer, _
  121.                     objOutputFile)
  122.     Call WriteLine("", objOutputFile)
  123.  
  124.     For Each objInst In objNetSet
  125.         Set obj = objService.Get(objInst.PartComponent)
  126.         Call WriteLine("Status        = " _
  127.                      & Obj.Status, objOutputFile)
  128.         Call WriteLine("Local Name    = " _
  129.                      & Obj.LocalName, objOutputFile)
  130.         Call WriteLine("Remote Name   = " _
  131.                      & Obj.RemoteName, objOutputFile)
  132.         Call WriteLine("Provider Name = " _
  133.                      & Obj.ProviderName, objOutputFile)
  134.         Call WriteLine("Persistent    = " _
  135.                      & Obj.Persistent, objOutputFile)
  136.         Call WriteLine("Resource Type = " _
  137.                      & Obj.ResourceType, objOutputFile)
  138.         Call WriteLine("", objOutputFile)
  139.     Next
  140.  
  141.     If IsObject(objOutputFile) Then
  142.         objOutputFile.Close
  143.         Call Wscript.Echo ("Results are saved in file " & strOutputFile & ".")
  144.     End If
  145.  
  146. End Sub
  147.  
  148. '********************************************************************
  149. '*
  150. '* Function intParseCmdLine()
  151. '*
  152. '* Purpose: Parses the command line.
  153. '* Input:   
  154. '*
  155. '* Output:  strServer         a remote server ("" = local server")
  156. '*          strUserName       the current user's name
  157. '*          strPassword       the current user's password
  158. '*          strOutputFile     an output file name
  159. '*
  160. '********************************************************************
  161. Private Function intParseCmdLine( ByRef strServer,        _
  162.                                   ByRef strUserName,      _
  163.                                   ByRef strPassword,      _
  164.                                   ByRef strOutputFile     )
  165.  
  166.  
  167.     ON ERROR RESUME NEXT
  168.  
  169.     Dim strFlag
  170.     Dim intState, intArgIter
  171.     Dim objFileSystem
  172.  
  173.     If Wscript.Arguments.Count > 0 Then
  174.         strFlag = Wscript.arguments.Item(0)
  175.     End If
  176.  
  177.     If IsEmpty(strFlag) Then                'No arguments have been received
  178.         intParseCmdLine = CONST_PROCEED
  179.         Exit Function
  180.     End If
  181.  
  182.     'Check if the user is asking for help or is just confused
  183.     If (strFlag="help") OR (strFlag="/h") OR (strFlag="\h") OR (strFlag="-h") _
  184.         OR (strFlag = "\?") OR (strFlag = "/?") OR (strFlag = "?") _ 
  185.         OR (strFlag="h") Then
  186.         intParseCmdLine = CONST_SHOW_USAGE
  187.         Exit Function
  188.     End If
  189.  
  190.     'Retrieve the command line and set appropriate variables
  191.      intArgIter = 0
  192.     Do While intArgIter <= Wscript.arguments.Count - 1
  193.         Select Case Left(LCase(Wscript.arguments.Item(intArgIter)),2)
  194.   
  195.             Case "/s"
  196.                 If Not blnGetArg("Server", strServer, intArgIter) Then
  197.                     intParseCmdLine = CONST_ERROR
  198.                     Exit Function
  199.                 End If
  200.                 intArgIter = intArgIter + 1
  201.  
  202.             Case "/o"
  203.                 If Not blnGetArg("Output File", strOutputFile, intArgIter) Then
  204.                     intParseCmdLine = CONST_ERROR
  205.                     Exit Function
  206.                 End If
  207.                 intArgIter = intArgIter + 1
  208.  
  209.             Case "/u"
  210.                 If Not blnGetArg("User Name", strUserName, intArgIter) Then
  211.                     intParseCmdLine = CONST_ERROR
  212.                     Exit Function
  213.                 End If
  214.                 intArgIter = intArgIter + 1
  215.  
  216.             Case "/w"
  217.                 If Not blnGetArg("User Password", strPassword, intArgIter) Then
  218.                     intParseCmdLine = CONST_ERROR
  219.                     Exit Function
  220.                 End If
  221.                 intArgIter = intArgIter + 1
  222.  
  223.             Case Else 'We shouldn't get here
  224.                 Call Wscript.Echo("Invalid or misplaced parameter: " _
  225.                    & Wscript.arguments.Item(intArgIter) & vbCRLF _
  226.                    & "Please check the input and try again," & vbCRLF _
  227.                    & "or invoke with '/?' for help with the syntax.")
  228.                 Wscript.Quit
  229.  
  230.         End Select
  231.  
  232.     Loop '** intArgIter <= Wscript.arguments.Count - 1
  233.  
  234.     If IsEmpty(intParseCmdLine) Then _
  235.         intParseCmdLine = CONST_PROCEED
  236.  
  237. End Function
  238.  
  239. '********************************************************************
  240. '*
  241. '* Sub ShowUsage()
  242. '*
  243. '* Purpose: Shows the correct usage to the user.
  244. '*
  245. '* Input:   None
  246. '*
  247. '* Output:  Help messages are displayed on screen.
  248. '*
  249. '********************************************************************
  250. Private Sub ShowUsage()
  251.  
  252.     Wscript.Echo ""
  253.     Wscript.Echo "Lists Selected Properties of Network Connections."
  254.     Wscript.Echo ""
  255.     Wscript.Echo "SYNTAX:"
  256.     Wscript.Echo "  NetConnections.vbs [/S <server>] [/U <username>]" _
  257.                & " [/W <password>]"
  258.     Wscript.Echo "  [/O <outputfile>]"
  259.     Wscript.Echo ""
  260.     Wscript.Echo "PARAMETER SPECIFIERS:"
  261.     Wscript.Echo "   server        A machine name."
  262.     Wscript.Echo "   username      The current user's name."
  263.     Wscript.Echo "   password      Password of the current user."
  264.     Wscript.Echo "   outputfile    The output file name."
  265.     Wscript.Echo ""
  266.     Wscript.Echo "EXAMPLE:"
  267.     Wscript.Echo "1. cscript NetConnections.vbs"
  268.     Wscript.Echo "   List network connections for the " _
  269.                & "current machine."
  270.     Wscript.Echo "2. cscript NetConnections.vbs /S MyMachine2"
  271.     Wscript.Echo "   List network connections for the " _
  272.               & "machine MyMachine2."
  273.  
  274. End Sub
  275.  
  276. '********************************************************************
  277. '* General Routines
  278. '********************************************************************
  279.  
  280. '********************************************************************
  281. '*
  282. '* Function strPackString()
  283. '*
  284. '* Purpose: Attaches spaces to a string to increase the length to intWidth.
  285. '*
  286. '* Input:   strString   a string
  287. '*          intWidth    the intended length of the string
  288. '*          blnAfter    Should spaces be added after the string?
  289. '*          blnTruncate specifies whether to truncate the string or not if
  290. '*                      the string length is longer than intWidth
  291. '*
  292. '* Output:  strPackString is returned as the packed string.
  293. '*
  294. '********************************************************************
  295. Private Function strPackString( ByVal strString, _
  296.                                 ByVal intWidth,  _
  297.                                 ByVal blnAfter,  _
  298.                                 ByVal blnTruncate)
  299.  
  300.     ON ERROR RESUME NEXT
  301.  
  302.     intWidth      = CInt(intWidth)
  303.     blnAfter      = CBool(blnAfter)
  304.     blnTruncate   = CBool(blnTruncate)
  305.  
  306.     If Err.Number Then
  307.         Call Wscript.Echo ("Argument type is incorrect!")
  308.         Err.Clear
  309.         Wscript.Quit
  310.     End If
  311.  
  312.     If IsNull(strString) Then
  313.         strPackString = "null" & Space(intWidth-4)
  314.         Exit Function
  315.     End If
  316.  
  317.     strString = CStr(strString)
  318.     If Err.Number Then
  319.         Call Wscript.Echo ("Argument type is incorrect!")
  320.         Err.Clear
  321.         Wscript.Quit
  322.     End If
  323.  
  324.     If intWidth > Len(strString) Then
  325.         If blnAfter Then
  326.             strPackString = strString & Space(intWidth-Len(strString))
  327.         Else
  328.             strPackString = Space(intWidth-Len(strString)) & strString & " "
  329.         End If
  330.     Else
  331.         If blnTruncate Then
  332.             strPackString = Left(strString, intWidth-1) & " "
  333.         Else
  334.             strPackString = strString & " "
  335.         End If
  336.     End If
  337.  
  338. End Function
  339.  
  340. '********************************************************************
  341. '* 
  342. '*  Function blnGetArg()
  343. '*
  344. '*  Purpose: Helper to intParseCmdLine()
  345. '* 
  346. '*  Usage:
  347. '*
  348. '*     Case "/s" 
  349. '*       blnGetArg ("server name", strServer, intArgIter)
  350. '*
  351. '********************************************************************
  352. Private Function blnGetArg ( ByVal StrVarName,   _
  353.                              ByRef strVar,       _
  354.                              ByRef intArgIter) 
  355.  
  356.     blnGetArg = False 'failure, changed to True upon successful completion
  357.  
  358.     If Len(Wscript.Arguments(intArgIter)) > 2 then
  359.         If Mid(Wscript.Arguments(intArgIter),3,1) = ":" then
  360.             If Len(Wscript.Arguments(intArgIter)) > 3 then
  361.                 strVar = Right(Wscript.Arguments(intArgIter), _
  362.                          Len(Wscript.Arguments(intArgIter)) - 3)
  363.                 blnGetArg = True
  364.                 Exit Function
  365.             Else
  366.                 intArgIter = intArgIter + 1
  367.                 If intArgIter > (Wscript.Arguments.Count - 1) Then
  368.                     Call Wscript.Echo( "Invalid " & StrVarName & ".")
  369.                     Call Wscript.Echo( "Please check the input and try again.")
  370.                     Exit Function
  371.                 End If
  372.  
  373.                 strVar = Wscript.Arguments.Item(intArgIter)
  374.                 If Err.Number Then
  375.                     Call Wscript.Echo( "Invalid " & StrVarName & ".")
  376.                     Call Wscript.Echo( "Please check the input and try again.")
  377.                     Exit Function
  378.                 End If
  379.  
  380.                 If InStr(strVar, "/") Then
  381.                     Call Wscript.Echo( "Invalid " & StrVarName)
  382.                     Call Wscript.Echo( "Please check the input and try again.")
  383.                     Exit Function
  384.                 End If
  385.  
  386.                 blnGetArg = True 'success
  387.             End If
  388.         Else
  389.             strVar = Right(Wscript.Arguments(intArgIter), _
  390.                      Len(Wscript.Arguments(intArgIter)) - 2)
  391.             blnGetArg = True 'success
  392.             Exit Function
  393.         End If
  394.     Else
  395.         intArgIter = intArgIter + 1
  396.         If intArgIter > (Wscript.Arguments.Count - 1) Then
  397.             Call Wscript.Echo( "Invalid " & StrVarName & ".")
  398.             Call Wscript.Echo( "Please check the input and try again.")
  399.             Exit Function
  400.         End If
  401.  
  402.         strVar = Wscript.Arguments.Item(intArgIter)
  403.         If Err.Number Then
  404.             Call Wscript.Echo( "Invalid " & StrVarName & ".")
  405.             Call Wscript.Echo( "Please check the input and try again.")
  406.             Exit Function
  407.         End If
  408.  
  409.         If InStr(strVar, "/") Then
  410.             Call Wscript.Echo( "Invalid " & StrVarName)
  411.             Call Wscript.Echo( "Please check the input and try again.")
  412.             Exit Function
  413.         End If
  414.         blnGetArg = True 'success
  415.     End If
  416. End Function
  417.  
  418. '********************************************************************
  419. '*
  420. '* Function blnConnect()
  421. '*
  422. '* Purpose: Connects to machine strServer.
  423. '*
  424. '* Input:   strServer       a machine name
  425. '*          strNameSpace    a namespace
  426. '*          strUserName     name of the current user
  427. '*          strPassword     password of the current user
  428. '*
  429. '* Output:  objService is returned  as a service object.
  430. '*          strServer is set to local host if left unspecified
  431. '*
  432. '********************************************************************
  433. Private Function blnConnect(ByVal strNameSpace, _
  434.                             ByVal strUserName,  _
  435.                             ByVal strPassword,  _
  436.                             ByRef strServer,    _
  437.                             ByRef objService)
  438.  
  439.     ON ERROR RESUME NEXT
  440.  
  441.     Dim objLocator, objWshNet
  442.  
  443.     blnConnect = False     'There is no error.
  444.  
  445.     'Create Locator object to connect to remote CIM object manager
  446.     Set objLocator = CreateObject("WbemScripting.SWbemLocator")
  447.     If Err.Number then
  448.         Call Wscript.Echo( "Error 0x" & CStr(Hex(Err.Number)) & _
  449.                            " occurred in creating a locator object." )
  450.         If Err.Description <> "" Then
  451.             Call Wscript.Echo( "Error description: " & Err.Description & "." )
  452.         End If
  453.         Err.Clear
  454.         blnConnect = True     'An error occurred
  455.         Exit Function
  456.     End If
  457.  
  458.     'Connect to the namespace which is either local or remote
  459.     Set objService = objLocator.ConnectServer (strServer, strNameSpace, _
  460.        strUserName, strPassword)
  461.     ObjService.Security_.impersonationlevel = 3
  462.     If Err.Number then
  463.         Call Wscript.Echo( "Error 0x" & CStr(Hex(Err.Number)) & _
  464.                            " occurred in connecting to server " _
  465.            & strServer & ".")
  466.         If Err.Description <> "" Then
  467.             Call Wscript.Echo( "Error description: " & Err.Description & "." )
  468.         End If
  469.         Err.Clear
  470.         blnConnect = True     'An error occurred
  471.     End If
  472.  
  473.     'Get the current server's name if left unspecified
  474.     If IsEmpty(strServer) Then
  475.         Set objWshNet = CreateObject("Wscript.Network")
  476.     strServer     = objWshNet.ComputerName
  477.     End If
  478.  
  479. End Function
  480.  
  481. '********************************************************************
  482. '*
  483. '* Sub      VerifyHostIsCscript()
  484. '*
  485. '* Purpose: Determines which program is used to run this script.
  486. '*
  487. '* Input:   None
  488. '*
  489. '* Output:  If host is not cscript, then an error message is printed 
  490. '*          and the script is aborted.
  491. '*
  492. '********************************************************************
  493. Sub VerifyHostIsCscript()
  494.  
  495.     ON ERROR RESUME NEXT
  496.  
  497.     Dim strFullName, strCommand, i, j, intStatus
  498.  
  499.     strFullName = WScript.FullName
  500.  
  501.     If Err.Number then
  502.         Call Wscript.Echo( "Error 0x" & CStr(Hex(Err.Number)) & " occurred." )
  503.         If Err.Description <> "" Then
  504.             Call Wscript.Echo( "Error description: " & Err.Description & "." )
  505.         End If
  506.         intStatus =  CONST_ERROR
  507.     End If
  508.  
  509.     i = InStr(1, strFullName, ".exe", 1)
  510.     If i = 0 Then
  511.         intStatus =  CONST_ERROR
  512.     Else
  513.         j = InStrRev(strFullName, "\", i, 1)
  514.         If j = 0 Then
  515.             intStatus =  CONST_ERROR
  516.         Else
  517.             strCommand = Mid(strFullName, j+1, i-j-1)
  518.             Select Case LCase(strCommand)
  519.                 Case "cscript"
  520.                     intStatus = CONST_CSCRIPT
  521.                 Case "wscript"
  522.                     intStatus = CONST_WSCRIPT
  523.                 Case Else       'should never happen
  524.                     Call Wscript.Echo( "An unexpected program was used to " _
  525.                                        & "run this script." )
  526.                     Call Wscript.Echo( "Only CScript.Exe or WScript.Exe can " _
  527.                                        & "be used to run this script." )
  528.                     intStatus = CONST_ERROR
  529.                 End Select
  530.         End If
  531.     End If
  532.  
  533.     If intStatus <> CONST_CSCRIPT Then
  534.         Call WScript.Echo( "Please run this script using CScript." & vbCRLF & _
  535.              "This can be achieved by" & vbCRLF & _
  536.              "1. Using ""CScript NetConnections.vbs arguments"" for Windows 95/98 or" _
  537.              & vbCRLF & "2. Changing the default Windows Scripting Host " _
  538.              & "setting to CScript" & vbCRLF & "    using ""CScript " _
  539.              & "//H:CScript //S"" and running the script using" & vbCRLF & _
  540.              "    ""NetConnections.vbs arguments"" for Windows NT/2000." )
  541.         WScript.Quit
  542.     End If
  543.  
  544. End Sub
  545.  
  546. '********************************************************************
  547. '*
  548. '* Sub WriteLine()
  549. '* Purpose: Writes a text line either to a file or on screen.
  550. '* Input:   strMessage  the string to print
  551. '*          objFile     an output file object
  552. '* Output:  strMessage is either displayed on screen or written to a file.
  553. '*
  554. '********************************************************************
  555. Sub WriteLine(ByVal strMessage, ByVal objFile)
  556.  
  557.     On Error Resume Next
  558.     If IsObject(objFile) then        'objFile should be a file object
  559.         objFile.WriteLine strMessage
  560.     Else
  561.         Call Wscript.Echo( strMessage )
  562.     End If
  563.  
  564. End Sub
  565.  
  566. '********************************************************************
  567. '* 
  568. '* Function blnErrorOccurred()
  569. '*
  570. '* Purpose: Reports error with a string saying what the error occurred in.
  571. '*
  572. '* Input:   strIn        string saying what the error occurred in.
  573. '*
  574. '* Output:  displayed on screen 
  575. '* 
  576. '********************************************************************
  577. Private Function blnErrorOccurred (ByVal strIn)
  578.  
  579.     If Err.Number Then
  580.         Call Wscript.Echo( "Error 0x" & CStr(Hex(Err.Number)) & ": " & strIn)
  581.         If Err.Description <> "" Then
  582.             Call Wscript.Echo( "Error description: " & Err.Description)
  583.         End If
  584.         Err.Clear
  585.         blnErrorOccurred = True
  586.     Else
  587.         blnErrorOccurred = False
  588.     End If
  589.  
  590. End Function
  591.  
  592. '********************************************************************
  593. '* 
  594. '* Function blnOpenFile
  595. '*
  596. '* Purpose: Opens a file.
  597. '*
  598. '* Input:   strFileName        A string with the name of the file.
  599. '*
  600. '* Output:  Sets objOpenFile to a FileSystemObject and setis it to 
  601. '*            Nothing upon Failure.
  602. '* 
  603. '********************************************************************
  604. Private Function blnOpenFile(ByVal strFileName, ByRef objOpenFile)
  605.  
  606.     ON ERROR RESUME NEXT
  607.  
  608.     Dim objFileSystem
  609.  
  610.     Set objFileSystem = Nothing
  611.  
  612.     If IsEmpty(strFileName) OR strFileName = "" Then
  613.         blnOpenFile = False
  614.         Set objOpenFile = Nothing
  615.         Exit Function
  616.     End If
  617.  
  618.     'Create a file object
  619.     Set objFileSystem = CreateObject("Scripting.FileSystemObject")
  620.     If blnErrorOccurred("Could not create filesystem object.") Then
  621.         blnOpenFile = False
  622.         Set objOpenFile = Nothing
  623.         Exit Function
  624.     End If
  625.  
  626.     'Open the file for output
  627.     Set objOpenFile = objFileSystem.OpenTextFile(strFileName, 8, True)
  628.     If blnErrorOccurred("Could not open") Then
  629.         blnOpenFile = False
  630.         Set objOpenFile = Nothing
  631.         Exit Function
  632.     End If
  633.     blnOpenFile = True
  634.  
  635. End Function
  636.  
  637. '********************************************************************
  638. '*                                                                  *
  639. '*                           End of File                            *
  640. '*                                                                  *
  641. '********************************************************************
  642.