home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 May / W2KPRK.iso / netmgmt.cab / conall.vbs < prev    next >
Text File  |  1999-11-04  |  7KB  |  353 lines

  1. '----------------------------------------------------------------------
  2. '
  3. ' Copyright (c) Microsoft Corporation 1998-1999
  4. ' All Rights Reserved
  5. '
  6. ' Abstract:
  7. '
  8. ' conall.vbs - connects to all printers on a print server.
  9. '
  10. ' Usage:
  11. ' conall [-a?] [-c server]
  12. '
  13. ' Examples:
  14. ' conall -c \\server
  15. ' conall -a -c \\server
  16. '
  17. '----------------------------------------------------------------------
  18.  
  19. option explicit
  20.  
  21. '
  22. ' Debugging trace flags, to enable debug output trace message
  23. ' change gDebugFlag to true.
  24. '
  25. const kDebugTrace = 1
  26. const kDebugError = 2
  27. dim   gDebugFlag
  28.  
  29. gDebugFlag = false
  30.  
  31. '
  32. ' Messages to be displayed if the scripting host is not cscript
  33. '                            
  34. const kMessage1 = "Please run this script using CScript."  
  35. const kMessage2 = "This can be achieved by"
  36. const kMessage3 = "1. Using ""CScript script.vbs arguments"" or" 
  37. const kMessage4 = "2. Changing the default Windows Scripting Host to CScript"
  38. const kMessage5 = "   using ""CScript //H:CScript //S"" and running the script "
  39. const kMessage6 = "   ""script.vbs arguments""."
  40.  
  41. '
  42. ' Operation action values.
  43. '
  44. const kActionUnknown    = 0
  45. const kActionConnect    = 1
  46.  
  47. const kErrorSuccess     = 0
  48. const KErrorFailure     = 1
  49.  
  50. main
  51.  
  52. '
  53. ' Main execution starts here
  54. '
  55. sub main
  56.  
  57.     dim iAction
  58.     dim iRetval
  59.     dim strServer
  60.     
  61.     '
  62.     ' Abort if the host is not cscript
  63.     '
  64.     if not IsHostCscript() then
  65.    
  66.         call wscript.echo(kMessage1 & vbCRLF & kMessage2 & vbCRLF & _
  67.                           kMessage3 & vbCRLF & kMessage4 & vbCRLF & _
  68.                           kMessage5 & vbCRLF & kMessage6 & vbCRLF)
  69.         
  70.         wscript.quit
  71.    
  72.     end if
  73.  
  74.     iRetval = ParseCommandLine(iAction, strServer)
  75.  
  76.     if iRetval = kErrorSuccess then
  77.  
  78.         select case iAction
  79.  
  80.         case kActionConnect
  81.                 
  82.             if strServer = "" then
  83.             
  84.                 wscript.echo "Please specify the server you want to connect to"
  85.                 
  86.                 iRetVal = kErrorFailure
  87.         
  88.             else
  89.         
  90.                 iRetval = AddAllPrinters(strServer)
  91.         
  92.             end if
  93.  
  94.         case else
  95.         
  96.              Usage(true)
  97.  
  98.         end select
  99.  
  100.     end if
  101.  
  102. end sub
  103.  
  104. '
  105. ' Add a printer connection
  106. '
  107. function AddPrinterConnection(strPrinter)
  108.  
  109.     on error resume next
  110.  
  111.     DebugPrint kDebugTrace, "In AddPrinterConnection"
  112.  
  113.     dim oMaster
  114.     dim iRetval
  115.  
  116.     set oMaster = CreateObject("PrintMaster.PrintMaster.1")
  117.  
  118.     oMaster.PrinterConnectionAdd strPrinter
  119.  
  120.     if Err.Number = kErrorSuccess then
  121.  
  122.         wscript.echo "Connected to printer """ & strPrinter & """ "
  123.         
  124.         iRetval = kErrorSuccess
  125.  
  126.     else
  127.  
  128.         wscript.echo "Unable to add printer connection """ & strPrinter & _
  129.                      """, error: 0x" & Hex(Err.Number) & ". " & Err.Description
  130.       
  131.         Err.Clear  
  132.         
  133.         iRetval = kErrorFailure
  134.  
  135.     end if
  136.     
  137.     AddPrinterConnection = iRetval
  138.  
  139. end function
  140.  
  141. '
  142. ' List the printers and make the connections
  143. '
  144. function AddAllPrinters(strServer)
  145.  
  146.     on error resume next
  147.     
  148.     DebugPrint kDebugTrace, "In AddAllPrinters"
  149.  
  150.     dim oMaster
  151.     dim oPrinter
  152.     dim oError
  153.     dim iCount
  154.  
  155.     set oMaster = CreateObject("PrintMaster.PrintMaster.1")
  156.     
  157.     iCount = 0
  158.     
  159.     for each oPrinter in oMaster.Printers(strServer)
  160.  
  161.         if Err.Number = kErrorSuccess then 
  162.         
  163.             if AddPrinterConnection(oPrinter.PrinterName) = kErrorSuccess then
  164.            
  165.                 '
  166.                 ' Count the number of printer connections that were made
  167.                 '
  168.                 iCount = iCount + 1
  169.                
  170.             end if
  171.             
  172.         else
  173.         
  174.             wscript.echo "Unable to enumerate printers on server, error: 0x" _
  175.                          & Hex(Err.Number) & ". " & Err.Description
  176.         
  177.             AddAllPrinters = kErrorFailure
  178.  
  179.         
  180.             exit function
  181.             
  182.         end if    
  183.  
  184.     next
  185.             
  186.     if iCount = 0 then
  187.         
  188.         wscript.echo "There were no printers to connect to"
  189.             
  190.     else
  191.         
  192.         wscript.echo "Number of connections made " & iCount 
  193.         
  194.     end if    
  195.         
  196.     AddAllPrinters = kErrorSuccess
  197.     
  198. end function
  199.  
  200. '
  201. ' Debug display helper function
  202. '
  203. sub DebugPrint(uFlags, strString)
  204.  
  205.     if gDebugFlag = true then
  206.  
  207.         if uFlags = kDebugTrace then
  208.  
  209.             wscript.echo "Debug: " & strString
  210.  
  211.         end if
  212.  
  213.         if uFlags = kDebugError then
  214.  
  215.             if Err <> 0 then
  216.  
  217.                 wscript.echo "Debug: " & strString & " Failed with " & Hex(Err)
  218.  
  219.             end if
  220.  
  221.         end if
  222.  
  223.     end if
  224.  
  225. end sub
  226.  
  227. '
  228. ' Parse the command line into it's components
  229. '
  230. function ParseCommandLine(iAction, strServer)
  231.  
  232.     on error resume next    
  233.  
  234.     DebugPrint kDebugTrace, "In the ParseCommandLine"
  235.  
  236.     dim oArgs
  237.     dim strArg
  238.     dim iIndex
  239.  
  240.     set oArgs = wscript.Arguments
  241.  
  242.     iAction = kActionUnknown
  243.     iIndex = 0
  244.  
  245.     while iIndex < oArgs.Count
  246.  
  247.         select case oArgs(iIndex)
  248.  
  249.             case "-a"
  250.                 iAction = kActionConnect
  251.  
  252.             case "-c"
  253.                 iIndex = iIndex + 1
  254.                 strServer = oArgs(iIndex)
  255.  
  256.             case "-?"
  257.                 Usage(true)
  258.                 exit function
  259.  
  260.             case else
  261.                 Usage(true)
  262.                 exit function
  263.  
  264.         end select
  265.  
  266.         iIndex = iIndex + 1
  267.  
  268.     wend
  269.  
  270.     if Err.Number = kErrorSuccess then
  271.  
  272.         ParseCommandLine = kErrorSuccess
  273.  
  274.     else
  275.  
  276.         wscript.echo "Unable to parse command line, error 0x" & _
  277.                      Hex(Err.Number) & ". " & Err.Description
  278.         
  279.         ParseCommandLine = KErrorFailure
  280.         
  281.     end if
  282.  
  283. end  function
  284.  
  285. '
  286. ' Display command usage.
  287. '
  288. sub Usage(bExit)
  289.  
  290.     wscript.echo "Usage: conall [-a?][-c server]"
  291.     wscript.echo "Arguments:"
  292.     wscript.echo "-a     - connect to all printers"
  293.     wscript.echo "-c     - server name"
  294.     wscript.echo ""
  295.     wscript.echo "Examples:"
  296.     wscript.echo "conall -a -c \\server"
  297.  
  298.     if bExit then
  299.     
  300.         wscript.quit(1)
  301.         
  302.     end if
  303.  
  304. end sub
  305.  
  306. '
  307. ' Determines which program is used to run this script. 
  308. ' Returns true if the script host is cscript.exe
  309. '
  310. function IsHostCscript()
  311.  
  312.     on error resume next
  313.     
  314.     dim strFullName 
  315.     dim strCommand 
  316.     dim i, j 
  317.     dim bReturn
  318.     
  319.     bReturn = false
  320.     
  321.     strFullName = WScript.FullName
  322.     
  323.     i = InStr(1, strFullName, ".exe", 1)
  324.     
  325.     if i <> 0 then
  326.         
  327.         j = InStrRev(strFullName, "\", i, 1)
  328.         
  329.         if j <> 0 then
  330.             
  331.             strCommand = Mid(strFullName, j+1, i-j-1)
  332.             
  333.             if LCase(strCommand) = "cscript" then
  334.             
  335.                 bReturn = true  
  336.             
  337.             end if    
  338.                 
  339.         end if
  340.         
  341.     end if
  342.     
  343.     if Err <> 0 then
  344.     
  345.         call wscript.echo("Error 0x" & hex(Err.Number) & " occurred. " & Err.Description _
  346.                           & ". " & vbCRLF & "The scripting host could not be determined.")       
  347.         
  348.     end if
  349.     
  350.     IsHostCscript = bReturn
  351.  
  352. end function
  353.