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

  1. '----------------------------------------------------------------------
  2. '
  3. ' Copyright (c) Microsoft Corporation 1998-1999
  4. ' All Rights Reserved
  5. '
  6. ' Abstract:
  7. '
  8. ' prnmgr.vbs - printer script for Windows 2000
  9. '
  10. ' Usage:
  11. ' prnmgr [-adl?][c] [-c server][-b printer][-m driver]
  12. '                   [-l driver path][-r port][-f file]
  13. ' Examples:
  14. ' prnmgr -l -c \\server
  15. ' prnmgr.vbs -a -b "printer" -m "driver" -r "lpt1:"
  16. ' prnmgr.vbs -a -b "printer" -m "driver" -r "lpt1:"
  17. ' prnmgr.vbs -a -b "printer" -c \\server
  18. ' prnmgr.vbs -ac -b "\\server\printer"
  19. ' prnmgr.vbs -dc -b "\\server\printer"
  20. '
  21. '----------------------------------------------------------------------
  22.  
  23. option explicit
  24.  
  25. '
  26. ' Debugging trace flags, to enable debug output trace message
  27. ' change gDebugFlag to true.
  28. '
  29. const kDebugTrace = 1
  30. const kDebugError = 2
  31. dim   gDebugFlag
  32.  
  33. gDebugFlag = false
  34.  
  35. '
  36. ' Messages to be displayed if the scripting host is not cscript
  37. '                            
  38. const kMessage1 = "Please run this script using CScript."  
  39. const kMessage2 = "This can be achieved by"
  40. const kMessage3 = "1. Using ""CScript script.vbs arguments"" or" 
  41. const kMessage4 = "2. Changing the default Windows Scripting Host to CScript"
  42. const kMessage5 = "   using ""CScript //H:CScript //S"" and running the script "
  43. const kMessage6 = "   ""script.vbs arguments""."
  44.  
  45. '
  46. ' Operation action values.
  47. '
  48. const kActionUnknown     = 0
  49. const kActionAdd         = 1
  50. const kActionAddConn     = 2
  51. const kActionDel         = 3
  52. const kActionDelConn     = 4
  53. const kActionList        = 5
  54. const kActionDelAll      = 6
  55. const kActionDelConnAll  = 7
  56.  
  57. const kErrorSuccess      = 0
  58. const KErrorFailure      = 1
  59.  
  60. const kPrinterNetwork    = 16                             
  61. const kLocalPrinterFlag  = 64
  62.  
  63. main
  64.  
  65. '
  66. ' Main execution starts here
  67. '
  68. sub main
  69.  
  70.     dim iAction
  71.     dim iRetval
  72.     dim strServer
  73.     dim strPrinter
  74.     dim strDriverPath
  75.     dim strDriver
  76.     dim strPort
  77.     dim strInfFile
  78.     
  79.     '
  80.     ' Abort if the host is not cscript
  81.     '
  82.     if not IsHostCscript() then
  83.    
  84.         call wscript.echo(kMessage1 & vbCRLF & kMessage2 & vbCRLF & _
  85.                           kMessage3 & vbCRLF & kMessage4 & vbCRLF & _
  86.                           kMessage5 & vbCRLF & kMessage6 & vbCRLF)
  87.         
  88.         wscript.quit
  89.    
  90.     end if
  91.  
  92.     iRetval = ParseCommandLine(iAction, strServer, strPrinter, strDriver, strDriverPath, strPort, strInfFile)
  93.  
  94.     if iRetval = kErrorSuccess then
  95.  
  96.         select case iAction
  97.  
  98.             case kActionAdd
  99.                  iRetval = AddPrinter(strServer, strPrinter, strDriver, strDriverPath, strPort, strInfFile)
  100.  
  101.             case kActionAddConn
  102.                  iRetval = AddPrinterConnection(strPrinter)
  103.               
  104.             case kActionDel
  105.                  iRetval = DelPrinter(strServer, strPrinter)
  106.  
  107.             case kActionDelConn
  108.                  iRetval = DelPrinterConnection(strPrinter)
  109.             
  110.             case kActionList
  111.                  iRetval = ListPrinters(strServer)
  112.                  
  113.             case kActionDelAll
  114.                  iRetval = DelPrinterAll(strServer)
  115.  
  116.             case kActionDelConnAll
  117.                  iRetval = DelPrinterConnectionAll()
  118.  
  119.             case else
  120.                  Usage(true)
  121.                  exit sub
  122.  
  123.         end select
  124.  
  125.     end if
  126.  
  127. end sub
  128.  
  129. '
  130. ' Add a printer
  131. '
  132. function AddPrinter(strServer, strPrinter, strDriver, strDriverPath, strPort, strInfFile)
  133.     
  134.     on error resume next    
  135.  
  136.     DebugPrint kDebugTrace, "In AddPrinter"
  137.     DebugPrint kDebugTrace, "Server      "  & strServer
  138.     DebugPrint kDebugTrace, "Printer     "  & strPrinter
  139.     DebugPrint kDebugTrace, "Driver      "  & strDriver
  140.     DebugPrint kDebugTrace, "DrivatePath "  & strDriverPath
  141.     DebugPrint kDebugTrace, "Port        "  & strPort
  142.     DebugPrint kDebugTrace, "InfFile     "  & strInfFile
  143.  
  144.     dim oMaster
  145.     dim oPrinter
  146.     dim iRetval
  147.     
  148.     set oMaster  = CreateObject("PrintMaster.PrintMaster.1")
  149.     set oPrinter = CreateObject("Printer.Printer.1")
  150.  
  151.     oPrinter.ServerName  = strServer
  152.  
  153.     oPrinter.DriverPath  = strDriverPath
  154.  
  155.     oPrinter.DriverName  = strDriver
  156.  
  157.     oPrinter.PortName    = strPort
  158.  
  159.     oPrinter.PrinterName = strPrinter
  160.     
  161.     oPrinter.InfFile     = strInfFile
  162.  
  163.     oMaster.PrinterAdd oPrinter
  164.  
  165.     if Err.Number = kErrorSuccess then
  166.  
  167.         wscript.echo "Printer """ & strPrinter & """ added"
  168.         
  169.         iRetval = kErrorSuccess
  170.  
  171.     else
  172.  
  173.         wscript.echo "Unable to add printer """ & strPrinter & """, error: 0x" _ 
  174.                      & Hex(Err.Number) & ". " & Err.Description
  175.                      
  176.        iRetval = kErrorFailure                     
  177.                                       
  178.     end if
  179.     
  180.     AddPrinter = iRetval
  181.  
  182. end function
  183.  
  184. '
  185. ' Add a printer connection
  186. '
  187. function AddPrinterConnection(strPrinter)
  188.  
  189.     on error resume next
  190.  
  191.     DebugPrint kDebugTrace, "In AddPrinterConnection"
  192.  
  193.     dim oMaster
  194.     dim iRetval
  195.     
  196.     set oMaster = CreateObject("PrintMaster.PrintMaster.1")
  197.  
  198.     oMaster.PrinterConnectionAdd strPrinter
  199.  
  200.     if Err.Number = kErrorSuccess then
  201.  
  202.         wscript.echo "Printer connection """ & strPrinter & """ added"
  203.         
  204.         iRetval = kErrorSuccess
  205.  
  206.     else
  207.  
  208.         wscript.echo "Unable to add printer connection, error: 0x" & Hex(Err.Number) _ 
  209.                      & ". " & Err.Description
  210.                      
  211.         iRetval = kErrorFailure             
  212.         
  213.     end if
  214.     
  215.     AddPrinterConnection = iRetval
  216.  
  217. end function
  218.  
  219. '
  220. ' Delete a printer connection
  221. '
  222. function DelPrinterConnection(strPrinter)
  223.  
  224.     on error resume next
  225.  
  226.     DebugPrint kDebugTrace, "In DelPrinterConnection"
  227.  
  228.     dim oMaster
  229.     dim iRetval
  230.     
  231.     set oMaster = CreateObject("PrintMaster.PrintMaster.1")
  232.  
  233.     oMaster.PrinterConnectionDel strPrinter
  234.  
  235.     if Err.Number = kErrorSuccess then
  236.  
  237.         wscript.echo "Deleted printer connection " & strPrinter
  238.         
  239.         iRetval = kErrorSuccess
  240.  
  241.     else
  242.  
  243.         wscript.echo "Unable to delete printer connection " & strPrinter & ", error: 0x"_
  244.                      & Hex(Err.Number) & ". " & Err.Description
  245.                      
  246.         iRetval = kErrorFailure             
  247.         
  248.     end if
  249.     
  250.     DelPrinterConnection = iRetval
  251.  
  252. end function
  253.  
  254. '
  255. ' Delete a printer
  256. '
  257. function DelPrinter(strServer, strPrinter)
  258.  
  259.     on error resume next
  260.  
  261.     DebugPrint kDebugTrace, "In DelPrinter"
  262.  
  263.     dim oMaster
  264.     dim oPrinter
  265.     dim iRetval
  266.     
  267.     set oMaster  = CreateObject("PrintMaster.PrintMaster.1")
  268.     set oPrinter = CreateObject("Printer.Printer.1")
  269.     
  270.     oPrinter.ServerName  = strServer 
  271.     oPrinter.PrinterName = strPrinter
  272.     
  273.     oMaster.PrinterDel oPrinter
  274.  
  275.     if Err.Number = kErrorSuccess then
  276.  
  277.         wscript.echo "Printer """ & strPrinter & """ deleted"
  278.         
  279.         iRetval = kErrorSuccess
  280.  
  281.     else
  282.  
  283.         wscript.echo "Unable to delete printer """ & strPrinter &  """, error: 0x" _ 
  284.                      & Hex(Err.Number) & ". " & Err.Description
  285.                      
  286.         iRetval = kErrorFailure             
  287.         
  288.     end if
  289.     
  290.     DelPrinter = iRetval
  291.  
  292. end function
  293.  
  294. '
  295. ' List the printers
  296. '
  297. function ListPrinters(strServer)
  298.  
  299.     on error resume next
  300.     
  301.     DebugPrint kDebugTrace, "In ListPrinter"
  302.  
  303.     dim oMaster
  304.     dim oPrinter
  305.     dim iRetval
  306.     
  307.     set oMaster = CreateObject("PrintMaster.PrintMaster.1")
  308.     
  309.     for each oPrinter in oMaster.Printers(strServer)
  310.  
  311.         if Err.Number = kErrorSuccess then 
  312.             
  313.             wscript.echo ""
  314.             wscript.echo "ServerName      : " & oPrinter.ServerName
  315.             wscript.echo "PrinterName     : " & oPrinter.PrinterName
  316.             wscript.echo "ShareName       : " & oPrinter.ShareName
  317.             wscript.echo "DriverName      : " & oPrinter.DriverName
  318.             wscript.echo "PortName        : " & oPrinter.PortName
  319.             wscript.echo "Comment         : " & oPrinter.Comment
  320.             wscript.echo "Location        : " & oPrinter.Location
  321.             wscript.echo "SepFile         : " & oPrinter.SepFile
  322.             wscript.echo "PrintProcesor   : " & oPrinter.PrintProcessor
  323.             wscript.echo "DataType        : " & oPrinter.DataType
  324.             wscript.echo "Parameters      : " & oPrinter.Parameters
  325.             wscript.echo "Attributes      : " & CSTR(oPrinter.Attributes)
  326.             wscript.echo "Priority        : " & CSTR(oPrinter.Priority)
  327.             wscript.echo "DefaultPriority : " & CStr(oPrinter.DefaultPriority)
  328.             wscript.echo "StartTime       : " & CStr(oPrinter.StartTime)
  329.             wscript.echo "UntilTime       : " & CStr(oPrinter.UntilTime)
  330.             wscript.echo "Status          : " & CStr(oPrinter.Status)
  331.             wscript.echo "Jobc Count      : " & CStr(oPrinter.Jobs)
  332.             wscript.echo "AveragePPM      : " & CStr(oPrinter.AveragePPM)
  333.             
  334.             Err.Clear
  335.             
  336.         else    
  337.         
  338.             wscript.echo "Unable to list printers, error: 0x" & _
  339.                          Hex(Err.Number) & ". " & Err.Description
  340.  
  341.             ListPrinters = kErrorFailure
  342.             
  343.             exit function
  344.             
  345.         end if
  346.                          
  347.     next
  348.  
  349.     wscript.echo "Success listing printers"
  350.     
  351.     ListPrinters = kErrorSuccess
  352.  
  353. end function
  354.  
  355. '
  356. ' Delete all local printers
  357. '
  358. function DelPrinterAll(strServer)
  359.  
  360.     on error resume next
  361.  
  362.     DebugPrint kDebugTrace, "In DelPrinterAll"
  363.  
  364.     dim oMaster
  365.     dim oPrinter
  366.     dim iTotal
  367.     dim iCount
  368.     
  369.     '
  370.     ' Number of local printers found
  371.     '
  372.     iTotal = 0
  373.     
  374.     '
  375.     ' Number of local printes deleted
  376.     '
  377.     iCount = 0
  378.     
  379.     set oMaster = CreateObject("PrintMaster.PrintMaster.1")
  380.  
  381.     for each oPrinter in oMaster.Printers(strServer)
  382.         
  383.         if Err.Number = kErrorSuccess then
  384.             
  385.             '
  386.             ' If strServer is not empty, the enumeration will contain 
  387.             ' only local printers on that server. The reason is that 
  388.             ' connections are per user resources 
  389.             '
  390.             if strServer = "" then
  391.                
  392.                 '
  393.                 ' Connections are enumerated in this case
  394.                 '
  395.                 if (oPrinter.Attributes and kLocalPrinterFlag) = kLocalPrinterFlag then
  396.    
  397.                     iTotal = iTotal + 1
  398.                     
  399.                     oMaster.PrinterPurge strServer, oPrinter.PrinterName 
  400.                 
  401.                     '
  402.                     ' In some cases, we can delete a printer, but not purge it 
  403.                     ' We need to clear the error
  404.                     '
  405.                     Err.Clear
  406.                     
  407.                     if DelPrinter(strServer, oPrinter.PrinterName) = kErrorSuccess then
  408.                     
  409.                         iCount = iCount + 1  
  410.                     
  411.                     end if
  412.                    
  413.                 end if
  414.                
  415.             else
  416.            
  417.                 '
  418.                 ' Only local printers on the server are enumerated
  419.                 '    
  420.                 iTotal = iTotal + 1
  421.                 
  422.                 oMaster.PrinterPurge strServer, oPrinter.PrinterName 
  423.                 
  424.                 '
  425.                 ' In some cases, we can delete a printer, but not purge it
  426.                 ' We need to clear the error
  427.                 '
  428.                 Err.Clear
  429.                 
  430.                 if DelPrinter(strServer, oPrinter.PrinterName) = kErrorSuccess then
  431.                 
  432.                     iCount = iCount + 1
  433.                 
  434.                 end if
  435.                                                                  
  436.             end if
  437.            
  438.             Err.Clear
  439.             
  440.         else
  441.         
  442.             wscript.echo "Unable to enumerate printers on server, error: 0x" _
  443.                          & Hex(Err.Number) & ". " & Err.Description
  444.         
  445.             DelPrinterAll = kErrorFailure    
  446.             
  447.             exit function
  448.             
  449.         end if    
  450.  
  451.     next
  452.  
  453.     wscript.echo "Number of local printers found " & iTotal & ". Printers deleted " & iCount
  454.         
  455.     DelPrinterAll = kErrorSuccess
  456.  
  457. end function
  458.  
  459. '
  460. ' Delete all printer connections
  461. '
  462. function DelPrinterConnectionAll()
  463.     
  464.     on error resume next
  465.  
  466.     DebugPrint kDebugTrace, "In DelPrinterConnectionAll"
  467.  
  468.     dim oMaster
  469.     dim oPrinter
  470.     dim iTotal
  471.     dim iCount
  472.     
  473.     '
  474.     ' Total number of connections found
  475.     '
  476.     iTotal = 0
  477.     
  478.     '
  479.     ' Total number of connections deleted
  480.     '
  481.     iCount = 0
  482.     
  483.     set oMaster = CreateObject("PrintMaster.PrintMaster.1")
  484.  
  485.     for each oPrinter in oMaster.Printers
  486.  
  487.         if Err.Number = kErrorSuccess then
  488.         
  489.             '
  490.             ' Test if the printer is not local 
  491.             '    
  492.             if (oPrinter.Attributes and kLocalPrinterFlag) <> kLocalPrinterFlag then
  493.    
  494.                 iTotal = iTotal + 1
  495.                 
  496.                 if DelPrinterConnection(oPrinter.PrinterName) = kErrorSuccess then
  497.                 
  498.                     iCount = iCount + 1
  499.                     
  500.                 end if
  501.                                                                 
  502.                 Err.Clear           
  503.             
  504.             end if
  505.             
  506.         else
  507.         
  508.             wscript.echo "Unable to enumerate printers on server, error: 0x" _
  509.                          & Hex(Err.Number) & ". " & Err.Description    
  510.     
  511.             DelPrinterConnectionAll = kErrorFailure
  512.             
  513.             exit function
  514.                                                        
  515.         end if
  516.  
  517.     next
  518.     
  519.     wscript.echo "Number of connection found " & iTotal & ". Connections deleted " & iCount
  520.     
  521.     DelPrinterConnectionAll = kErrorSuccess
  522.  
  523. end function
  524.  
  525. '
  526. ' Debug display helper function
  527. '
  528. sub DebugPrint(uFlags, strString)
  529.  
  530.     if gDebugFlag = true then
  531.  
  532.         if uFlags = kDebugTrace then
  533.  
  534.             wscript.echo "Debug: " & strString
  535.  
  536.         end if
  537.  
  538.         if uFlags = kDebugError then
  539.  
  540.             if Err <> 0 then
  541.  
  542.                 wscript.echo "Debug: " & strString & " Failed with " & Hex(Err)
  543.  
  544.             end if
  545.  
  546.         end if
  547.  
  548.     end if
  549.  
  550. end sub
  551.  
  552. '
  553. ' Parse the command line into it's components
  554. '
  555. function ParseCommandLine(iAction, strServer, strPrinter, strDriver, strDriverPath, strPort, strInfFile)
  556.  
  557.     on error resume next    
  558.  
  559.     DebugPrint kDebugTrace, "In the ParseCommandLine"
  560.  
  561.     dim oArgs
  562.     dim iIndex
  563.  
  564.     iAction = kActionUnknown
  565.     iIndex  = 0
  566.  
  567.     set oArgs = wscript.Arguments
  568.  
  569.     while iIndex < oArgs.Count
  570.  
  571.         select case oArgs(iIndex)
  572.  
  573.             case "-a"
  574.                 iAction = kActionAdd
  575.  
  576.             case "-ac"
  577.                 iAction = kActionAddConn
  578.             
  579.             case "-d"
  580.                 iAction = kActionDel
  581.  
  582.             case "-dc"
  583.                 iAction = kActionDelConn
  584.             
  585.             case "-l"
  586.                 iAction = kActionList
  587.                 
  588.             case "-x"
  589.                 iAction = kActionDelAll
  590.  
  591.             case "-xc"
  592.                 iAction = kActionDelConnAll
  593.  
  594.             case "-c"
  595.                 iIndex = iIndex + 1
  596.                 strServer = oArgs(iIndex)
  597.  
  598.             case "-b"
  599.                 iIndex = iIndex + 1
  600.                 strPrinter = oArgs(iIndex)
  601.  
  602.             case "-f"
  603.                 iIndex = iIndex + 1
  604.                 strInfFile = oArgs(iIndex)                          
  605.                                       
  606.             case "-m"
  607.                 iIndex = iIndex + 1
  608.                 strDriver = oArgs(iIndex)
  609.  
  610.             case "-r"
  611.                 iIndex = iIndex + 1
  612.                 strPort = oArgs(iIndex)
  613.  
  614.             case "-p"
  615.                 iIndex = iIndex + 1
  616.                 strDriverPath = oArgs(iIndex)
  617.  
  618.             case "-?"
  619.                 Usage(true)
  620.                 exit function
  621.  
  622.             case else
  623.                 Usage(true)
  624.                 exit function
  625.  
  626.         end select
  627.  
  628.         iIndex = iIndex + 1
  629.  
  630.     wend    
  631.  
  632.     if Err = kErrorSuccess then
  633.  
  634.         ParseCommandLine = kErrorSuccess
  635.  
  636.     else
  637.     
  638.         wscript.echo "Unable to parse command line, error 0x" & _
  639.                      Hex(Err.Number) & ". " & Err.Description
  640.         
  641.         ParseCommandLine = kErrorFailure        
  642.  
  643.     end if    
  644.     
  645. end  function
  646.  
  647. '
  648. ' Display command usage.
  649. '
  650. sub Usage(bExit)
  651.  
  652.     wscript.echo "Usage: prnmgr [-adl?][c] [-c server][-b printer][-m driver model]"
  653.     wscript.echo "              [-p driver path][-r port][-f file]" 
  654.     wscript.echo "Arguments:"
  655.     wscript.echo "-a     - add local printer"
  656.     wscript.echo "-ac    - add printer connection"
  657.     wscript.echo "-d     - delete local printer"
  658.     wscript.echo "-dc    - delete printer connection"
  659.     wscript.echo "-f     - inf file"
  660.     wscript.echo "-l     - list printers"
  661.     wscript.echo "-c     - server name"
  662.     wscript.echo "-b     - print name"
  663.     wscript.echo "-m     - driver model"
  664.     wscript.echo "-r     - port name"
  665.     wscript.echo "-p     - dirver path can be local or network path i.e. a:\ or \\server\share"
  666.     wscript.echo "-x     - delete all local printers"
  667.     wscript.echo "-xc    - delete all printer connections, cannot be used with the -c option"
  668.     wscript.echo "-?     - display command usage"
  669.     wscript.echo ""
  670.     wscript.echo "Examples:"
  671.     wscript.echo "prnmgr -l -c \\server"
  672.     wscript.echo "prnmgr -a -b ""printer"" -m ""driver"" -r ""lpt1:""" 
  673.     wscript.echo "prnmgr -d -b ""printer"" -c \\server"
  674.     wscript.echo "prnmgr -ac -b ""\\server\printer"""
  675.     wscript.echo "prnmgr -dc -b ""\\server\printer"""
  676.     wscript.echo "prnmgr -x"
  677.     wscript.echo "prnmgr -x -c \\server"
  678.     wscript.echo "prnmgr -xc"
  679.  
  680.     if bExit then
  681.     
  682.         wscript.quit(1)
  683.         
  684.     end if
  685.  
  686. end sub
  687.  
  688. '
  689. ' Determines which program is used to run this script. 
  690. ' Returns true if the script host is cscript.exe
  691. '
  692. function IsHostCscript()
  693.  
  694.     on error resume next
  695.     
  696.     dim strFullName 
  697.     dim strCommand 
  698.     dim i, j 
  699.     dim bReturn
  700.     
  701.     bReturn = false
  702.     
  703.     strFullName = WScript.FullName
  704.     
  705.     i = InStr(1, strFullName, ".exe", 1)
  706.     
  707.     if i <> 0 then
  708.         
  709.         j = InStrRev(strFullName, "\", i, 1)
  710.         
  711.         if j <> 0 then
  712.             
  713.             strCommand = Mid(strFullName, j+1, i-j-1)
  714.             
  715.             if LCase(strCommand) = "cscript" then
  716.             
  717.                 bReturn = true  
  718.             
  719.             end if    
  720.                 
  721.         end if
  722.         
  723.     end if
  724.     
  725.     if Err <> 0 then
  726.     
  727.         call wscript.echo("Error 0x" & hex(Err.Number) & " occurred. " & Err.Description _
  728.                           & ". " & vbCRLF & "The scripting host could not be determined.")       
  729.         
  730.     end if
  731.     
  732.     IsHostCscript = bReturn
  733.  
  734. end function
  735.  
  736.