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

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