No announcement yet.

printer assignment wsh, wierd errors

  • Filter
  • Time
  • Show
Clear All
new posts

  • printer assignment wsh, wierd errors

    EDIT: Windows XP SP3, attached to a Windows Server 2003 domain infrastructure. We're upgrading to 2008R2 soon and I'm tasked with making everything as infrastructure independent as possible before the change over to a mixed environment.

    The errors I'm getting now appear random and do not lead me to any solution. Your help in explaining how stupid my lack of sleep is making me is appreciated.

    What I want to happen is that this script pulls a list of network printers from the local machine, then compares that with a defined array of printers and then adds any that it is missing from the array. The script has to be able define both the print server and the printer list in the script as we are prevented from creating any GPO's for this project. I will be able to edit the script to add in additional printers and change the print server if needed and the script must be able to handle these changes without any changes except to the static arrays. I thought I had it and I nailed down every error except for the obviously false ones like the one below.

    The current error is: (35, 3) Wrong number of arguments or invalid property assignment. Which means nothing to me as the referenced function has two inputs and two arguments.

    What I've tried:
    1. Ubound and Lbound error out and fail every time I try to implement them when comparing arrays.
    2. Nested 'For Each' loops fail out on the first line of the secondary loop. So I went to functions.
    3. When I function(ed) the primary functions I got errors at each variable so I added in ByVal, then tried changing the variable names.
    4. Now I'm getting errors about inadequate function arguments at lines and characters that are basic variable Boolean operations.

    <code begins>
    ' This script will iterate through all installed network printers and install those that are missing.
    Option Explicit
    Dim objSysInfo, strUserDN, objUser, arrContainers, arrOU, strOU
    Dim strParentDN, objNetwork, strPrtSvr, currentPrinters(), i, printer, myPrinters, systemPrinters, ouPrinters
    Dim strComputer, objWMIService, colItems, objItem, newPtrName, hasPtr, thisPtr, arrNetworkPrinters()
    '------------Set the Print Server here------------------
    strPrtSvr = "thePrintServer"
    hasPtr = 0
    '------------Setup Printers here------------------------
    redim exisPtrs(1)
    exisPtrs(0) = "IS01_HP_Color_4650"
    exisPtrs(1) = "IS02_HP_4200dtn"
    ' Tell the user what we're doing
    wScript.Echo "Making sure you have the right printers for your office..."
    ' Get the users OU
    strOU = getOU()
    ' Get all network printers
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set systemPrinters = objWMIService.ExecQuery("Select * from Win32_Printer Where Network = TRUE",,48)
    ' Depending on OU, loop through the network printers and if check if the new one exists, if not add it.
    Select Case strOU
        Case "EX-IS"
            ouPrinters = exisPtrs
            myPrinters = systemPrinters             '-----------------------  THIS IS LINE 35 ---------
            For Each printer in myPrinters
                if isInstalled(printer, ouPrinters) = 0 then
                    wScript.Echo "Looks like you already have this one: " & printer
                end if
        ' More offices will for here    
        Case Else
            wScript.Echo "Looks like you already have all the default printers."
    End Select
    ' Functions are below here
    Function isInstalled(ByVAL thisOne, ByVal thePrinters)
        wScript.echo thisOne
        dim aPrinter
        dim realityCheck
        dim newPtr
        wScript.echo "thisOne=" & "\\" & strPrtSvr & "\" & thisOne
        For Each aPrinter In thePrinters 
                wscript.echo "thisOne=" & "\\" & strPrtSvr & "\" & thisOne
                wscript.echo "objectID=" & aPrinter.DeviceID
                newPtr = "\\" & strPrtSvr & "\" & thisOne 
                if newPtr = aPrinter.DeviceID then
                    realityCheck = 1
                End if
        If realityCheck = 1 Then isInstalled = 1 Else isInstalled = 0 End If
    End Function
    Function installPrinter(ByVal thisOne)
        newPtrName = "\\" & strPrtSvr & "\" & thisOne
        wScript.Echo "Looks like you need this one: " & newPtrName
        wScript.Echo "Adding " & newPtrName & " to your computer..."
        objNetwork.AddWindowsPrinterConnection newPtrName
    end Function
    Function getOU()
        ' Retrieve DN of current user.
        Set objSysInfo = CreateObject("ADSystemInfo")
        strUserDN = objSysInfo.UserName
        ' Excaping forward slashes not needed.
        ' Escape any forward slash characters.
        '    strUserDN = Replace(strUserDN, "\", "")    ' Bind to current user object.
        Set objUser = GetObject("LDAP://" & strUserDN)
        ' Retrieve DN of parent OU/Container.
        strParentDN = objUser.Parent
        ' Map printers according to parent OU.
        Set objNetwork = CreateObject("Wscript.Network")
        ' Parse Parent DN into comma delimited components.
        arrContainers = Split(strParentDN, ",")
        ' Parse the first component to retrieve name of the OU/Container.
        arrOU = Split(arrContainers(0), "=")
        strOU = arrOU(1)
        Wscript.Echo "The Network says you are a memeber of MYCOMPANY-" & strOU & "."
        getOU = strOU
    end Function
    <code ends>
    Last edited by Rems; 29th March 2012, 16:03. Reason: keyboard met face

  • #2
    Re: printer assignment wsh, wierd errors

    In case anyone looks this up in the future I started from scratch and re-wrote the whole thing. It works now.

    ' This script will iterate through all installed network printers and install those that are missing.
    ' Editable Variables
    ' Declaring Variables
    dim userDN, parentDN, tempArray, tempArray2, myOU
    dim thisComputer, printServer, tempPrinter
    printServer = "\\thePrintServer\"
    ' Setup System level stuff
    Set WshNetwork = CreateObject("Wscript.Network")
    set localSystemInfo = CreateObject("ADSystemInfo")
    set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
    set localPrinters = objWMIService.ExecQuery("Select * from Win32_Printer Where Network = TRUE",,48)
    userDN = localSystemInfo.UserName
    Set objUser = GetObject("LDAP://" & userDN)
    parentDN = objUser.Parent
    tempArray = Split(parentDN, ",")
    tempArray2 = Split(tempArray(0), "=")
    myOU = tempArray2(1)
    For Each localPrinter in localPrinters
        Select Case myOU
            Case "EX-IS"
                tempPrinter = "IS01"
                If (localPrinter.DeviceID <> printServer & tempPrinter) Then WshNetwork.AddWindowsPrinterConnection printServer & tempPrinter End If
                tempPrinter = "IS02"
                If (localPrinter.DeviceID <> printServer & tempPrinter) Then WshNetwork.AddWindowsPrinterConnection printServer & tempPrinter End If
        End Select
    Last edited by Rems; 29th March 2012, 16:02.