VBScript for changing network(queue)printer to IP printer

Author Message
RaulMadrid

  • Total Posts : 6
  • Scores: 0
  • Reward points : 0
  • Joined: 12/24/2011
  • Status: offline
VBScript for changing network(queue)printer to IP printer Sunday, January 01, 2012 10:03 AM (permalink)
0
Dear all,
I am new in VB and want to write a script that changes some printersettings on XP workstations. We want to remove some printservers and want the users to print directly to an IP port instead of a neworkqueue (for example: printer \\server1\printer1 with IP address 10.10.10.200 needs to be changed to print directly to IP_10.10.10.200 without the need for the printserver)
 
I want to do the following on a local XP workstation:
 
1. Determine if a printer is a networkprinter (printserver), if not skip this printer.
2. If it is a networkprinter, than retrieve the IP address of that used networkprinter.
3. Use this IP address for creating a new tcp/ip printer on the XP workstation.
4. Keep the rest of the settings default as it is.
 
With other words, users who now prints to a printer located on a printserver, needs to be changed so that they don't need the printserver anymore, and instead print directly to a TCP/IP printerport. I assume that the correct printerdrives are already installed on the local workstation. The best way in my opinion if possible is to change only the current printer from printserver to IP, with all the settings kept. If not, than also adding a new printer is no problem with the correct drivers etc.
 
Hopefully you guys can help me out with this. If needed i can post the current script.
Many thanks in advance for your help.
 
Ismael
 
#1
    59cobalt

    • Total Posts : 981
    • Scores: 91
    • Reward points : 0
    • Joined: 7/17/2011
    • Status: offline
    Re:VBScript for changing network(queue)printer to IP printer Tuesday, January 03, 2012 2:04 AM (permalink)
    0
    It's normally not a good idea to switch clients from a printserver to directly printing to a network printer.

    That said, to determine the IP address of the printer, you need to determine the port associated with the printer on the print server:
    printServer = "SERVER_NAME"
    printerName = "PRINTER_NAME"
    
    Set wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & printServer & "\root\cimv2")
    Set printers =  wmi.ExecQuery("Select * from Win32_Printer")
    For Each printer In printers 
     WScript.Echo printer.PortName
    Next
    If the port name is based on the IP address (IP_a.b.c.d) you can extract the address using the Mid() function, otherwise you need to resolve the name to the actual address.
     
    #2
      RaulMadrid

      • Total Posts : 6
      • Scores: 0
      • Reward points : 0
      • Joined: 12/24/2011
      • Status: offline
      Re:VBScript for changing network(queue)printer to IP printer Tuesday, January 03, 2012 2:40 AM (permalink)
      0
      Hi 59Cobalt,
      Many thanks for your feedback. I agree with you that this is not a suitable solution but that is what the projectteam decided to do. And they only want to remove the printservers from Medium and Small sites. The big sites will keep the printservers.
       
      In the meantime we have collected almost every information. The only thing we need is the correct permissions to query the printserver for the IP address. The rest of the properties can be obtained from the local workstation like drivername, comment, location etc. We will use your advise in our script. Is there no way to obtain the IP from the local workstation instead of query the printserver?
       
      Anyway, i really appreciate your help. Have a nice day.
       
      Regards
       
      #3
        59cobalt

        • Total Posts : 981
        • Scores: 91
        • Reward points : 0
        • Joined: 7/17/2011
        • Status: offline
        Re:VBScript for changing network(queue)printer to IP printer Tuesday, January 03, 2012 3:46 AM (permalink)
        0
        I don't believe you can get that information on the client, because that information is irrelevant for the client. Only the print server needs to know the printers' IP addresses (or names respectively).
         
        #4
          RaulMadrid

          • Total Posts : 6
          • Scores: 0
          • Reward points : 0
          • Joined: 12/24/2011
          • Status: offline
          Re:VBScript for changing network(queue)printer to IP printer Tuesday, January 03, 2012 3:56 AM (permalink)
          0
          So my assumption is correct, this needs to be done on the printserver. We are working now on a solution. If you are interested, i can send the vbscript as it is right now so you can have a look. This script needs to check on all workstations all the printers installed which are hosted on a printserver. From this printers, we need the drivers, location, comment etc and with this information, together with the IP we need to create a new printer with the same specs but configured to print directly to an IP port instead of a unc starting with \\printservername.
           
          Thanks.
           
          #5
            59cobalt

            • Total Posts : 981
            • Scores: 91
            • Reward points : 0
            • Joined: 7/17/2011
            • Status: offline
            Re:VBScript for changing network(queue)printer to IP printer Wednesday, January 04, 2012 9:39 AM (permalink)
            0
            Just so there are no misunderstandings: the code snippet I posted is supposed to be run on the client. From there it will look up the information on the server via WMI.

            You should be able to create a local printer port with WMI as well:
            addr = "a.b.c.d"
            
            Set wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
            
            Set port = wmi.Get("Win32_TCPIPPrinterPort").SpawnInstance_
            port.Name        = "IP_" & addr
            port.Protocol    = 1
            port.HostAddress = addr
            port.PortNumber  = "9100"
            port.SNMPEnabled = False
            port.Put_

            As for reviewing your script: post it here and I'll probably take a look at it. Do not send it to me in private. I don't do personal support for free.
             
            #6
              RaulMadrid

              • Total Posts : 6
              • Scores: 0
              • Reward points : 0
              • Joined: 12/24/2011
              • Status: offline
              Re:VBScript for changing network(queue)printer to IP printer Wednesday, January 04, 2012 10:52 PM (permalink)
              0
              ' VBScript Source File -- Created with SAPIEN Technologies PrimalScript 2009
              '
              ' NAME: Change printers
              '
              ' AUTHOR: Erik Kottman , Atos Origin
              ' INPUT:  Ismail Amozlouf, Atos Origin
              ' DATE  : 04-01-2012
              '
              ' VERSION : Draft 1.1
              '
              ' COMMENT: Created for Akzo Nobel AAC to migrate server printer connections
              ' COMMENT: to local printer connections
              '
              ' Printservers which can be used are: SASVN16 - SASN51
              '==========================================================================
               
              Option Explicit
              'On Error Resume Next
               
              ' Declaration
              Dim blnDefault
              Dim objWMIService, objShell, objPort, objNewPort, objfso, objfile, objPrinter, objService
              Dim colItems1, colItems2, colports, colServiceList
              Dim arrServer, arrPrinter, errReturn
              Dim strComment, strComputer, strDefault, strDeviceID, strDriver, strIP
              Dim strLocation, strName, strPath, strPort, strPrinter, strResult, strServer
              ' Main Section
               
               
               '=============================================== BEGIN "GET DEFAULT PRINTER" ======================================================================
              ' Get Default Printer
              strDefault = Get_Default_Printer
               WScript.Echo "Default: " & strDefault
               
               '=============================================== END "GET DEFAULT PRINTER" ======================================================================
               
               
               '=============================================== BEGIN "PUT ALL PRINTERS IN FILE" ======================================================================
               ' Get Local Printers
              Set objFSO = CreateObject("Scripting.FileSystemObject")
              Set objFile = objFSO.CreateTextFile("PRINTERS.CSV", True)
              strComputer ="."
              Set objWMIService = GetObject ("winmgmts:\\" & strComputer & "\root\CIMV2")
              Set colItems1 = objWMIService.ExecQuery ("SELECT * FROM Win32_Printer")
              '=============================================== END "PUT ALL PRINTERS IN FILE" ======================================================================
               
              '=============================================== BEGIN STOP/START SPOOLER SERVICE ======================================================================
              '        Set colServiceList = objWMIService.ExecQuery _
              '                ("Select * from Win32_Service where Name='Spooler'")
              '        For Each objService In colServiceList  
              '            errReturn = objService.StopService()
              '            WScript.Sleep 5000
              '        Next
                       
              'Start Spooler Service
               
              '        For Each objService In colServiceList
              '            errReturn = objService.StartService()
              '            WScript.Sleep 5000
              '        Next
              '=============================================== END STOP/START SPOOLER SERVICE ========================================================================
               
              ' =============================================== BEGIN PRINTER COLLECTION ==============================================================================
               For Each objPrinter In colItems1
                              strPrinter = objPrinter.DeviceID
                              blnDefault = False
                              If Left(strPrinter,2) = "\\" Then
                                 arrServer            = Split(strPrinter,"\")
                                             strServer             = (arrServer(2))
                        'objFile.WriteLine "IP Nummer" & ";" & "Drivername" & ";" & "Device ID"
                        objFile.WriteLine objPrinter.PortName & ";"  & objPrinter.DriverName & ";" & objPrinter.DeviceID
                       
                        strName = Get_Name(strPrinter)
                        If strPrinter = strDefault Then
                            blnDefault = True
                        End If
                        If Not IsNull(objPrinter.Location) then
                            strLocation = objPrinter.Location
                        End If
                        strPort     = objPrinter.PortName
                        strIP       = Get_IP(strPort)
                        strPort     = "IP_" & strIP
                        strDriver   = objPrinter.DriverName
                                             strDeviceID = objPrinter.DeviceID
                                             'strResult   = Get_Host_Address(strServer, strPort)
                       
                        strComment = objPrinter.Comment
                        'WScript.Echo strComment
                                
                                             'Create new TCP/IP printer
                                             If Not strIP = "" Then
                             strResult = Create_Printer(strName, strLocation, strPort, strDeviceID, strIP, strDriver, strDefault, strComment,                                       blnDefault)
                                                     'Delete the old Printer connection
                          If Err.Number = 0 Then
                                                         strResult = Delete_Printer(strServer, strPrinter)
                             objPrinter.Delete_
                                                     End If
                                             End If
                                             Err.Clear
                                            
                              End If
               
              Next
              WScript.Echo "All printers are replaced by IP printers"
              Wscript.Quit
              Function Get_Default_Printer
              Dim strDefault
              ' Get default printer of WINXP workstations
              strDefault = ""
              Set objShell = CreateObject("WScript.Shell")
              strPath = "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device"
              arrPrinter = Split(objShell.RegRead(strPath), ",")
               
              If IsArray(arrPrinter) Then
                              strDefault = arrPrinter(0)
              End If
              Get_Default_Printer = strDefault
              Err.Clear
              End Function
               
              ' =============================================== BEGIN QUERY ON PRINTSERVER ==============================================================================
               
              Function Get_Host_Address(strServer, strPort)
              ' Get ip adress or hostname of printer port from print server
              ' Rights to query the server are necessary to perform this step
              strComputer = strServer
              strIP                      = ""
              'Set objWMIService = GetObject ("winmgmts:\\" & strComputer & "\root\CIMV2")
              Set colItems2 = objWMIService.ExecQuery ("SELECT * FROM Win32_TCPIPPrinterPort")
               
              For Each objPort in colItems2
               
                              If Ucase(objPort.Name) = Ucase(strPort) Then
                                          strIP = objPort.HostAddress
                              End If
               
              Next
               
              Err.Clear
              End Function
              '=============================================== END QUERY ON PRINTSERVER ==============================================================================
               
               
               
              '=============================================== BEGIN FUNCTION "CREATE_PRINTER" ======================================================================= 
               
              Function Create_Printer(strName, strLocation, strPort, strDeviceID, strIP, strDriver, strDefault, strComment, blnDefault)
              Dim colInstalledPrinters, colServiceList, DrvPath, errReturn, InfPath, intResult
              Dim objDriver, objNewPort, objPrinter, objService, objWMIService
              Dim PrnComment, strComputer
                  Set objWMIService = GetObject("Winmgmts:")
                  objWMIService.Security_.Privileges.AddAsString "SeLoadDriverPrivilege", True
              DrvPath = "D:\Printer creation\drivers\Inkjet 2800\hpbi2800\Drivers\PCL6\Win2k_XP\English"
              InfPath = DrvPath & "\hpw2800c.inf"
              ' =============================================== BEGIN IP-PORT INSTALLATION ==============================================================================
              strComputer = "."
              Set objWMIService = GetObject("winmgmts:" _
                  & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
               
              Set objNewPort = objWMIService.Get _
                  ("Win32_TCPIPPrinterPort").SpawnInstance_
              objNewPort.Name = strPort
              objNewPort.Protocol = 1
              objNewPort.HostAddress = strIP
              objNewPort.PortNumber = "9100"
              objNewPort.SNMPEnabled = False
              objNewPort.Put_
              If Err = 0 Then
                  wsh.echo "IP Port created"
              Else
                  wsh.echo "Error !!!"
              End If
              Err.Clear
              '=============================================== END IP-PORT INSTALLATION ==============================================================================
              '=============================================== BEGIN STOP/START SPOOLER SERVICE ======================================================================
                      Set colServiceList = objWMIService.ExecQuery _
                              ("Select * from Win32_Service where Name='Spooler'")
                      For Each objService In colServiceList  
                          errReturn = objService.StopService()
                          WScript.Sleep 5000
                      Next
                      
              'Start Spooler Service
               
                      For Each objService In colServiceList
                          errReturn = objService.StartService()
                          WScript.Sleep 5000
                      Next
              '=============================================== END STOP/START SPOOLER SERVICE ==================================================================
               
              '=============================================== BEGIN DRIVER INSTALLATION ============================================================================
              '
              ' If the driver is not signed, one cannot use WMI scripting to install the driver.
              ' Make sure the cat file for the package is copied to the same location as the driver
              objWMIService.Security_.Privileges.AddAsString "SeLoadDriverPrivilege", True
              Set objDriver = objWMIService.Get("Win32_PrinterDriver")
              objDriver.Name = strDriver
              objDriver.SupportedPlatform = "Windows NT x86"
              objDriver.Version = "3"
              intResult = objDriver.AddPrinterDriver(objDriver)
              If Err = 0 Then
                  wsh.echo "Printerdriver installed"
              Else
                  wsh.echo "Error !!!"
              End If
              Err.Clear
              '=============================================== END DRIVER INSTALLATION ==============================================================================
               
              '=============================================== BEGIN ADDING LOCAL IP PRINTER ========================================================================
                        strComputer = "."
                      Set objWMIService = GetObject("winmgmts:" _
                              & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
               
                      Set objPrinter = objWMIService.Get("Win32_Printer").SpawnInstance_
               
                objPrinter.DriverName = StrDriver
                objPrinter.PortName   = strPort
                      objPrinter.DeviceID   = strName
                      objPrinter.Network = True
                      objPrinter.Shared = False
                      objPrinter.ShareName = ""
                      objPrinter.Published = False
                      If IsNull(strLocation) Then
                          objPrinter.Location = " "
                      Else
                          objPrinter.Location = strLocation  
                      End If    
                      If strLocation = "" Then
                          objPrinter.Location = " "
                      Else
                          objPrinter.Location = strLocation  
                      End If    
                objPrinter.Comment = strComment
                      objPrinter.Put_
                If Err = 0 Then
                     wsh.echo "Local IP Printer created and added" 
                Else
                          wsh.echo "Error !!!"
                      End If
                      Err.Clear  
                  
              '=============================================== END ADDING LOCAL IP PRINTER ========================================================================  
                
              '=============================================== BEGIN SETTING PRINTER AS DEFAULT ===================================================================  
               
               'SETS PRINTER AS DEFAULT.
                  'wsh.echo "Name: " & strName
              If blnDefault = True Then 
                  Set colInstalledPrinters =  objWMIService.ExecQuery _
                      ("Select * from Win32_Printer Where Name = '" & strName & "'")
                  For Each objPrinter in colInstalledPrinters
                      objPrinter.SetDefaultPrinter()
                  next
                  If Err = 0 Then
                      wsh.echo "Default printer set"
               else
                      wsh.echo "Error while setting default printer!!!"
                  End If
                  Err.Clear
               End If
              '=============================================== END SETTING PRINTER AS DEFAULT ===================================================================
              End Function
              ' =============================================== END FUNCTION "CREATE_PRINTER" =================================================================== 
               
              ' =============================================== BEGIN FUNCTION "DELETE_PRINTER" ======================================================================= 
               
              Function Delete_Printer(strServer, strPrinter)
              ' Delete the old Printer connection
              ' This part is not completed yet but also should not be done before creation is succesfull
              ' First test the creation of the new printer before coding the removal
              'WScript.Echo     "strServer: " & strServer & " " & vbCrLf & _
               '                                                              "strPrinter: " & strPrinter & vbCrLf & _
               '                                                              ""
              'Err.Clear
              End Function
              ' =============================================== END FUNCTION "DELETE_PRINTER" =======================================================================

              ' =============================================== BEGIN FUNCTION "GET IP ADDRESS FROM PRINTER" ==================================================== 
              Function Get_IP(strPort)
              ' Get the IP address of the printer connection
              Dim intColon, intIP, intUnderscore
              Dim strIP
              'strPort = "IP_161.90.92.51"
              'strPort = "161.90.92.510:RAW1"
              'strPort = "IP_161.90.92.51:RAW1"
              'strPort = "161.90.92.510_RAW1"
              'strPort = "IP_161.90.92.51_RAW1"
              intIP = InStr(1, strPort, "IP_", 1)
              If intIP > 0 Then
                  strIP = Mid(strPort, intIP + 3)
                  intColon = InStr(1, strIP, ":", 1)
                  If intColon > 0 Then
                      strIP = Left(strIP, intColon - 1)
                  End If
                  intUnderscore = InStr(1, strIP, "_", 1)
                  If intUnderscore > 0 Then
                      strIP = Left(strIP, intUnderscore - 1)
                  End If
              Else
                  intColon = InStr(1, strPort, ":", 1)
               If intColon > 0 Then
                      strIP = Left(strPort, intColon - 1)
               Else
                   strIP = strPort
               End If
                  intUnderscore = InStr(1, strPort, "_", 1)
               If intUnderscore > 0 Then
                      strIP = Left(strPort, intUnderscore - 1)
               End If
              End If
                 
              Get_IP = strIP
              End Function
              ' =============================================== END FUNCTION "GET_IP" =============================================================================== 

              ' =============================================== BEGIN FUNCTION "GET_NAME" =========================================================================== 
              Function Get_Name(strPrinter)
              Dim intSlash1
              Dim intSlash2
              Dim intSlash3
              Dim strName
              intSlash1 = InStr(1, strPrinter, "\", 1)
              strName = Mid(strPrinter, intSlash1 + 1)
              intSlash2 = InStr(1, strName, "\", 1)
              strName = Mid(strName, intSlash2 + 1)
              intSlash3 = InStr(1, strName, "\", 1)
              strName = Mid(strName, intSlash3 + 1)
              Get_Name = strName
              End Function
              ' =============================================== END FUNCTION "GET_NAME" ============================================================================= 
               
              #7
                RaulMadrid

                • Total Posts : 6
                • Scores: 0
                • Reward points : 0
                • Joined: 12/24/2011
                • Status: offline
                Re:VBScript for changing network(queue)printer to IP printer Wednesday, January 04, 2012 10:55 PM (permalink)
                0
                59cobalt

                 
                TEST

                Just so there are no misunderstandings: the code snippet I posted is supposed to be run on the client. From there it will look up the information on the server via WMI.

                You should be able to create a local printer port with WMI as well:
                addr = "a.b.c.d" 
                
                Set wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2") 
                
                Set port = wmi.Get("Win32_TCPIPPrinterPort").SpawnInstance_ 
                port.Name        = "IP_" & addr 
                port.Protocol    = 1 
                port.HostAddress = addr 
                port.PortNumber  = "9100" 
                port.SNMPEnabled = False 
                port.Put_

                As for reviewing your script: post it here and I'll probably take a look at it. Do not send it to me in private. I don't do personal support for free.


                 
                #8
                  RaulMadrid

                  • Total Posts : 6
                  • Scores: 0
                  • Reward points : 0
                  • Joined: 12/24/2011
                  • Status: offline
                  Re:VBScript for changing network(queue)printer to IP printer Wednesday, January 04, 2012 11:02 PM (permalink)
                  0
                  I was fighting with the reply options. I think you can see the script as it is right now. It does what it needs to do. It reads all the network installed printers on the workstation. Grabs the driver, ip, comment, location etc from the printer. Local printers will not be touched. With this information, it creates the new printerport, printer and driver. After a succesfull creation, the old printer is deleted. The current default printer will be converted and set back as default. I have tested the script and it works fine.
                   
                  The only thing i need now is a trustable way of getting the IP address from the fysical printer. Now we read this from the printer which is installed on the local workstation but i need to be sure about the information. So today i will receive a list with all the printers and their IP address. Or is there another way of grabbing the real IP address of the printer. If i had the right permissions (which i don't have) i could read this information from the printserver. So you say this can't be done on the local workstation, there is no way to retrieve the information. When i look at the port properties of the printers installed on the local workstation, i do see an IP address, where is this information coming from, i assume this is filled in during creation of the printer.
                   
                  Anyway, thanks for your replies, and if you have some feedback or advis regarding the script, please feel free to share with me.
                   
                  Regards.
                   
                  59cobalt


                  Just so there are no misunderstandings: the code snippet I posted is supposed to be run on the client. From there it will look up the information on the server via WMI.

                  You should be able to create a local printer port with WMI as well:
                  addr = "a.b.c.d" 
                  
                  Set wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2") 
                  
                  Set port = wmi.Get("Win32_TCPIPPrinterPort").SpawnInstance_ 
                  port.Name        = "IP_" & addr 
                  port.Protocol    = 1 
                  port.HostAddress = addr 
                  port.PortNumber  = "9100" 
                  port.SNMPEnabled = False 
                  port.Put_

                  As for reviewing your script: post it here and I'll probably take a look at it. Do not send it to me in private. I don't do personal support for free.


                   
                  #9
                    59cobalt

                    • Total Posts : 981
                    • Scores: 91
                    • Reward points : 0
                    • Joined: 7/17/2011
                    • Status: offline
                    Re:VBScript for changing network(queue)printer to IP printer Friday, January 06, 2012 12:14 AM (permalink)
                    0
                    RaulMadrid
                    The only thing i need now is a trustable way of getting the IP address from the fysical printer. Now we read this from the printer which is installed on the local workstation but i need to be sure about the information. So today i will receive a list with all the printers and their IP address. Or is there another way of grabbing the real IP address of the printer.
                    This may not be an option to you, but one way of dealing with this kind of issue is to use DHCP and DNS.
                    • Configure the network printer to obtain its IP address via DHCP or BOOTP.
                    • Configure an appropriate hostname on the printer.
                    • Configure an address reservation for the printer's MAC address on the DHCP server.
                    • Configure the DHCP server to register addresses in DNS (for this registration the hostname configured on the printer will be used).
                    • Configure the DNS server to allow (secure) dynamic updates.
                    With a setup like that, you can use the printer hostnames for the port definition, and don't have to worry about IP addresses anymore.

                    RaulMadrid
                    If i had the right permissions (which i don't have) i could read this information from the printserver. So you say this can't be done on the local workstation, there is no way to retrieve the information.
                    Since the PortName of the Win32_Printer object seems to reveal that information (I wasn't aware of that), you probably don't need access to the print server.

                    RaulMadrid
                    Anyway, thanks for your replies, and if you have some feedback or advis regarding the script, please feel free to share with me.
                    Just a couple things that caught my eye:
                    RaulMadrid
                    If Left(strPrinter,2) = "\\" Then
                     arrServer = Split(strPrinter,"\")
                     strServer = (arrServer(2))
                     'objFile.WriteLine "IP Nummer" & ";" & "Drivername" & ";" & "Device ID"
                     objFile.WriteLine objPrinter.PortName & ";"  & objPrinter.DriverName & ";" & objPrinter.DeviceID
                    
                     strName = Get_Name(strPrinter)
                    This can be simplified to
                    If Left(strPrinter, 2) = "\\" Then
                     arrPrinter = Split(Mid(strPrinter, 3), "\", 2)
                     strServer  = arrPrinter(0)
                     strName    = arrPrinter(1)

                    RaulMadrid
                    Function Get_IP(strPort)
                     ' Get the IP address of the printer connection
                     Dim intColon, intIP, intUnderscore
                     Dim strIP
                     'strPort = "IP_161.90.92.51"
                     'strPort = "161.90.92.510:RAW1"
                     'strPort = "IP_161.90.92.51:RAW1"
                     'strPort = "161.90.92.510_RAW1"
                     'strPort = "IP_161.90.92.51_RAW1"
                     intIP = InStr(1, strPort, "IP_", 1)
                     If intIP > 0 Then
                     strIP = Mid(strPort, intIP + 3)
                     intColon = InStr(1, strIP, ":", 1)
                     If intColon > 0 Then
                     strIP = Left(strIP, intColon - 1)
                     End If
                     intUnderscore = InStr(1, strIP, "_", 1)
                     If intUnderscore > 0 Then
                     strIP = Left(strIP, intUnderscore - 1)
                     End If
                     Else
                     intColon = InStr(1, strPort, ":", 1)
                     If intColon > 0 Then
                     strIP = Left(strPort, intColon - 1)
                     Else
                     strIP = strPort
                     End If
                     intUnderscore = InStr(1, strPort, "_", 1)
                     If intUnderscore > 0 Then
                     strIP = Left(strPort, intUnderscore - 1)
                     End If
                     End If
                    
                     Get_IP = strIP
                    End Function
                    You can simplify this whole shebang to
                    strIP = Replace(strPort, "IP_", "")
                    strIP = Split(strIP, ":")(0)
                    strIP = Split(strIP, "_")(0)
                    or
                    Set re = New RegExp
                    re.Pattern = ".*(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).*"
                    strIP = re.Replace(strPort, "$1")

                    RaulMadrid
                    Function Get_Default_Printer
                     Dim strDefault
                     ' Get default printer of WINXP workstations
                     strDefault = ""
                     Set objShell = CreateObject("WScript.Shell")
                     strPath = "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device"
                     arrPrinter = Split(objShell.RegRead(strPath), ",")
                    
                     If IsArray(arrPrinter) Then
                     strDefault = arrPrinter(0)
                     End If
                     Get_Default_Printer = strDefault
                     Err.Clear
                    End Function
                    It's probably better to do this with WMI:
                    For Each objPrinter in objWMIService.ExecQuery("SELECT * FROM Win32_Printer WHERE Default = 'True'")
                     strDefault = objPrinter.Name
                    Next

                    RaulMadrid
                    If blnDefault = True Then
                     Set colInstalledPrinters =  objWMIService.ExecQuery _
                     ("Select * from Win32_Printer Where Name = '" & strName & "'")
                     For Each objPrinter in colInstalledPrinters
                     objPrinter.SetDefaultPrinter()
                     next
                     If Err = 0 Then
                     wsh.echo "Default printer set"
                     else
                     wsh.echo "Error while setting default printer!!!"
                     End If
                     Err.Clear
                    End If
                    It's easier to use the WshNetwork object for setting the default printer:
                    If blnDefault Then
                     CreateObject("WScript.Network").SetDefaultPrinter strName
                    End If

                    Also it should be sufficient to get the WMI object once, add "SeLoadDriverPrivilege" once, and then use it everywhere in the script.
                    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\CIMV2")
                    objWMIService.Security_.Privileges.AddAsString "SeLoadDriverPrivilege", True

                    <message edited by 59cobalt on Friday, January 06, 2012 12:17 AM>
                     
                    #10

                      Online Bookmarks Sharing: Share/Bookmark

                      Jump to:

                      Current active users

                      There are 0 members and 1 guests.

                      Icon Legend and Permission

                      • New Messages
                      • No New Messages
                      • Hot Topic w/ New Messages
                      • Hot Topic w/o New Messages
                      • Locked w/ New Messages
                      • Locked w/o New Messages
                      • Read Message
                      • Post New Thread
                      • Reply to message
                      • Post New Poll
                      • Submit Vote
                      • Post reward post
                      • Delete my own posts
                      • Delete my own threads
                      • Rate post

                      2000-2012 ASPPlayground.NET Forum Version 3.9