LeightonJames Posted March 10, 2014 Posted March 10, 2014 (edited) 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: 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: 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 Edited March 10, 2014 by LeightonJames
spadam Posted March 11, 2014 Posted March 11, 2014 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.
LeightonJames Posted March 11, 2014 Author Posted March 11, 2014 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.
Pilot72 Posted February 29, 2016 Posted February 29, 2016 Hello, I was looking for a similar script that will delete Printer Ports using a text file listing the IP ports to be removed by the script on windows 7. Thank you in advance for the support. Regards
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now