Announcement

Collapse
No announcement yet.

VBS: migrating printers.

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • VBS: migrating printers.

    Today I wrote this script as a start to migrate client mapped printers from a couple of servers, to one dedicated printserver. Although it isn't completly finished (I need to create some kind of logging) I questioned myself if I can't make it more simple.
    Although this kind of work doens't belong anymore to my current job, i still think i haven't completly forgotten it

    Script is attached, cause it's quit big already

    If anyone has some suggestions, please let me know.
    Attached Files
    Last edited by Dumber; 2nd March 2007, 22:04.
    Marcel
    Technical Consultant
    Netherlands
    http://www.phetios.com
    http://blog.nessus.nl

    MCITP(EA, SA), MCSA/E 2003:Security, CCNA, SNAF, DCUCI, CCSA/E/E+ (R60), VCP4/5, NCDA, NCIE - SAN, NCIE - BR, EMCPE
    "No matter how secure, there is always the human factor."

    "Enjoy life today, tomorrow may never come."
    "If you're going through hell, keep going. ~Winston Churchill"


  • #2
    Re: VBS: migrating printers.

    Last year I used some of the %windir%\system32\ printer vbscripts (prnmngr.vbs, prncnfg.vbs) linked from an other script, to change mapped printer and drivers on clients after a print server migration.
    I resently looked if I could copy-past the 'determining the default printer' part from one of these, no success, but that's OK I found also an other way to do it.

    Well your script will be perfect for the job I guess (and you confirmed that yourself in an other thread , which is btw also about adding network printers to clients w/ special conditions, if others may be interested).

    I am not sure though if variable DefPrinter.. in line 55 from the attachment is the good one, shouldn't that be DefPrinter2 here?
    If your final script is a lot different that the first one you posted before. pls do post it.

    tnx
    \Rem
    Last edited by Rems; 8th March 2007, 23:52.

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment


    • #3
      Re: VBS: migrating printers.

      I'll sure i post it back including the logging.
      You see that i'm getting more info then needed, by for example reading the registry. This because it seems like that WMI doesn't show the portnames eg LPT1.
      In the other thread, i ment he should be able to download or opening the attachment

      Within days from now on, i would get an anwser where i should put the logging. From that point i would finishing the script.
      If you have an idea then, to make the script more cleaner, and more understandable please let me know
      Marcel
      Technical Consultant
      Netherlands
      http://www.phetios.com
      http://blog.nessus.nl

      MCITP(EA, SA), MCSA/E 2003:Security, CCNA, SNAF, DCUCI, CCSA/E/E+ (R60), VCP4/5, NCDA, NCIE - SAN, NCIE - BR, EMCPE
      "No matter how secure, there is always the human factor."

      "Enjoy life today, tomorrow may never come."
      "If you're going through hell, keep going. ~Winston Churchill"

      Comment


      • #4
        Re: VBS: migrating printers.

        Because the computer does not connect to the script (like in a case of a logon script), but in this case the script connects to the computer. The WMI service can now indeed not determine the default printer (I did not know that before you told me).

        There for alternatively you must collect that information from the current users part of the registry.
        And there you have an other obstacle.
        Since you make the connection to the remote computer with another account, you cannot use the HKEY_CURRENT_USER key to collect information about the other logged-on user. You have to use the key HKEY_USERS key now. But then you need the SID of the currently logged-on user for the correct hive.

        I used the "CheckDefaultPrinter" routine from your script. I added a part to it that determins the SID of the currently logged-on domain user first. And then create the correct registry path in HKEY_USERS.
        Code:
         Option Explicit
        
        Dim Strcomputer, DefPrinter
         
        strComputer = "."  '<------------------------------------------------remote computer
        
        
        CheckDefaultPrinter(strComputer)
          If DefPrinter = "New Printer" then WScript.Quit(0)
        
        'proceed...
        
        Wscript.quit 0
        
        
        
        Sub CheckDefaultPrinter(strComputer)
        
            Dim objWMIService, colInstalledPrinters, ObjPrinter
            Dim who, users, user, strUsr, strDomain, colAccounts
            Dim objAccount, strSID, oReg, strKeyPath1, DefPrinter
         
        '\
        '**********Begin "get SID"
        'first created on 7 march 07 by Remco Simons [nl]
        '(http://forums.petri.com/showthread.php?t=14109)
        '/
            Const HKEY_USERS = &H80000003
        
            who = "winmgmts:{impersonationLevel=impersonate}!//"& strComputer &""
            Set Users = GetObject( who ).InstancesOf ("Win32_ComputerSystem")
        
                for each User in Users
                    strUsr    = right(User.UserName,len(User.UserName)-InStr(User.UserName,"\"))
                    strDomain = left(User.UserName,InStr(User.UserName,"\")-1)
                    exit For
                next
        
            Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
            set colAccounts = objWMIService.ExecQuery _
                ("select * From Win32_UserAccount where name = '" & strUsr & "' " & _
                 "AND domain = '" & strDomain & "'")
        
                For each objAccount in colAccounts
                    strSID = objAccount.SID
                    exit For
                Next
        '**********End "get SID"
        
            'Read Default printer...
        
            strKeyPath1 = strSID & "\Software\Microsoft\Windows NT\CurrentVersion\Windows"
        
            Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & _
                                 "\root\default:StdRegProv")
        
            oReg.GetExpandedStringValue HKEY_USERS,strKeyPath1,"Device",DefPrinter
        
        
            'If the default printer points to the newly installed printserver, then quits...
        
         	If InStr (DefPrinter, "\\NL-NIE1-M027") <> 0 Then
        		DefPrinter = "New Printer" 
                         exit sub
         	Else 
         		WScript.Echo "New PrintServer not in use"
         		WScript.Echo "Current defaultprinter = " & DefPrinter
         	End If
        
        End sub
        \Rem
        Last edited by Rems; 28th August 2009, 23:23.

        This posting is provided "AS IS" with no warranties, and confers no rights.

        __________________

        ** Remember to give credit where credit's due **
        and leave Reputation Points for meaningful posts

        Comment

        Working...
        X