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

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