• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 312
  • Last Modified:

VB Scripting question in Windows AD environment

Thank you all in advance for helping me with this. I am  slowly learning VB scripting so I apologize if my questions are elementary.

I have about 24 printers on my print server that were set up by consultants before I got here so they are long ridiculous names. I would like to be able to change all the names to simpler names, pretty much a straight mapping from one name to the other. My problem comes in when all of my users are connected to the old print names. What would the script look like that would allow me to change printer names on the client.

My theory is that the script would look at what is installed on the desktop, check with a list of printers it has and then change the names if they match the list. I don't want to install all 24 printers on all of my computers, just change the ones that are already there.

I know what you are thinking, use a login script that checks group membership and just add the printers that way. I intend to do this eventually but right now I need to clean up the names and have them changed on all the user's desktops. I have found many scripts showing me how to change the server name when the printer name stays the same, I need to do the opposite, change the printer name and keep the server the same.

I really appreciate all of your help, I am asking a lot so I am giving this 500 points.
0
EFHC
Asked:
EFHC
  • 8
  • 7
1 Solution
 
MilanKMCommented:
0
 
Chris DentPowerShell DeveloperCommented:

Hi,

We need to dip into WMI briefly to find all the printers that are attached to a PC so we can sort through and remove them.

Once you have that list you get a couple of choices about what you do with it. You can either remove all the printers and start over. Or remove specific printers.

Removing everything is easy, it's just a case of doing something like this:

Const WBEM_RETURN_IMMEDIATELY = &h10
Const WBEM_FORWARD_ONLY = &h20

Set objNetwork = CreateObject("WScript.Network")
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")

Set colPrinters = objWMIService.ExecQuery("SELECT * FROM Win32_Printer", _
      "WQL", WBEM_RETURN_IMMEDIATELY + WBEM_FORWARD_ONLY)

For Each objPrinter in colPrinters
      strPrinterServer = objPrinter.ServerName

      On Error Resume Next
      If strPrinterServer <> "" Then
            objNetwork.RemovePrinterConnection strPrinterServer & "\" & objPrinter.ShareName
      End If
      On Error Goto 0
Next

If you're removing a Specific Printer then we need to make this bit do a bit more work:

      If strPrinterServer <> "" Then
            objNetwork.RemovePrinterConnection strPrinterServer & "\" & objPrinter.ShareName
      End If

All we've done at the moment is see if the printer we're trying to remove is a network printer, we haven't paid the slightest bit of attention to it's name so far. We could do something like this:

      If strPrinterServer <> "" Then
            strPrinterShare = objPrinter.ShareName
            If strPrinterShare = "SomePrinterName" Then
                  objNetwork.RemovePrinterConnection strPrinterServer & "\" & objPrinter.ShareName
            End If
      End If

The disadvantage is you have to tell it all the printers you're looking to remove. Remember that the Comparison we're doing (If Variable1 = Variable2) is case sensitive as well, if case is likely to be a problem then converting both to Lower Case or Upper Case would be a good idea.

            If LCase(strPrinterShare) = "someprintername" Then

All making sense so far?

Chris
0
 
EFHCAuthor Commented:
Yes I think this is making sense. Can the script remember which printers it is removing and then install the new printers that coorelate to the ones it deleted?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
EFHCAuthor Commented:
MilanKM :

I am not sure what this scirpt is trying to do, also how would I set it up to change the name of 24 possible printers?
0
 
EFHCAuthor Commented:
I have no problem writing out this script for all 24 printers, I just don't know how to get it to cycle through and delete the old and add the new printer that the old one was changed to.
0
 
Chris DentPowerShell DeveloperCommented:

> Can the script remember which printers it is removing and then install the
> new printers that coorelate to the ones it deleted?

It would have to be told to do that, not really a big problem, something like this perhaps:

     If strPrinterServer <> "" Then
          strPrinterShare = objPrinter.ShareName
          If strPrinterShare = "SomePrinterName" Then
               objNetwork.RemovePrinterConnection strPrinterServer & "\" & strPrinterShare
               objNetwork.AddWindowsPrinterConnection strPrinterServer & "\<New Printer Share Name>"
          End If
     End If

Is that what you had in mind?

Chris
0
 
EFHCAuthor Commented:
********************************************************************
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colPrinters =  objWMIService.ExecQuery _
    ("Select * from Win32_Printer Where DeviceID = 'HP LaserJet 4Si M'")

For Each objPrinter in colPrinters
    objPrinter.RenamePrinter("ArtDepartmentPrinter")
Next

Set colPrinters = objWMIService.ExecQuery _
    ("Select * From Win32_Printer Where DeviceID = 'ArtDepartmentPrinter' ")

For Each objPrinter in colPrinters
    objPrinter.ShareName = "ArtDepartmentPrinter"
    objPrinter.Put_
Next
**********************************************************

The script that MilanKM pointed me to seems like it may work, would I keep doing a NEXT and copy the same thing for each printer that I want renamed on the workstation? Don't I need to specify ON ERROR RESUME NEXT?
0
 
Chris DentPowerShell DeveloperCommented:

Oh and one other bit... if you want it to deal with more than one printer then it's just a case of adding If statements:

     If strPrinterServer <> "" Then
          strPrinterShare = objPrinter.ShareName

          If strPrinterShare = "SomePrinterName" Then
               objNetwork.RemovePrinterConnection strPrinterServer & "\" & strPrinterShare
               objNetwork.AddWindowsPrinterConnection strPrinterServer & "\<New Printer Share Name>"
          End If
          If strPrinterShare = "SomeOtherPrinterName" Then
               objNetwork.RemovePrinterConnection strPrinterServer & "\" & strPrinterShare
               objNetwork.AddWindowsPrinterConnection strPrinterServer & "\<Other New Printer Share Name>"
          End If
     End If

And so on.

Chris
0
 
EFHCAuthor Commented:
CHris, this seems like it would work. All the information in your first post has confused me a little bit, how would I start this?
0
 
Chris DentPowerShell DeveloperCommented:

Just one Next, you're already looping through all the printers available on the local computer:

Putting it together a little we have:

Const WBEM_RETURN_IMMEDIATELY = &h10
Const WBEM_FORWARD_ONLY = &h20

Set objNetwork = CreateObject("WScript.Network")
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")

Set colPrinters = objWMIService.ExecQuery("SELECT * FROM Win32_Printer", _
     "WQL", WBEM_RETURN_IMMEDIATELY + WBEM_FORWARD_ONLY)

For Each objPrinter in colPrinters
     strPrinterServer = objPrinter.ServerName

     On Error Resume Next
     If strPrinterServer <> "" Then
          strPrinterShare = objPrinter.ShareName

          If strPrinterShare = "SomePrinterName" Then
               objNetwork.RemovePrinterConnection strPrinterServer & "\" & strPrinterShare
               objNetwork.AddWindowsPrinterConnection strPrinterServer & "\<New Printer Share Name>"
          End If

          If strPrinterShare = "SomeOtherPrinterName" Then
               objNetwork.RemovePrinterConnection strPrinterServer & "\" & strPrinterShare
               objNetwork.AddWindowsPrinterConnection strPrinterServer & "\<Other New Printer Share Name>"
          End If
     End If
     On Error Goto 0
Next

The On Error Resume Next statement tells the script to just carry on if it bumps into a problem - for this it's a good idea to include it, if you need to find out why things are breaking then comment it out. And if you want to capture the errors the additional non-fatal error control could be added to capture messages.

Chris
0
 
EFHCAuthor Commented:
I have my printer names in the script. I tried to run it and it only deletes the printer names, it doesn't re-establish them with the new name. Could this be because we deleted the printer now it doesn't know what server to point the printer share to?





Const WBEM_RETURN_IMMEDIATELY = &h10
Const WBEM_FORWARD_ONLY = &h20

Set objNetwork = CreateObject("WScript.Network")
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")

Set colPrinters = objWMIService.ExecQuery("SELECT * FROM Win32_Printer", _
     "WQL", WBEM_RETURN_IMMEDIATELY + WBEM_FORWARD_ONLY)

For Each objPrinter in colPrinters
     strPrinterServer = objPrinter.ServerName

     On Error Resume Next
     If strPrinterServer <> "" Then
          strPrinterShare = objPrinter.ShareName

          If strPrinterShare = "Canon WT-Financial" Then
               objNetwork.RemovePrinterConnection strPrinterServer & "\" & strPrinterShare
               objNetwork.AddWindowsPrinterConnection strPrinterServer & "\wt-3-finance"
          End If

          If strPrinterShare = "Canon iR 5000-6000 PCL6" Then
               objNetwork.RemovePrinterConnection strPrinterServer & "\" & strPrinterShare
               objNetwork.AddWindowsPrinterConnection strPrinterServer & "\canon-changed"
          End If
     End If
     On Error Goto 0
Next
0
 
Chris DentPowerShell DeveloperCommented:

Nope, we store the Printer Server Name in strPrinterServer, it stays there even after we've deleted the old printer.

To see why we're getting the error try this simple bit (fill in ServerName as appropriate):

Set objNetwork = CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection "\\ServerName\canon-changed"

And it'll either add the printer or bounce back with an error message.

Chris
0
 
EFHCAuthor Commented:
My fault, I had the name mispelled on the server, it couldn't find the share.
0
 
Chris DentPowerShell DeveloperCommented:

That would do it :) It's working now?

Chris
0
 
EFHCAuthor Commented:
Yes it is working now, thank you so much for your assistance.
0
 
Chris DentPowerShell DeveloperCommented:

Pleasure :)
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 8
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now