Page 1 of 1 1
Topic Options
#116799 - 2004-03-25 08:46 AM Migrating batch script to KIX.
BjornTore Offline
Fresh Scripter

Registered: 2004-03-25
Posts: 5
I have a batch script that checks the group membership list for a group with the name *LOC*, then reads the comment field on this group from AD, then splits the comment field based on ";", then maps a network drive based on the second part of the comment field. If the mapping fails, then a "default" network drive is mapped.

Anyone want to give me some hints about how to convert it to kix ?

Batch Code
-------------------------------------
if exist w:\ net use /d W: >>%log% 2>&1

:FIND_W_GROUP
for /f "tokens=2 delims=\." %%A in ('%SystemDrive%\Temp\ifmember /list^|find /i "LOC"') do call :MAP_W %%A
goto check

:MAP_W
if /i "%1"=="" goto end
for /f "tokens=2 delims=;" %%A in ('%SystemDrive%\Temp\ntuser -s %LOGONSERVER% group show %1^|find/i "comment"') do net use W: %%A >>%log% 2>&1
goto end

:check
if not exist w:\ call :fail
goto end

:fail
echo ..We will map W:\ to the default tempdrive in Location.
echo %computername%;%gwadr%;%username%;%loc%;>>%loclog%\_Mapped_default.txt
net use W: \\Server\Share >>%log% 2>&1

:end

Top
#116800 - 2004-03-25 03:33 PM Re: Migrating batch script to KIX.
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Cross-post from http://www.scriptlogic.com/forums/display_message.asp?mid=11557
_________________________
There are two types of vessels, submarines and targets.

Top
#116801 - 2004-03-25 03:50 PM Re: Migrating batch script to KIX.
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4567
Loc: USA
There is a UDF here that might help: UserGroups() - returns all groups of a given user

And here is kind of a skeleton of what you might do to get started(untested)

Code:
 

$yourdomain="Test"
$findstr="LOC"
$groups=usergroups($yourdomain,@userid)
$counter=0
while $counter<=ubound($groups) and $found<>1
if instr($groups[$counter],$findstr)>0
$found=1

; your
;
; code
;
; here
;

endif
$counter=$counter + 1
loop



Top
#116802 - 2004-03-25 10:30 PM Re: Migrating batch script to KIX.
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4567
Loc: USA
Because I found this moderately interesting I decided to do a little more... this is as close to silver platter as it gets...

Code:
 

break on
$=setoption("WrapAtEOL","ON")
;**************************
;Get the user's groups
;**************************
$cn="cn=" + @fullname
$ou="ou=user,ou=xyz"
$dc="dc=your,dc=domain,dc=com"
$objUser = GetObject("LDAP://" + $cn + "," + $ou + "," + $dc)
$Groups = $objUser.Getex("memberof")
;***********************************
;Find the Groups with LOC and get the description
;only displaying the 2nd part by using the delimiter ";"
;***********************************

$findstr="LOC"
$delim=";"

$counter=0
while $counter<=ubound($groups) and $found<>1
if instr($groups[$counter],$findstr)>0
$found=1
$descrip=split(getobject("LDAP://" + $groups[$counter]).get("Description"),$delim)[1]
? $descrip
endif
$counter=$counter + 1
loop




Now all you should have to do is replace the variables and the ? $descrip line with whatever drive mapping you have. Hope this helps.

Top
#116803 - 2004-03-26 11:54 AM Re: Migrating batch script to KIX.
BjornTore Offline
Fresh Scripter

Registered: 2004-03-25
Posts: 5
Tanx.
I'll try it out.
Actually, this is very valuable in a big environment..
By setting the path to a share in the groups comment field, we simplify the administration a lot. No need to modify logonscript, or any other thing when moving shares around
BT

Top
#116804 - 2004-03-29 12:54 PM Re: Migrating batch script to KIX.
BjornTore Offline
Fresh Scripter

Registered: 2004-03-25
Posts: 5
Status Update.
Well, i really didnt get your code up and running, so i made some cuts and pastes and tied stuff together..
I think i have a problem with LDAP

Anyone finding anything wrong with the code below, or improvements, let me know.. Its going into production with 10000 users within a couple of months ...

Bjorn Tore

----------------------------------------------------------

; ======================================================================
;
;
; NAME: ConnectLocationDrive.Kix
;
; AUTHOR: Bjørn Tore Jakobsen
; DATE : 26.03.2004
;
; COMMENT: Basically what this does is find the first group the logged on user is a member of
; that starts with AK-LOC, then reads out the comment(description) field on the group.
; Then it maps the share specified in the comment field.
; If the user is not a member of such a group, it maps to a default share.
;
; ConnectLocationDrive("W:, AK-LOC, ;")
; First parameter = Drive to map to.
; Second parameter = first part of group name to search for
; Third Parameter = Delimiter in the groups comment field.
; NB! The path to the share has to be after the first ";" in the comment field.
; ========================================================================
;
;ConnectLocationDrive("W:, AK-LOC, ;")


Function ConnectLocationDrive( $DriveDefinition )

$DriveLetter=SubStr($DriveDefinition,1,InStr($DriveDefinition,",")-1)
$DriveDefinition=LTrim(RTrim(SubStr($DriveDefinition,InStr($DriveDefinition,",")+1)))
$GroupDescr=LTrim(RTrim(SubStr($DriveDefinition,1,InStr($DriveDefinition,",")-1)))
$DescDelim=LTrim(RTrim(SubStr($DriveDefinition,InStr($DriveDefinition,",")+1)))

$findstr = $GroupDescr
$delim = $DescDelim
$DefaultPath = "\\Server\DATABIN"

Global $NetPath[1]

;**************************
;Get the user's groups
;**************************

$LServer = Substr("@LSERVER",3)

$objUser = GetObject("WinNT://$LServer/@UserID,user")
$found = 0
For Each $group in $objUser.Groups
if InStr($group.name,$findstr)>0 and $found<>1
$found=1
$NetPath = Split($Group.description,$delim)
? "Found"
Use $DriveLetter /delete
;Progress("Connecting " + $DriveLetter + " to " + $NetPath[1])
Use $DriveLetter $NetPath[1]
EndIf
Next
If found = 0
? "not found"
Use $DriveLetter /delete
;Progress("Connecting Default" + $DriveLetter + " - " + $DefaultPath)
Use $DriveLetter $DefaultPath
EndIf

EndFunction


Top
#116805 - 2004-03-29 02:05 PM Re: Migrating batch script to KIX.
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
First I think you are trying to do too much in your script. Try breaking down the functionality into smaller pieces.

See my MapDrive function below. Write a different function to analyze your group membership then call MapDrive with the proper parameters.
Code:
Function MapDrive($Drive, $Server, $Share)
Dim $Drive, $Server, $Share, $shell
Color c+/n

If $Drive<>"" and $Server<>"" and $Share<>""
$LogText="Connecting $Drive to \\$Server\$Share"
? $LogText
USE $Drive /Delete /Persistent
USE $Drive "\\$Server\$Share"
If @error=0
color g+/n
$x=" - Success"
"$x"
If val($DOS) >= 5
$shell=createobject("shell.application")
$shell.namespace($Drive+"\").self.name=$Share + " on '" + $Server + "'"
$shell = 0
Endif
Else
color r+/n
$x=" - Failed: Error @error"
"$x"
$ErrorState=1
Endif
WriteLog ($LogText + $x)
WriteLog2("%temp%\MapDrive.log",$LogText + $x,1)
Color w+/n
Else
WriteLog ("Function 'MapDrive' called with invalid parameters: '$Drive', '$Server', '$Share'")
Endif
Endfunction



WriteLog() and WriteLog2() can be found in the UDF Library.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#116806 - 2004-03-29 03:06 PM Re: Migrating batch script to KIX.
BjornTore Offline
Fresh Scripter

Registered: 2004-03-25
Posts: 5
Tanx for the answer.
Yea, i agree. I should split up the script. I already have an other mapdrive function that i'll use:)
But is easier to do testing with everything in one function..
Regards
BT

Top
#116807 - 2004-03-29 05:56 PM Re: Migrating batch script to KIX.
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Quote:


But is easier to do testing with everything in one function..




Nope, it is not. The more functionality a function contains, the more can go wrong. Use functions for very specific tasks and compartmentalize your scripts into simple building blocks. The UDF Library already provides over 400 functions. Also, think about the functionality of a script before writing it in order to keep it simple.
_________________________
There are two types of vessels, submarines and targets.

Top
Page 1 of 1 1


Moderator:  Arend_, Allen, Jochen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 1057 anonymous users online.
Newest Members
ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder, M_Moore
17887 Registered Users

Generated in 0.063 seconds in which 0.029 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org