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

  1. '----------------------------------------------------------------------
  2. '
  3. ' Copyright (c) Microsoft Corporation 1998-1999
  4. ' All Rights Reserved
  5. '
  6. ' Abstract:
  7. '
  8. ' portconv.vbs - Script for converting lpr ports to tcp ports
  9. '
  10. ' Usage:
  11. ' portconv [-ag?][-p port][-c source server][-i ip][-d destination server]
  12. '
  13. ' Examples:
  14. ' portconv -g -i 1.2.3.4
  15. '
  16. '----------------------------------------------------------------------
  17.  
  18. option explicit
  19.  
  20. '
  21. ' Debugging trace flags, to enable debug output trace message
  22. ' change gDebugFlag to true.
  23. '
  24. const kDebugTrace = 1
  25. const kDebugError = 2
  26. dim   gDebugFlag
  27.  
  28. gDebugFlag = false
  29.  
  30. '
  31. ' Messages to be displayed if the scripting host is not cscript
  32. '                            
  33. const kMessage1 = "Please run this script using CScript."  
  34. const kMessage2 = "This can be achieved by"
  35. const kMessage3 = "1. Using ""CScript script.vbs arguments"" or" 
  36. const kMessage4 = "2. Changing the default Windows Scripting Host to CScript"
  37. const kMessage5 = "   using ""CScript //H:CScript //S"" and running the script "
  38. const kMessage6 = "   ""script.vbs arguments""."
  39.  
  40. '
  41. ' Operation action values.
  42. '
  43. const kActionUnknown    = 0
  44. const kActionAdd        = 1
  45. const kActionGet        = 2
  46. const kActionConvertAll = 3
  47.  
  48. const kErrorSuccess = 0
  49. const KErrorFailure = 1
  50.  
  51. '
  52. ' Port Types
  53. '
  54. const kTcpRaw   = 1
  55. const kTcpLPr   = 2
  56. const kLocal    = 3
  57. const kLprMon   = 5
  58. const kHPdlc    = 7
  59. const kUnknown  = 8
  60.  
  61. '
  62. ' The only supported conversion is from lpr mon to tcp
  63. '
  64. const kLprToTcp = 1
  65.  
  66. main
  67.  
  68. '
  69. ' Main execution starts here
  70. '
  71. sub main
  72.  
  73.     dim iAction
  74.     dim iRetval
  75.     dim strIPAddress 
  76.     dim strSourceServer 
  77.     dim strDestServer 
  78.     dim strPort
  79.     
  80.     '
  81.     ' Abort if the host is not cscript
  82.     '
  83.     if not IsHostCscript() then
  84.    
  85.         call wscript.echo(kMessage1 & vbCRLF & kMessage2 & vbCRLF & _
  86.                           kMessage3 & vbCRLF & kMessage4 & vbCRLF & _
  87.                           kMessage5 & vbCRLF & kMessage6 & vbCRLF)
  88.         
  89.         wscript.quit
  90.    
  91.     end if
  92.  
  93.     iRetval = ParseCommandLine(iAction, strSourceServer, strDestServer, strPort, strIPAddress)
  94.  
  95.     if iRetval = kErrorSuccess then
  96.  
  97.         select case iAction
  98.  
  99.             case kActionAdd
  100.                  iRetval = AddEquivalentTCPPort(strSourceServer, strDestServer, strPort)
  101.                  
  102.             case kActionGet    
  103.                  iRetval = GetEquivalentTCPSettings(strIPAddress)
  104.                  
  105.             case kActionConvertAll
  106.                  iRetval = ConvertAll(strSourceServer, strDestServer)
  107.  
  108.             case else
  109.                  Usage(true)
  110.  
  111.         end select
  112.  
  113.     end if
  114.  
  115. end sub
  116.  
  117. '
  118. ' Get the TCP equivalent of a lpr mon port
  119. '
  120. function GetEquivalentTCPSettings(strIPAddress)
  121.  
  122.     on error resume next
  123.     
  124.     DebugPrint kDebugTrace, "In GetEquivalentTCPSettings"
  125.  
  126.     dim oMaster
  127.     dim oPort
  128.     dim iResult
  129.  
  130.     set oMaster = CreateObject("PrintMaster.PrintMaster.1")
  131.     set oPort   = CreateObject("Port.Port.1")
  132.     
  133.     oPort.HostAddress = strIPAddress
  134.                       
  135.     '
  136.     ' oPort will contain the settings of the device
  137.     '                                      
  138.     oMaster.PortConversion oPort, kLprToTcp
  139.  
  140.     if Err.Number <> kErrorSuccess then
  141.  
  142.         wscript.echo "Error converting the port, error 0x" & _
  143.                      Hex(Err.Number) & ". " & Err.Description
  144.         
  145.         iResult = kErrorFailure
  146.  
  147.     else
  148.     
  149.         '
  150.         ' Check if the device responded
  151.         ' 
  152.         if oPort.DeviceType <> "" then 
  153.         
  154.             wscript.echo "DeviceType  " & oPort.DeviceType
  155.             
  156.         else
  157.         
  158.             wscript.echo "The device did not respond. The default port settings will be displayed"
  159.             
  160.         end if    
  161.         
  162.         wscript.echo "Name        " & oPort.PortName
  163.         wscript.echo "HostAddress " & oPort.HostAddress
  164.         
  165.         if oPort.PortType = kTcpRaw then
  166.         
  167.             wscript.echo "Protocol    RAW"
  168.             
  169.             wscript.echo "PortNumber  " & oPort.PortNumber
  170.             
  171.         else
  172.         
  173.             wscript.echo "Protocol    LPR"
  174.         
  175.             wscript.echo "Queue       " & oPort.QueueName
  176.         
  177.         end if    
  178.         
  179.         if oPort.SNMP then
  180.         
  181.             wscript.echo "SNMP        Enabled"
  182.             
  183.         else
  184.             
  185.             wscript.echo "SNMP        Disabled"
  186.         
  187.         end if
  188.         
  189.         if oPort.DoubleSpool then
  190.         
  191.             wscript.echo "DoubleSpool Yes"
  192.             
  193.         else
  194.             
  195.             wscript.echo "DoubleSpool No"
  196.         
  197.         end if  
  198.         
  199.         iResult = kErrrorSuccess
  200.         
  201.     end if
  202.     
  203.     GetEquivalentTCPSettings = iResult
  204.     
  205. end function
  206.  
  207. '
  208. ' Add an equivalent tcp port. strSource is the server where strPort is on.
  209. ' If strPort is a lpr mon port, a corresponding tcp port will be added to
  210. ' the destination server strDestServer
  211. '
  212. function AddEquivalentTCPPort(strSourceServer, strDestServer, strPort)
  213.  
  214.     on error resume next
  215.     
  216.     DebugPrint kDebugTrace, "In AddEquivalentTCPPort"
  217.  
  218.     dim oMaster
  219.     dim oPort
  220.     dim iResult
  221.     
  222.     iResult = kErrorFailure
  223.  
  224.     set oMaster = CreateObject("PrintMaster.PrintMaster.1")
  225.     set oPort   = CreateObject("Port.Port.1")
  226.     
  227.     '
  228.     ' Get the configuration of the port to be converted
  229.     '
  230.     oMaster.PortGet strSourceServer, strPort, oPort
  231.  
  232.     if Err.Number <> kErrorSuccess then
  233.  
  234.         wscript.echo "Unable to get the configurate for the port """ & strPort & _
  235.                      """, error 0x" & Hex( Err.Number ) & " " & Err.Description
  236.                 
  237.     else
  238.  
  239.         '
  240.         ' Check if it is lpr mon port
  241.         '                                               
  242.         if oPort.PortType = kLprMon then
  243.         
  244.            '
  245.            ' Attempt to get the equivalent tcp port
  246.            '
  247.            oMaster.PortConversion oPort, kLprToTcp
  248.            
  249.            if Err.Number <> kErrorSuccess then 
  250.            
  251.                wscript.echo "Unable to convert the port, error: 0x" & Hex(Err.Number) _ 
  252.                             & " " & Err.Description
  253.                
  254.            else  
  255.            
  256.               ' 
  257.               ' An empty DeviceType means the device did not respond or couldn't be identified 
  258.               '
  259.               if oPort.DeviceType = "" then
  260.               
  261.                   wscript.echo "The device did not respond. A port with default settings will be added"
  262.                   
  263.               end if    
  264.               
  265.               oPort.ServerName = strDestServer
  266.               
  267.               '
  268.               ' Add the equivalent port
  269.               '                                     
  270.               oMaster.PortAdd oPort
  271.               
  272.               if Err.Number = kErrorSuccess then
  273.               
  274.                   wscript.echo "Success adding the TCP port: """ & oPort.PortName & """ on server " & strDestServer
  275.                   
  276.                   iResult = kErrorSuccess
  277.                   
  278.               else
  279.               
  280.                   wscript.echo "Unable to add the TCP port, error: 0x" & _
  281.                                Hex(Err.Number) & ". " & Err.Description
  282.                   
  283.               end if    
  284.            
  285.            end if
  286.            
  287.         else   
  288.         
  289.             wscript.echo "Error: This port is not lpr mon"              
  290.             
  291.         end if
  292.         
  293.     end if
  294.     
  295.     AddEquivalentTCPPort = iResult
  296.  
  297. end function
  298.  
  299. '
  300. ' Convert all lpr mon ports from the source server onto the destination server 
  301. '
  302. function ConvertAll(strSourceServer, strDestServer)
  303.  
  304.     on error resume next
  305.     
  306.     DebugPrint kDebugTrace, "In ConvertAll"
  307.  
  308.     dim oMaster
  309.     dim oPort
  310.     dim iTotal
  311.     dim iLprCount
  312.     dim iTcpCount
  313.     
  314.     '
  315.     ' Total number of ports on the source server
  316.     '
  317.     iTotal = 0
  318.     
  319.     '
  320.     ' Total number of lpr mon ports on the source server
  321.     '
  322.     iLprCount = 0
  323.     
  324.     '
  325.     ' Total number of equivalent tcp ports added on the destination server
  326.     '
  327.     iTcpCount = 0
  328.     
  329.     set oMaster = CreateObject("PrintMaster.PrintMaster.1")
  330.     
  331.     for each oPort in oMaster.Ports(strSourceServer)
  332.     
  333.         
  334.         if Err.Number = kErrorSuccess then 
  335.         
  336.             iTotal = iTotal + 1
  337.        
  338.             if oPort.PortType = kLprMon then
  339.               
  340.                 iLprCount = iLprCount + 1
  341.            
  342.                 oMaster.PortConversion oPort, kLprToTcp
  343.                
  344.                 if Err.Number = kErrorSuccess then
  345.                
  346.                     '
  347.                     ' Check if the device is responding
  348.                     '
  349.                     if oPort.DeviceType = "" then
  350.                    
  351.                         wscript.echo "The device " & oPort.HostAddress & _
  352.                                      " did not respond. Adding a port with default settings"
  353.                                     
  354.                         '
  355.                         ' Enable LPR byte counting
  356.                         '                         
  357.                         oPort.DoubleSpool = true
  358.                        
  359.                     else
  360.                    
  361.                         wscript.echo oPort.HostAddress & " is " & oPort.DeviceType
  362.                        
  363.                     end if    
  364.                    
  365.                     oPort.ServerName = strDestServer
  366.                     
  367.                     '
  368.                     ' Add the equivalent port
  369.                     '
  370.                     oMaster.PortAdd oPort
  371.                    
  372.                     if Err.Number = kErrorSuccess then
  373.                    
  374.                         iTcpCount = iTcpCount + 1
  375.                    
  376.                         wscript.echo oPort.PortName & " was added"
  377.                        
  378.                     else 
  379.                        
  380.                         wscript.echo "Unable to add """ & oPort.PortName & """, error: 0x" _ 
  381.                                       & Hex(Err.Number) & ". " & Err.Description
  382.                        
  383.                         Err.Clear
  384.                  
  385.                     end if
  386.                    
  387.                 else
  388.                
  389.                     wscript.echo "Unable to convert port """ & oPort.PortName & """ , error: 0x" _
  390.                                  & Hex(Err.Number) & ". " & Err.Description
  391.    
  392.                     Err.Clear
  393.                
  394.                 end if    
  395.            
  396.             end if
  397.            
  398.         else
  399.         
  400.             wscript.echo "Unable to list ports, error: 0x" & Hex(Err.Number) & ". " & Err.Description
  401.         
  402.             ConvertAll = kErrorFailure
  403.            
  404.             exit function
  405.         
  406.         end if   
  407.     
  408.     next
  409.     
  410.     wscript.echo "Number of ports on the source server                    " & iTotal
  411.     wscript.echo "Number of lpr mon ports on the source server            " & iLprCount
  412.     wscript.echo "Number of tcp ports added to the the destination server " & iTcpCount
  413.                 
  414.     ConvertAll = kErrorSuccess
  415.     
  416. end function
  417.  
  418. '
  419. ' Debug display helper function
  420. '
  421. sub DebugPrint(uFlags, strString)
  422.  
  423.     if gDebugFlag = true then
  424.  
  425.         if uFlags = kDebugTrace then
  426.  
  427.             wscript.echo "Debug: " & strString
  428.  
  429.         end if
  430.  
  431.         if uFlags = kDebugError then
  432.  
  433.             if Err <> 0 then
  434.  
  435.                 wscript.echo "Debug: " & strString & " Failed with " & Hex(Err)
  436.  
  437.             end if
  438.  
  439.         end if
  440.  
  441.     end if
  442.  
  443. end sub
  444.  
  445. '
  446. ' Parse the command line into it's components
  447. '
  448. function ParseCommandLine(iAction, strSourceServer, strDestServer, strPort, strIPAddress)
  449.  
  450.     on error resume next
  451.  
  452.     DebugPrint kDebugTrace, "In the ParseCommandLine"
  453.  
  454.     dim oArgs
  455.     dim strArg
  456.     dim iIndex 
  457.  
  458.     set oArgs = wscript.Arguments
  459.  
  460.     iAction = kActionUnknown
  461.     iIndex = 0
  462.  
  463.     while iIndex < oArgs.Count
  464.  
  465.         select case oArgs(iIndex)
  466.  
  467.             case "-a"
  468.                 iAction = kActionAdd
  469.                 
  470.             case "-g"
  471.                 iAction = kActionGet   
  472.                 
  473.             case "-w"
  474.                 iAction = kActionConvertAll           
  475.             
  476.             case "-i"
  477.                 iIndex = iIndex + 1
  478.                 strIPAddress = oArgs(iIndex)
  479.                 
  480.             case "-p"
  481.                 iIndex = iIndex + 1
  482.                 strPort = oArgs(iIndex)
  483.                 
  484.             case "-c"
  485.                 iIndex = iIndex + 1
  486.                 strSourceServer = oArgs(iIndex)
  487.                 
  488.             case "-d"
  489.                 iIndex = iIndex + 1
  490.                 strDestServer = oArgs(iIndex)    
  491.                 
  492.             case "-?"
  493.                 Usage(true)
  494.                 exit function
  495.  
  496.             case else
  497.                 Usage(true)
  498.                 exit function
  499.  
  500.         end select
  501.  
  502.         iIndex = iIndex + 1
  503.  
  504.     wend
  505.  
  506.     if Err.Number <> kErrorSuccess then
  507.  
  508.         wscript.echo "Unable to parse command line, error 0x" & _
  509.                      Hex(Err.Number) & " " & Err.Description
  510.         
  511.         ParseCommandLine = kErrorFailure
  512.  
  513.     else
  514.  
  515.         ParseCommandLine = kErrorSuccess
  516.  
  517.     end if
  518.  
  519. end  function
  520.  
  521. '
  522. ' Display command usage.
  523. '
  524. sub Usage(bExit)
  525.  
  526.     wscript.echo "Usage: portconv [-agw?][-p port][-c source server]"
  527.     wscript.echo "                       [-i ip][-d destination server]" 
  528.     wscript.echo "Arguments:"
  529.     wscript.echo "-a     - adds the equivalent tcp port for an lpr port"
  530.     wscript.echo "-g     - for an IP address, gets the preferred device settings" 
  531.     wscript.echo "-w     - convert all"
  532.     wscript.echo "-p     - port name"
  533.     wscript.echo "-c     - source server name"
  534.     wscript.echo "-i     - ip address of the device to get the settings of"
  535.     wscript.echo "-d     - destination server name, where the port will be added"  
  536.     wscript.echo ""
  537.     wscript.echo "Examples:"
  538.     wscript.echo "portconv -g -i 1.2.3.4" 
  539.     wscript.echo "portconv -a -p 1.2.3.4:Queue -c \\server"
  540.     wscript.echo "portconv -a -p 1.2.3.4:Queue -c \\server -d \\dest" 
  541.     wscript.echo "portconv -w -c \\server"
  542.     wscript.echo "portconv -w -c \\server -d \\dest"
  543.     wscript.echo "portconv -w -d \\dest"
  544.  
  545.     if bExit <> 0 then
  546.     
  547.         wscript.quit(1)
  548.         
  549.     end if
  550.  
  551. end sub
  552.  
  553. '
  554. ' Determines which program is used to run this script. 
  555. ' Returns true if the script host is cscript.exe
  556. '
  557. function IsHostCscript()
  558.  
  559.     on error resume next
  560.     
  561.     dim strFullName 
  562.     dim strCommand 
  563.     dim i, j 
  564.     dim bReturn
  565.     
  566.     bReturn = false
  567.     
  568.     strFullName = WScript.FullName
  569.     
  570.     i = InStr(1, strFullName, ".exe", 1)
  571.     
  572.     if i <> 0 then
  573.         
  574.         j = InStrRev(strFullName, "\", i, 1)
  575.         
  576.         if j <> 0 then
  577.             
  578.             strCommand = Mid(strFullName, j+1, i-j-1)
  579.             
  580.             if LCase(strCommand) = "cscript" then
  581.             
  582.                 bReturn = true  
  583.             
  584.             end if    
  585.                 
  586.         end if
  587.         
  588.     end if
  589.     
  590.     if Err <> 0 then
  591.     
  592.         call wscript.echo("Error 0x" & hex(Err.Number) & " occurred. " & Err.Description _
  593.                           & ". " & vbCRLF & "The scripting host could not be determined.")       
  594.         
  595.     end if
  596.     
  597.     IsHostCscript = bReturn
  598.  
  599. end function
  600.  
  601.