+ Post New Thread
Results 1 to 3 of 3
Scripts Thread, Printer port change in Coding and Web Development; Hi all I have successfully created a port change script which when run adds a new IP Port and then ...
  1. #1

    LeightonJames's Avatar
    Join Date
    Sep 2009
    Posts
    621
    Thank Post
    23
    Thanked 85 Times in 54 Posts
    Rep Power
    267

    Printer port change

    Hi all I have successfully created a port change script which when run adds a new IP Port and then changes the relevant printer to that port. It works fine on Windows 7 and 8. It's on Windows XP I have the issue. Please don't tell me that it's not going to be an issue in a few weeks because unfortunately, in schools, we all know thats not the case.

    Here is the script:
    Code:
    Set wshShell = CreateObject("WScript.Shell")
    strComputer = "."
    filename = ".\printers.txt"
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set f = fso.OpenTextFile(filename)
    
    Do Until f.AtEndOfStream
    	
    	Dim myArray
    	
    	Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & 
    
    "\root\cimv2")
    	Set objNewPort = objWMIService.Get("Win32_TCPIPPrinterPort").SpawnInstance_
    	
    	myArray = split(f.readLine, ",")
    	
    	strOldIPAddress = myArray(0)
    	strNewIPAddress = myArray(1)
    
    	objNewPort.Name = strNewIPAddress
    	objNewPort.Protocol = 1
    	objNewPort.HostAddress = strNewIPAddress
    	objNewPort.PortNumber = "9100"
    	objNewPort.SNMPEnabled = False
    	objNewPort.Put_
    
    	Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    
    	Set colPrinters = objWMIService.ExecQuery _
    		("Select * From Win32_Printer Where PortName='" & strOldIPAddress & "'")
    
    	For Each objPrinter in colPrinters
    		objPrinter.PortName = strNewIPAddress
    		objPrinter.Put_
    	Next
    
    Loop
    f.Close
    When run on XP I get a substring out of range error on Line 20.

    The line in question is this:

    Code:
    strNewIPAddress = myArray(1)
    I have read on some sites that it is because I am not declaring the size of the array yet when I declare the array size I get the same array and if this was the case I would expect the error on 7 and 8.

    EDIT: Forgot to say, the printers.txt file contains lines like this:

    192.168.0.1,192.168.5.68
    192.168.0.3,192.168.7.90
    Last edited by LeightonJames; 10th March 2014 at 05:27 PM.

  2. #2

    Join Date
    Feb 2011
    Location
    Dudley
    Posts
    145
    Thank Post
    22
    Thanked 37 Times in 27 Posts
    Rep Power
    14
    is the printers.txt file identical in all cases? If not check the syntax in the file.. Other than that I don't know what it could be.

  3. #3

    LeightonJames's Avatar
    Join Date
    Sep 2009
    Posts
    621
    Thank Post
    23
    Thanked 85 Times in 54 Posts
    Rep Power
    267
    Quote Originally Posted by spadam View Post
    is the printers.txt file identical in all cases? If not check the syntax in the file.. Other than that I don't know what it could be.
    Yep, the text file is identical. It is stored in the same directory as the script hence the relative path reference. It is only on XP that I have this probem. It's really annoying too as I am in the middle of a network wide server refresh and each site has different printers on the machines. We were initially using printmig and print management to change the printers but it became cumbersome and slow after a while. Changing the ports is much quicker and utilizes the drivers already on the machine.



SHARE:
+ Post New Thread

Similar Threads

  1. Access denied adding/ changing printer ports
    By penfold in forum Windows Server 2000/2003
    Replies: 3
    Last Post: 22nd June 2012, 01:45 PM
  2. Printers randomly changing ports
    By SteveBentley in forum Windows Server 2008 R2
    Replies: 3
    Last Post: 14th September 2010, 10:24 AM
  3. Cannot Delete Corrupt Printer Port
    By DaveP in forum How do you do....it?
    Replies: 1
    Last Post: 21st October 2009, 11:20 AM
  4. Replies: 6
    Last Post: 23rd June 2007, 09:44 AM
  5. Windows 2003 Server R2 - No printer ports !!
    By pmassingham in forum Windows
    Replies: 2
    Last Post: 1st September 2006, 04:16 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •