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

  1. '----------------------------------------------------------------------
  2. '
  3. ' Copyright (c) Microsoft Corporation 1998-1999
  4. ' All Rights Reserved
  5. '
  6. ' Abstract:
  7. '
  8. ' defprn.vbs - default printer script for Windows 2000
  9. '
  10. ' Usage:
  11. ' defprn [-sg?] [-n printer]
  12. '
  13. ' Example:
  14. ' defprn -s -n printer
  15. ' defprn -g
  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 kActionSet        = 1
  46. const kActionGet        = 2
  47.  
  48. const kErrorSuccess     = 0
  49. const KErrorFailure     = 1
  50.  
  51. main
  52.  
  53. '
  54. ' Main execution starts here
  55. '
  56. sub main
  57.  
  58.     dim iAction
  59.     dim iRetval
  60.     dim strPrinterName
  61.     
  62.     '
  63.     ' Abort if the host is not cscript
  64.     '
  65.     if not IsHostCscript() then
  66.    
  67.         call wscript.echo(kMessage1 & vbCRLF & kMessage2 & vbCRLF & _
  68.                           kMessage3 & vbCRLF & kMessage4 & vbCRLF & _
  69.                           kMessage5 & vbCRLF & kMessage6 & vbCRLF)
  70.         
  71.         wscript.quit
  72.    
  73.     end if
  74.  
  75.     iRetval = ParseCommandLine(iAction, strPrinterName)
  76.     
  77.     if iRetval = kErrorSuccess then
  78.  
  79.         select case iAction
  80.  
  81.             case kActionSet
  82.                 iRetval = SetDefaultPrinter(strPrinterName)
  83.  
  84.             case kActionGet
  85.                 iRetval = GetDefaultPrinter()
  86.  
  87.             case else
  88.                 Usage(True)
  89.                 exit sub
  90.  
  91.         end select
  92.  
  93.     end if
  94.  
  95. end sub
  96.  
  97. '
  98. ' Get the name of the default printer if one exists.
  99. '
  100. function GetDefaultPrinter()
  101.  
  102.     on error resume next
  103.  
  104.     DebugPrint kDebugTrace, "In the GetDefaultPrinter"
  105.  
  106.     dim strDefaultPrinter
  107.     dim oPrint
  108.     dim iRetval
  109.  
  110.     set oPrint = CreateObject("PrintMaster.PrintMaster.1")
  111.  
  112.     strDefaultPrinter = oPrint.DefaultPrinter
  113.  
  114.     if Err.Number = kErrorSuccess then
  115.  
  116.         wscript.echo "The default printer is: """ & strDefaultPrinter & """ "
  117.  
  118.         iRetval = kErrorSuccess
  119.  
  120.     else
  121.  
  122.         wscript.echo "Unable to get the default printer, error: 0x" & Hex(Err.Number) & ". " & Err.Description
  123.  
  124.         iRetval = kErrorFailure
  125.  
  126.     end if
  127.  
  128.     GetDefaultPrinter = iRetval
  129.  
  130. end function
  131.  
  132. '
  133. ' Set the specified printer name as the default printer.
  134. '
  135. function SetDefaultPrinter(strName)
  136.  
  137.     on error resume next
  138.  
  139.     DebugPrint kDebugTrace, "In the SetDefaultPrinter " & strName
  140.  
  141.     dim oPrint
  142.     dim iRetval
  143.  
  144.     set oPrint = CreateObject("PrintMaster.PrintMaster.1")
  145.    
  146.     oPrint.DefaultPrinter = strName
  147.  
  148.     if Err.Number = kErrorSuccess then
  149.  
  150.         wscript.echo "The default printer was set to: """ & strName & """ "
  151.  
  152.         iRetval = kErrorSuccess
  153.  
  154.     else
  155.  
  156.         wscript.echo "Unable to set the default printer, error: 0x" & Hex(Err.Number) & ". " & Err.Description
  157.  
  158.         iRetval = kErrorFailure
  159.  
  160.     end if
  161.  
  162.     SetDefaultPrinter = iRetval
  163.  
  164. end function
  165.  
  166. '
  167. ' Debug display helper function
  168. '
  169. sub DebugPrint(uFlags, strString)
  170.  
  171.     if gDebugFlag = true then
  172.  
  173.         if uFlags = kDebugTrace then
  174.  
  175.             wscript.echo "Debug: " & strString
  176.  
  177.         end if
  178.  
  179.         if uFlags = kDebugError then
  180.  
  181.             if Err <> 0 then
  182.  
  183.                 wscript.echo "Debug: " & strString & " Failed with " & Hex(Err)
  184.  
  185.             end if
  186.  
  187.         end if
  188.  
  189.     end if
  190.  
  191. end sub
  192.  
  193. '
  194. ' Parse the command line into it's components
  195. '
  196. function ParseCommandLine(iAction, strPrinterName)
  197.  
  198.     on error resume next
  199.  
  200.     DebugPrint kDebugTrace, "In the ParseCommandLine"
  201.  
  202.     dim oArgs
  203.     dim iIndex
  204.  
  205.     iAction = kActionUnknown
  206.     iIndex = 0
  207.  
  208.     set oArgs = wscript.Arguments
  209.  
  210.     while iIndex < oArgs.Count
  211.  
  212.         select case oArgs(iIndex)
  213.  
  214.             case "-s"
  215.                 iAction = kActionSet
  216.  
  217.             case "-g"
  218.                 iAction = kActionGet
  219.  
  220.             case "-n"
  221.                 iIndex = iIndex + 1
  222.                 strPrinterName = oArgs(iIndex)
  223.  
  224.             case "-?"
  225.                 Usage(true)
  226.                 exit function
  227.  
  228.             case else
  229.                 Usage(true)
  230.                 exit function
  231.  
  232.         end select
  233.  
  234.         iIndex = iIndex + 1
  235.  
  236.     wend
  237.  
  238.     if Err.Number = kErrorSuccess then
  239.  
  240.         ParseCommandLine = kErrorSuccess
  241.     
  242.     else 
  243.                                                
  244.         wscript.echo "Unable to parse command line, error 0x" & Hex(Err.Number) & ". " & Err.Description 
  245.         
  246.         ParseCommandLine = kErrorFailure
  247.  
  248.     end if
  249.     
  250. end  function
  251.  
  252. '
  253. ' Display command usage.
  254. '
  255. sub Usage(bExit)
  256.  
  257.     wscript.echo "Usage: defprn [-sg?] [-n printer]"
  258.     wscript.echo "Arguments:"
  259.     wscript.echo "-s     - set the specified printer as the default printer"
  260.     wscript.echo "-g     - get the current default printer"
  261.     wscript.echo "-n     - printer name"    
  262.     wscript.echo ""
  263.     wscript.echo "Examples:"
  264.     wscript.echo "defprn -s -n printer"
  265.     wscript.echo "defprn -g"
  266.  
  267.     if bExit then
  268.     
  269.         wscript.quit(1)
  270.         
  271.     end if
  272.  
  273. end sub
  274.  
  275. '
  276. ' Determines which program is used to run this script. 
  277. ' Returns true if the script host is cscript.exe
  278. '
  279. function IsHostCscript()
  280.  
  281.     on error resume next
  282.     
  283.     dim strFullName 
  284.     dim strCommand 
  285.     dim i, j 
  286.     dim bReturn
  287.     
  288.     bReturn = false
  289.     
  290.     strFullName = WScript.FullName
  291.     
  292.     i = InStr(1, strFullName, ".exe", 1)
  293.     
  294.     if i <> 0 then
  295.         
  296.         j = InStrRev(strFullName, "\", i, 1)
  297.         
  298.         if j <> 0 then
  299.             
  300.             strCommand = Mid(strFullName, j+1, i-j-1)
  301.             
  302.             if LCase(strCommand) = "cscript" then
  303.             
  304.                 bReturn = true  
  305.             
  306.             end if    
  307.                 
  308.         end if
  309.         
  310.     end if
  311.     
  312.     if Err <> 0 then
  313.     
  314.         call wscript.echo("Error 0x" & hex(Err.Number) & " occurred. " & Err.Description _
  315.                           & ". " & vbCRLF & "The scripting host could not be determined.")       
  316.         
  317.     end if
  318.     
  319.     IsHostCscript = bReturn
  320.  
  321. end function
  322.  
  323.