Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't get this printer script to work

Status
Not open for further replies.

Raziel014

Technical User
Nov 1, 2005
51
NO
I'm working on this script which is supposed to map a printer based on computer membership. But I can't it to work. Could anyone take a look at it please?

Note that I've put a msgbox strPrinter at the bottom just to find out if the function is working. And the message box doesn't even appear!

Code:
Function IsComputerMember(sGroup)
    Dim oGroup
    on error resume next
    Set oGroup = GetObject("WinNT://" & strDomain & "/" & sGroup & ",group")
    IsComputerMember = CBool(oGroup.IsMember(objComputer.ADsPath & "$"))

Set objNetwork = WScript.CreateObject("WScript.Network")
' Navn på domene
strDomain = "Ringve"

' Hent datamaskinnavn
strComputerName = objNetwork.ComputerName

' Last inn referanse til vår datamaskin
Set objComputer = GetObject("WinNT://" & strDomain & "/" & strComputerName & ",computer")

'Legger til Printer
strPrinter = ""

Select Case CBool(-1)
 Case isComputerMember("Gymkontor")
     strPrinter = "\\Ringve-fil\Dell_Laser_Gymkontor"
 Case isComputerMember("Lærerbakrom")
     strPrinter = "\\Ringve-fil\Fargelaser_lærerbakrom"
 Case isComputerMember("Bibliotek")
     strPrinter = "\\Ringve-fil\Dell_Laser_Bibliotek"
 Case isComputerMember("FO03")
     strPrinter = "\\Ringve-fil\Dell_Fargelaser_FO03"
 Case isComputerMember("AA05")
     strPrinter = "\\Ringve-fil\HPLaser_AA05"
 Case isComputerMember("AA07")
     strPrinter = "\\Ringve-fil\HPLaser_AA07"
 Case isComputerMember("AA08")
     strPrinter = "\\Ringve-fil\HPLaser_AA08"
 Case isComputerMember("Østmarka IT")
     strPrinter = "\\Ringve-fil\Lexmark_Østmarka_IT-rom"
 Case isComputerMember("Østmarka lærerrom")
     strPrinter = "\\Ringve-fil\Kopimaskin_PHST-Lærerrom"
 Case isComputerMember("Arbeidsrom 3 og 4")
     strPrinter = "\\Ringve-fil\Kopimaskin_arbeidsrom_3og4"
 Case Else
     MsgBox "No Printer"
End Select
'objNetwork.AddWindowsPrinterConnection strPrinter
End Function
MsgBox strPrinter
 
Ok, the messagebox does appear but it doesn't reply a value. It's just an empty messagebox...
 
well you have confused the hell out of me.
i would suggest that your Select Case statement should not be in your IsComputerMember function. by its name i would suggest IsComputerMember should only be returning a boolean of if the machine is a member of the group.

infact i would suggest that seeing as your script seems to be running per machine then you would be better off grabbing a dictionary of which groups the machine is a member of and using this to compare against your target printer groups. that way you dont have to bind to every group for every machine, you only need to bind to one AD object....should be much much faster

 
You have to make a call to the function first.
The MsgBox is outside and better be :

MsgBox(IsComputerMember(sGroup))

Then take away the "On Error Resume Next".
That could give you a hint......

Everything seems to be in the wrong order....?
 
Try This :

Code:
Option Explicit

Dim strComputerName, objNetwork, objComputer 
Dim arrPrinters, colObject

Set objNetwork = CreateObject("WScript.Network")
Set objComputer = GetObject("WinNT://" & objNetwork.UserDomain & "/" & objNetwork.ComputerName & ",computer")

Dim colPrinters(10)
colPrinters(0) = "Gymkontor;\\Ringve-fil\Dell_Laser_Gymkontor"
colPrinters(1) = "Lærerbakrom;\\Ringve-fil\Fargelaser_lærerbakrom"
colPrinters(2) = "Bibliotek;\\Ringve-fil\Dell_Laser_Bibliotek"
colPrinters(3) = "FO03;\\Ringve-fil\Dell_Fargelaser_FO03"
colPrinters(4) = "AA05;\\Ringve-fil\HPLaser_AA05"
colPrinters(5) = "AA07;\\Ringve-fil\HPLaser_AA07"
colPrinters(6) = "AA08;\\Ringve-fil\HPLaser_AA08"
colPrinters(7) = "Østmarka IT;\\Ringve-fil\Lexmark_Østmarka_IT-rom"
colPrinters(8) = "Østmarka lærerrom;\\Ringve-fil\Kopimaskin_PHST-Lærerrom"
colPrinters(9) = "Arbeidsrom 3 og 4;\\Ringve-fil\Kopimaskin_arbeidsrom_3og4"




For Each colObject in colPrinters
    If Not IsEmpty(colObject) Then
        arrPrinters = Split(colObject, ";")
        If IsComputerMember(arrPrinters(0)) Then
            Err.Clear
            objNetwork.AddWindowsPrinterConnection arrPrinters(1)
            If Err.Number = 0 Then
                Wscript.Echo arrPrinters(1) & " connected"
            Else
                Wscript.Echo "Error on connecting " & arrPrinters(1)
            End if
        End if
    End if
Next 




Function IsComputerMember(sGroup)
    
   Dim oGroup
   Set oGroup = GetObject("WinNT://" & objNetwork.UserDomain & "/" & sGroup & ",group")
   IsComputerMember= oGroup.IsMember(objComputer.ADsPath)
   Set oGroup = Nothing

End Function
 
Hmm, just tried the last one and nothing is happening. If I just want to test it to see what value is returned from the IsComputerMember, where should I put the MsgBox IsComputerMember?

I tried putting it below End Function and an error message appears stating that the variable "sGroup" is not defined
 
Here :

Code:
IsComputerMember= oGroup.IsMember(objComputer.ADsPath)
[COLOR=red]
Wscript.Echo oGroup.IsMember(objComputer.ADsPath)
[/color]
Set oGroup = Nothing
 
What, you mean I should put in the red line between the others at the bottom of the script? I just tried that and put a messagebox and I got "0" in the messagebox plus that the box itself showed up alot of times. And at the end, I got the same error. "sGroup" is not defined.
 
>What, you mean I should put in the red line between the others at the bottom of the script?
Why are you so surprise? You asked for advice and reasonable advice was given.

>I just tried that and put a messagebox and I got "0" in the messagebox plus that the box itself showed up alot of times
0 means false, meaning not member. "alot of times"? Can you not count? There are 10 calls to the function, and you get 10 messge boxes or about. What is the surprise?

If you just copy some other person script, do it correctly. And to do it correctly, it needs some basic effort on your part, rather than sit back and ask every trivial thing and be surprised by even good advice every time.

You original copy is not that far.
[tt]
Function IsComputerMember(sGroup)
Dim oGroup
on error resume next
Set oGroup = GetObject("WinNT://" & strDomain & "/" & sGroup & ",group")
IsComputerMember = CBool(oGroup.IsMember(objComputer.ADsPath & "$"))
[blue]End Function[/blue]

Set objNetwork = WScript.CreateObject("WScript.Network")
' Navn på domene
strDomain = "Ringve"

' Hent datamaskinnavn
strComputerName = objNetwork.ComputerName

' Last inn referanse til vår datamaskin
Set objComputer = GetObject("WinNT://" & strDomain & "/" & strComputerName & ",computer")

'Legger til Printer
strPrinter = ""

Select Case CBool(-1)
Case isComputerMember("Gymkontor")
strPrinter = "\\Ringve-fil\Dell_Laser_Gymkontor"
Case isComputerMember("Lærerbakrom")
strPrinter = "\\Ringve-fil\Fargelaser_lærerbakrom"
Case isComputerMember("Bibliotek")
strPrinter = "\\Ringve-fil\Dell_Laser_Bibliotek"
Case isComputerMember("FO03")
strPrinter = "\\Ringve-fil\Dell_Fargelaser_FO03"
Case isComputerMember("AA05")
strPrinter = "\\Ringve-fil\HPLaser_AA05"
Case isComputerMember("AA07")
strPrinter = "\\Ringve-fil\HPLaser_AA07"
Case isComputerMember("AA08")
strPrinter = "\\Ringve-fil\HPLaser_AA08"
Case isComputerMember("Østmarka IT")
strPrinter = "\\Ringve-fil\Lexmark_Østmarka_IT-rom"
Case isComputerMember("Østmarka lærerrom")
strPrinter = "\\Ringve-fil\Kopimaskin_PHST-Lærerrom"
Case isComputerMember("Arbeidsrom 3 og 4")
strPrinter = "\\Ringve-fil\Kopimaskin_arbeidsrom_3og4"
Case Else
MsgBox "No Printer"
End Select

[blue]if strPrinter<>"" then
objNetwork.AddWindowsPrinterConnection strPrinter
msgbox "Printer connected to " & strPrinter
else
msgbox "Your computer is not member of any target group and not printer is not established."
end if[/blue]

[red]'[/red]End Function
[red]'[/red]MsgBox strPrinter
[/tt]
The major frame is there. The rest is just refinement.
 
Thanks! Just tried it and it does work, only problem is I can't figure out why there's only one printer as a result. I've done similar scripts before and got more than one result. When I removed the printer from the result, I got the other printer I'm supposed to get. Is there some way to get the script to do more than one at a time?
 
Sorry....forgot your & "$" to objComputer.ADsPath
You can also add IsComputerMember = False as the first statement in the function. Then get rid of the Cbool function. The known "bug" with true or false is that 0 = False ! everything else = True
It can in fact be -1 or just 1. This depends on what provider your use to query the domain.


As tsuji wrote I've made the loop 10 times, in case a computer are member of more than 1 printergroup. Your script just checks 1 one time and then leaves the Select- statement.

There are probably another 100 ways to write this loop.
You can read the array from argument, dictionary, collection, MemberOf in AD.........

I think you should visit Microsofts web and download some of their scripting helpfiles. There are also a lot of examples and good stuff "out there".
Another tip is to run your scripts in a command-window and set cscript as your default. Don't use those anoying MsgBox:es. Use Wscript.Echo instead.

 
I don't know where in Norway you are located, but if it isn't to far away....you can invite me to a holiday in Oslo with Øl-drinking on Akkes-Brygge, Then I'll teach you all about "The Windows Scripting Secrets" [party]
 
Hello again! I live in Trondheim and haven't been to Oslo for ages. Anyway, I'm not sure I understand what you mean with the & "$" to objComputer.ADsPath. Isn't that what it already says? Atleast that's what's in the latest script that tsuji wrote.

And about the isComputerMember = False, you mean putting this in remove the Cbool and replace Cbool with False?

Should it then be like this?:

IsComputerMember = False(oGroup.IsMember(objComputer.ADsPath & "$"))

??

Should I then edit the "Select Case CBool(-1)" too to "Select Case False(-1)"? Or "Select Case False (1)?

 
I'm not sure what script your talking about. The one Tsuji wrote or the one you wrote previous?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top