Trygve
(Fresh Scripter)
2005-11-17 01:37 PM
IF based on Orginzational Unit

Is it possible to use IF on OU, like we use IF ingroup ?

Mart
(KiX Supporter)
2005-11-17 01:41 PM
Re: IF based on Orginzational Unit

Welcome to the board.

You can't use ingroup to check on OU membership.
However there is a UDF here that can do this task for you.
InContainer() - Determines if the current account or computer is in a container


Trygve
(Fresh Scripter)
2005-11-17 01:46 PM
Re: IF based on Orginzational Unit

Thank you for faaast answaer
Will the syntax be : 'IF INCONTAINER ("OU")'

TR


Mart
(KiX Supporter)
2005-11-17 01:50 PM
Re: IF based on Orginzational Unit

The syntax is listed in the commented section (see examples part). Please also read the dependencies section (just above the examples part) cause it requires an extra UDF called TranslateName() and AD aware clients.

Trygve
(Fresh Scripter)
2005-11-17 01:54 PM
Re: IF based on Orginzational Unit

I'm sorry, I'm a rookie on kix, what would be the right syntax if I want to check a users OU membership syntax ?

Mart
(KiX Supporter)
2005-11-17 02:01 PM
Re: IF based on Orginzational Unit

Something like this should do.

You should change this part OU=test,OU=9826,OU=NCS,OU=Machines,DC=us,DC=tycoelectronics,DC=com to match your situation. The user to query is now set to the user the script runs on by the first line of code below.

Code:

$User = @USERID

$rc = InContainer ("OU=test,OU=9826,OU=NCS,OU=Machines,DC=us,DC=tycoelectronics,DC=com", $User)
Select
Case $rc[0]=1 ? "object is a member of the specified container."
Case $rc[0]=2 ? "object is a member of a child container lower in the hierarchy."
Case $rc[0]=0 ? "object is NOT a member of this container or a child of this container."
Case $rc[0]=-1 ? "InContainer() Error - Invalid input for $NameType "
Case $rc[0]=-2 ? "TranslateName() Error"
Case 1 ? "Unknown return code"
EndSelect


;Do NOT midify anything below this line!
;
;FUNCTION InContainer()
Function InContainer ($Container, $NameType)
Dim $CurrentContainer, $Name1, $Name2, $Found, $commaloc

Select
Case $NameType = "Computer" $Name1 = @Domain + "\" + @wksta + "$$"
Case $NameType = "User" $Name1 = @LDomain + "\" + @UserID
Case 1 $Name1 = ""
EndSelect

If $Name1 <> ""
$Name2 = TranslateName (3, "", 3, $Name1, 1)

If $Name2[1] = 0

$Found=0
While $Found=0
$commaloc = InStr($Name2[0], ",")
If $commaloc > 1
If SubStr($Name2[0],$commaloc-1,1) = "\"
$Name2[0] = SubStr($Name2[0], $commaloc+1)
Else
$Found=1
$CurrentContainer = SubStr($Name2[0], $commaloc+1)
EndIf
Else
$Found=1
EndIf
Loop

Select
Case $CurrentContainer=$Container $InContainer = 1, $Name2[1], $Name2[2]
Case InStr($Name2[0], $Container) $InContainer = 2, $Name2[1], $Name2[2]
Case 1 $InContainer = 0, $Name2[1], $Name2[2]
EndSelect
Else
$InContainer = -2, $Name2[1], $Name2[2]
EndIf
Else
$InContainer = -1, 0, ""
EndIf
EndFunction

;FUNCTION TranslateName()
Function TranslateName ($InitType, $BindName, $LookupNameType, $LookupName, $ReturnNameType)

Dim $InitType, $BindName, $LookupNameType, $LookupName, $ReturnNameType
Dim $NameTranslate, $ReturnName, $Error, $ErrorText

$Error = 0
$ErrorText = ""
$ReturnName = ""
$NameTranslate = CreateObject ("NameTranslate")
$Error = @error
$ErrorText = @serror
If $Error = 0
$NameTranslate.Init ($InitType, $BindName)
$Error = @error
$ErrorText = @serror
If $Error = 0
$NameTranslate.Set ($LookupNameType, $LookupName)
$Error = @error
$ErrorText = @serror
If $Error = 0
$ReturnName = $NameTranslate.Get($ReturnNameType)
$Error = @error
$ErrorText = @serror
EndIf
EndIf
EndIf
$TranslateName = $ReturnName, $Error, $ErrorText
EndFunction



Trygve
(Fresh Scripter)
2005-11-17 02:06 PM
Re: IF based on Orginzational Unit

Jeez, all this just to check if a user is member of an OU ???

Mart
(KiX Supporter)
2005-11-17 02:14 PM
Re: IF based on Orginzational Unit

Two functions are included in the code above. One to check if the user is a member and one to convert the name.

Richard H.Administrator
(KiX Supporter)
2005-11-17 02:46 PM
Re: IF based on Orginzational Unit

Quote:

Jeez, all this just to check if a user is member of an OU ???




Yes, KiXtart doesn't support the features natively, so it can get a little complicated.

However, for your needs the following might provide all the information that you require:
Code:
$=SetOption("WrapAtEOL","ON")

$oADInfo = CreateObject("ADSystemInfo")
$oADInfo.ComputerName ?
$oADInfo.UserName ?



Trygve
(Fresh Scripter)
2005-11-17 03:28 PM
Re: IF based on Orginzational Unit

I was going to use a check on OU, for mapping printers for users in that spesific OU.

ps. I am getting the error code 2 when using setdefaultprinter, any idea ?

This is my script :

IF INGROUP ("Stamsund")
If AddPrinterConnection ("\\aks01\hp4200-stamsund") = 0
? "Added printer connection...."
endif
SetDefaultPrinter("\\aks01\hp4200-stamsund")
? "Set default printer to HP LaserJet 4...."


Les
(KiX Master)
2005-11-17 03:29 PM
Re: IF based on Orginzational Unit

Is the printer AND the share name the same?

JochenAdministrator
(KiX Supporter)
2005-11-17 03:29 PM
Re: IF based on Orginzational Unit

You should use Addprinterconnection() first

Trygve
(Fresh Scripter)
2005-11-17 04:11 PM
Re: IF based on Orginzational Unit

yepp. I typed wrong. AddPrinterConnection is first. My problem with that one was wrong printername, when using SetPrinterDefault..

Trygve
(Fresh Scripter)
2005-11-17 04:13 PM
Re: IF based on Orginzational Unit

But I really need som good advice for a short script for checking before mapping a printer, with user in a OU, not a group...

Richard H. : Can I use you script for this purpose ?


Trygve
(Fresh Scripter)
2005-11-18 09:05 AM
Re: IF based on Orginzational Unit

Is there no good tips for using IF with OU check for users, when mapping printers ?

Richard H.Administrator
(KiX Supporter)
2005-11-18 10:25 AM
Re: IF based on Orginzational Unit

Quote:

Richard H. : Can I use you script for this purpose ?




Yes. Have you tried it? You should see your user path displayed.

Now you can use something like "InStr()" to check is you are in an OU.

Here is an example which checks if I'm in the IT staff OU:
Code:
$oADInfo = CreateObject("ADSystemInfo")

$sITOU="OU=IT Users,OU=I.T. Teams,OU=Leatherhead Office,OU=Corporate,DC=ACME,DC=com"

If InStr($oADInfo.UserName,$sITOU)
"Yes, I'm in the IT OU"+@CRLF
Else
"No, I'm not IT staff"+@CRLF
EndIf



Howard Bullock
(KiX Supporter)
2005-11-18 01:25 PM
Re: IF based on Orginzational Unit

I think you have already been given the best solution. Use InContainer().

As discussed in a previous thread InContainer always calls TranslateNAme() which can add substatial overhead to the script. A cleaner solution is to use TranslateName() to get the user's OU and then use a modified Incontainer() that only compares strings representations of the OU's DN.

I will post the code after I get to work.


Richard H.Administrator
(KiX Supporter)
2005-11-18 01:36 PM
Re: IF based on Orginzational Unit

Howard,

What's the issue with using the scriptlet I've just posted?

I'd planned on using it myself in various places so if there is a potential problem I'd like to know about it.


Howard Bullock
(KiX Supporter)
2005-11-18 05:34 PM
Re: IF based on Orginzational Unit

Looks good Richard. I was rushing around and did not spend sufficient time reading your post. You solution is elegantly simple and directly applicable to a single user logging on which satisfies the request.

I think that my solution is more flexible/powerful (shameless plug) from an admin script perspective.

Your solution only seems to identify that the user is somewhere in the OU tree. My solution determine if you are in the OU or a child.


flashcash5
(Lurker)
2006-01-24 11:14 PM
Re: IF based on Orginzational Unit

Hi All!
Good to be a member of the board, i've been looking through this forum alot, thanks to everyone for the help so far.

I am hoping to do the same thing Tryvge was trying to do in his last post (with InContainer).
For example if a user is in a certain OU map this printer, drive etc,
if a user is in this other OU do this, that etc..

Was just wondering if you had that sample code you mentioned by any chance please?
Quote:

I will post the code after I get to work.




Just if you have it handy thats all,

Thanks a mill


flashcash5
(Lurker)
2006-01-25 10:42 AM
Re: IF based on Orginzational Unit

With regard to the above, thanks anyway lads, i've used Richard H's script instead which did the job.

Thanks a million