Page 1 of 2 12>
Topic Options
#185495 - 2008-02-20 02:27 AM My logon script - looking for suggestions
lukeod Offline
Getting the hang of it

Registered: 2008-01-11
Posts: 70
Loc: Australia
Hello,

I've just redone our generic logon script to be pushed out to 27 different schools, and i am looking for a bit of feedback. I have very little programming experience (a semester of VB.NET at highschool), so take it easy!!.

A few notes:

>Been done so it's easy for IT staff with no KIX experience can see without too much trouble, what groups map which printers and drives.

>Put $enablelogging at the top of the script so the staff can easily find it

> $enablelogging is to make it easier to diagnose logon script problems, by piping output to c:\temp\[username].txt

> Being run only at logon

> All clients are XP w/ SP2. Servers are 2003. Running latest version of kix32.exe

Here it is in all it's glory:

 Code:
;*************************************************************************
;  Script Name:		Generic Logon Script
;  Script Version:	1.2
;  Author:			Luke
;  Date:			19/02/2008
;  Description:		This script is run during logon.
;			It maps drives, printers and calls other scripts
;			such as audit wizard and wireless.
;  Changelog:		1.0 - Initial Release (14/02/2008)
;			1.1 - Improved Logging, switch at top of the page. 
;			    - Replaced 'Case' statements with IfThenElse
;			1.2 - Added Variable passing of $nasip to wireless script	
;*************************************************************************

$enablelogging = 0		;Change to 1 to enable logging, pipes output to c:\temp\[username].txt, ensure priviliges are sufficent.
			;Leave at 0 during normal operation

;NOTE: For best viewing, disable word wrap

;====================================================================================================
;							VARIABLES
;							---------
;In this section, ALL used variables should be declared.
;Comment any variable which is not self explanitory, and group like variables together
;Keep variable names all in lowercase
;Before ever deleting or renaming a variable, search for it as it may be called in multiple sections
;====================================================================================================

;-----------------------------
;Common Variables
;May be used in multiple areas
;-----------------------------

$domain = @DOMAIN				;Returns the DFS name of the server, such as [domain] or [domain2]
$servername = @LSERVER + "\"		;@LSERVER returns \\servername of AD server such as "\\[an AD server name]"
$netlogon_path = $servername + "NETLOGON\"	
$nasip = "[insert NAS Server IP address here]"	;Required to call Wireless Script

;------------------------
;Mapped Printer variables
;------------------------

	;Printer Groups

$printgroup1 = "[PrinterGroupName]"
$printgroup2 = "[PrinterGroupName]"
$printgroup3 = "[PrinterGroupName]"

	;Printer Paths

$printer1 = $servername + "[PrinterShareName]"
$printer2 = $servermame + "[PrinterShareName]"
$printer3 = $servername + "[PrinterShareName]"

;-----------------------
;Mapped Drive Variables
;-----------------------

	;Drive Groups

$drivegroup1 = "[Drivegroup1]"
$drivegroup2 = "[Drivegroup2]"
$drivegroup3 = "[Drivegroup3]"
$drivegroup4 = "[Drivegroup4]"

	;Drive Paths

$drivepath1 = $servername + "ShareName"
$drivepath2 = $servername + "ShareName"
$drivepath3 = $servername + "ShareName"
$drivepath4 = $servername + "ShareName"

;-------------------------
;Wireless Script Variables
;-------------------------

$wirelessgroup = "[Wireless Group Name]"
$inipath = "c:\temp\variables.ini"

;-------------------------
;Event Logging Variables
;-------------------------

$logpath = "c:\temp\" + @USERID + ".txt"
$extralogcommands = '%comspec% /c "IPCONFIG /all >> $logpath"'	;Executed if $enablelogging = 1
$index = 0			;Loop Counter

;============================================================
;			Logging
;Pipes output to c:\temp\[username].txt
;Overwite enabled by '1' switch
;To enable, set $enablelogging to 1, at top of script
;Also executes $extralogcommands and appends it to $logpath
;============================================================

IF $enablelogging = 1
	REDIRECTOUTPUT($logpath,1)
	? " "
	? "----------------------------------"	
	? "Session Information:"
	? "----------------------------------"
	? " "
	? "User ID:  	 " + @USERID
	? "Domain:   	 " + @DOMAIN
	? "Date:     	 " + @DATE
	? "Time:     	 " + @TIME
	? "Computer Name   " + @WKSTA
	? "OS Version      " + @PRODUCTTYPE
	? "Logon Server	 " + @LSERVER
	SHELL $extralogcommands
	? "----------------------------------"
	? " "
	? "---------------------------------"
	? "User: " @USERID " is a member of:"
	? "---------------------------------"

;Loop to output groups the USER is a member of
	DO
		$group = ENUMGROUP($index)
		$index = $index + 1
		if LEN($group) > 2
			? "-> " $group
		ENDIF
	UNTIL Len($group) = 0
ENDIF
;--------------------------------------------------
;			Map Printers
;			------------
;Maps printers by Passing the Printername to the
;Function "computeringroup"
;--------------------------------------------------

IF computeringroup($printgroup1,$domain)
	mapprinter($printer1)
ENDIF

IF computeringroup($printgroup2,$domain)
	mapprinter($printer2)
ENDIF

IF computeringroup($printgroup3,$domain)
	mapprinter($printer3)
ENDIF

;--------------------------------------------------
;			Map Drives
;			----------
;Maps printers by Passing the drivepath and letter
;to the Function "mapdrive"
;--------------------------------------------------

IF ingroup($drivegroup1)
	mapdrive($drivepath1,"[DriveLetter]")
ENDIF

IF ingroup($drivegroup2)
	mapdrive($drivepath2,"[DriveLetter]")
ENDIF

IF ingroup($drivegroup3)
	mapdrive($drivepath3,"[DriveLetter]")
ENDIF	

IF ingroup($drivegroup4)
	mapdrive($drivepath4,"[DriveLetter]")
ENDIF

;--------------------------------------------------
;	Calls the Wireless Script
;--------------------------------------------------


?"netlogon path is: " $netlogon_path
IF computeringroup($wirelessgroup,$domain)
	?"Computer is in the correct group ($wirelessgroup) to recieve Wireless Settings"
	?"Creating INI file..."
	WRITEPROFILESTRING("c:\temp\variables.ini", "vars", "nasip", $nasip)
	
	IF @error = 0
		?"INI created Successfully!"
	ELSE
		?"INI NOT created succesfully, error code: " @ERROR
		?"Skipping wireless script"
		GOTO "EndWirelessScript"
	ENDIF
	
	?"Running Wireless Script...."
	RUN "%comspec% /c " + $netlogon_path + "CPAU.exe -dec -file " +  $netlogon_path +  "wireless.job -lwop"
ENDIF

:EndWirelessScript
;--------------------------------------------------
;	Calls the Audit Wizard Script
;--------------------------------------------------

RUN "%comspec% /c " + $netlogon_path + "CPAU.exe -dec -file " +  $netlogon_path +  "audit.job -lwop"
?"Running Audit Wizard Script..."

;******************************************************************************
;/////////////////////////////////////////////////////////////////////////////*
;\\\\\Called Functions, DO NOT MODIFY!!!!!!!!\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
;/////As it may prevent large sections of the above script from working///////*
;\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
;******************************************************************************
	;--------------------------------
	;Called to check group membership
	;--------------------------------

Function ComputerInGroup($group1,$domain)
   Dim $oGrp
   if not $domain      $domain=@domain      endif
   $oGrp = GetObject("WinNT://" + $domain + "/"  + $group1 + ",group" )
   if @error   exit 1      endif

   if $oGrp.IsMember("WinNT://" + $domain + "/"  + @wksta + "$$" )
      $ComputerInGroup=1
   else
      $ComputerInGroup=0
      endif
endfunction
	
	;---------------------
	;Called to Map Printer
	;---------------------

function mapprinter ($printer)
	if ADDPRINTERCONNECTION ($printer) = 0
		? "  Printer " $printer " added successfully"
		else
			?"*************************************************"
			?"Printer (" $printer ") was not added successfully!!!!!"
			?"Please contact the IT Department"
			?"*************************************************"
			Sleep 5
	endif
endfunction

	;---------------------
	;Called to Map Drive
	;---------------------

function mapdrive ($drivepath,$driveletter)
	Use $driveletter $drivepath
	If @ERROR=0
		?"Drive: " $drivepath " was mapped successfully!"
		ELSE
			?"*************************************************"
			?"Drive ( $drivepath ) was NOT mapped!!!!!"
			?"Please contact the IT Department"
			?"*************************************************"
			Sleep 5
	ENDIF
endfunction

;******************************************************************************
;/////////////////////////////////////////////////////////////////////////////*
;\\\\\\\\\\\\\\\\\\\\\\END OF CALLED FUNCTIONS\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
;/////////////////////////////////////////////////////////////////////////////*
;\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
;******************************************************************************


I've implemented it at 1 site for a dummy run and has been a week without any complaints!

Thankyou in advance

Luke


Edited by lukeod (2008-02-21 04:49 AM)
Edit Reason: Changed script a bit

Top
#185496 - 2008-02-20 02:41 AM Re: My logon script - looking for suggestions [Re: lukeod]
lukeod Offline
Getting the hang of it

Registered: 2008-01-11
Posts: 70
Loc: Australia
I also considered removing the need for an IF ENDIF statement for each printer and drive, replacing it with a DO WHILE loop containing a counter and using ISDECLARED or somthing like that.

It'd work except that there are some instances where multiple printers would be called with the one group \:\(

Top
#185497 - 2008-02-20 03:56 AM Re: My logon script - looking for suggestions [Re: lukeod]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
First thought = Get rid of any screen output, it is usefull in debuggin but your users are not going to care or pay attention to after the first 5 times of seeing it.


Your printer section, do they need mapping to more than one printer? If so then your method is fine, otherwise go back to a Select Case statements, as the processing of the Select ends upon the first match.

Overall not to bad. But you may want to look at what is considered by many here to be the "good scripting" practices and include this at the very top of your script...

 Code:
If NOT @LOGONMODE
	          Break On
EndIf

$SO=SETOPTION("Explicit", "ON")
$SO=SETOPTION("NoMacrosInStrings", "ON")
$SO=SETOPTION("NoVarsInStrings", "ON")
$SO=SETOPTION("WrapAtEOL", "ON")


Using these statements will make you go back and rewrite some of it as you have variables and macros in strings.

You will also have to declare all variables before they are used.
_________________________
Today is the tomorrow you worried about yesterday.

Top
#185498 - 2008-02-20 04:26 AM Re: My logon script - looking for suggestions [Re: Gargoyle]
lukeod Offline
Getting the hang of it

Registered: 2008-01-11
Posts: 70
Loc: Australia
Some computers (printers mapped based on computer instead of user) are a member of multiple print groups, hence IF + ENDIF.

It was never a case statement - originally i had the mapped drives in SELECT CASE structures, until i realised that some admin staff map multiple drives.

Is there any documentation on "good practices"? As i'm unsure as to how to best declare variables. Should i DIM the variable on one line, then set its value on another?

Also what is the significance of $SO ? I've seen it used a bit in the learning forums where people posted solutions to example problems, but am yet to get my head around it.

As for the splash screen - its a good point, i might wrap the lot into "IF $enablelogging = 1 ...." statement, so when set to '1' it'll output a text file with quite alot of information (session info + NIC details).

Glen mentioned in another thread that it's not best practice to use the ? " some text to display". In my script, what would be the best way of 'echoing' text to the console / logging text file?

Thanks for the reply

Luke

Top
#185499 - 2008-02-20 07:15 AM Re: My logon script - looking for suggestions [Re: lukeod]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
Good Practice, read this board frequently and FAQ section. As for any official documentation, not that I am aware of.

$SO is a variable used to catch the output of function to prevent any output to the screen (SetOption in this case), many others use "$" for the same. If you leave it off you will see lots of 0's 1's and occasionaly other numbers appear on your console. There is a FAQ on it.

Your idea of changing the output to be only during logging may not be the best, as you would most likely be troubleshooting a particular user / workstation, and if you set the variable globally in the logon script, it would effect everyone that logons during that time.

I personally leave the If $logging = 1... in my scripts but when troubleshooting a particular user / workstation, run the script in debug mode - kix32 \\logonserver\netlogon\logon.kix /d - and set the variable in the debug mode.

What Glen was referring to was simply the results of the formatting on screen, in KiX "?" is = CRLF (carriage return line feed). When writing to a text file you would use the @CRLF macro to accomplish the same thing. To echo text to the console you simply put it on line
 Code:
For $ = 1 to 100
cls
"this is line " $ 
sleep 0.25
Next
_________________________
Today is the tomorrow you worried about yesterday.

Top
#185504 - 2008-02-20 12:48 PM Re: My logon script - looking for suggestions [Re: Gargoyle]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
Hey Gargoyle!

When you're presenting an example on "?" or "@CRLF", it's best to actually include at least ONE of those in your example!

OK - back to the regularly scheduled example:
 Code:
For $ = 1 to 100
  cls
  "this is line " $ ?     ; Can use "?" or "@CRLF" here interchangably
  sleep 0.25
Next

That will print 100 lines - here's another common way NOT to use @CRLF to display 100 pieces of info, but only one line.
 Code:
For $ = 1 to 100
  cls
  "this is line " $ "     " Chr(13)
  sleep 0.25
Next
@CRLF

Note that there are some blank spaces printed after the "$" value. In this simple example, it insures that a large number followed by a small one will be displayed correctly (think about displaying 100 Random numbers, rather than sequential). When displaying random status lines in this manner, you might display 79 blanks, a Chr(13), and then your message and another Chr(13). A final @CRLF at the end insures that any following text begins on its own line.

Gargoyle brings up some excellent points (even without the ?). I, too, place output throughout my code to aid in debugging. I have a MSG() library that includes a DBG UDF. It outputs only if a global $DEBUG var is true, and writes to a log if a global $LOG_FILE_ var is defined, otherwise outputs to the screen. I often can control the value of $DEBUG in several ways - $DEBUG=1 on the command, via a --D:1 arg on the command line that the script interprets, reading a config file, finding a DEBUG.TXT file in the working folder, or if certain errors occur.

My comment about "?" placement is based on the following logic:
It is a "shortcut" for @CRLF. This sequence traditionally comes at the end of a line of text, which is why it's known as a "line termination sequence". If you place it first, it outputs your text AFTER moving to a new line, leaving your cursor where the text ends. Any following messages that you OR THE SYSTEM output will be placed there instead of at the beginning of a line. This can make it difficult to spot errors in some situations.

The confusion comes about because certain versions of BASIC, going back as far as I can remember (and digging out my old BYTE magazines from the 1970's), used the "?" character as a shortcut for the PRINT statement. This was the first character, followed by the text to be output, followed by an optional semicolon to suppress the CRLF.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#185505 - 2008-02-20 01:34 PM Re: My logon script - looking for suggestions [Re: Glenn Barnas]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
Glenn, you are correct. I was not really trying to show the use of "?", but how to simply put text on the console. I can see how it would be confusing considering the prior explanation.
_________________________
Today is the tomorrow you worried about yesterday.

Top
#185507 - 2008-02-20 02:55 PM Re: My logon script - looking for suggestions [Re: Gargoyle]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
Yeah, well, we'll forgive you (this time) considering the late hour of your post. \:\)

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#185523 - 2008-02-21 01:41 AM Re: My logon script - looking for suggestions [Re: Glenn Barnas]
lukeod Offline
Getting the hang of it

Registered: 2008-01-11
Posts: 70
Loc: Australia
Ahhh okay, i think i get it. Thanks Glenn and Gargoyle!. I am still very "noob" in all ways programming! That does make sense - my perception of the '?' character was to tell kix32.exe to 'not execute this line as code, but output it'. Thanks for the clarification. In future i'll carrige return after a line instead of before it.

I was thinking that simply putting text in quotation marks would result in some sort of syntax error.

I've taken Gargoyle's advice on the splash screen and instead, wrapped it into a large 'If $enablelogging = 1 [splash screen] ENDIF'.

I think i'll leave the switch at the top as the primary means of enabling logging, due to it reletivly speaking, is easier to explain to an onsite tech who has absolutly no programming knowledge to change an obvious character (due to comments) in a script rather than debugging the script and feeding the value for the variable in.

I'm not too concerned with the switch effecting everyone who logs on - due to it only really piping output to a text file, it may take an extra... 2 seconds over the usual?

Attempting to code it in a 'troubleshooting for dummies' style :S.

Thanks


Edited by lukeod (2008-02-21 01:42 AM)

Top
#185528 - 2008-02-21 02:49 AM Re: My logon script - looking for suggestions [Re: lukeod]
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Ah don't let these guys bully you around. I still stick my ? marks at the beginning of all my output lines ... If you stick them at the end, just makes your script look like a bunch of "unanswered questions" ;-)
Top
#185529 - 2008-02-21 03:02 AM Re: My logon script - looking for suggestions [Re: Shawn]
lukeod Offline
Getting the hang of it

Registered: 2008-01-11
Posts: 70
Loc: Australia
Haha it does too :P

I think i'm much more likley to forget about putting them in if i put them after the line \:\(

Cheers

Luke

Top
#185534 - 2008-02-21 06:02 AM Re: My logon script - looking for suggestions [Re: lukeod]
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Well look at it this way, lets say you want to put out a message saying that your install failed ... using that wussy way above it looks like this:

"The install failed" ?

Not very convincing coding if you ask me. It looks like the script is asking if the install failed. I mean, whats with that ?

No, here's the "manly" way of coding that:

? "The install failed"

Now your talking. Can't mistake that. The install failed! And if you don't like it, tough. Manly coding - not wussy coding.

-Shawn ;-)



Top
#185537 - 2008-02-21 07:19 AM Re: My logon script - looking for suggestions [Re: Shawn]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
Shawn go back to your sheep and leave us Manly Men alone \:D
_________________________
Today is the tomorrow you worried about yesterday.

Top
#185538 - 2008-02-21 07:51 AM Re: My logon script - looking for suggestions [Re: Gargoyle]
lukeod Offline
Getting the hang of it

Registered: 2008-01-11
Posts: 70
Loc: Australia
'Manly' coding eh?

Perhaps you could write a manual on how to code in this manly fashion.

Or even make a UDF to input a 'wussy' script and output a 'manly' script. Mabey chuck in some MR T Variables like $ThisIsACounterFOOL

You wouldnt have to worry about catching errors, because manly scripts dont have errors.


Edited by lukeod (2008-02-21 07:54 AM)

Top
#185541 - 2008-02-21 08:24 AM Re: My logon script - looking for suggestions [Re: lukeod]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
LOL

Oh sorry LOL is kinda not manly. Lets use (with a Barry White tone) Hahahahahaha!
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#185546 - 2008-02-21 09:31 AM Re: My logon script - looking for suggestions [Re: Mart]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
No homophobia or sexism here, it was a typo - he meant to say "Mainly Men"
Top
#185549 - 2008-02-21 12:52 PM Re: My logon script - looking for suggestions [Re: Richard H.]
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
All right, all right. I will do the "manly" thing and admit that I am wrong. Putting the question mark last is probably "most correct".
Top
#185550 - 2008-02-21 01:25 PM Re: My logon script - looking for suggestions [Re: Shawn]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Urban tek warriors don't use "?" at all, it's only there for old duffers who living a moribund halcyon dream.

We use the righteous @CRLF - and none of that vague coding either, "Explicit Catenation!" is our mantra, feel the steely resolution of my code.

Ooerrr Nurse! It's happened again...

Top
#185551 - 2008-02-21 01:49 PM Re: My logon script - looking for suggestions [Re: Richard H.]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
Feeling a little "sheepish" now, Shawn?

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#185559 - 2008-02-21 02:48 PM Re: My logon script - looking for suggestions [Re: Glenn Barnas]
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Feeling that I shouldn't argue with the likes of you guys. You guys aren't lazy coders like I am.
Top
Page 1 of 2 12>


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

Who's Online
0 registered and 194 anonymous users online.
Newest Members
BeeEm, min_seow, Audio, Hoschi, Comet
17882 Registered Users

Generated in 0.05 seconds in which 0.023 seconds were spent on a total of 14 queries. Zlib compression enabled.

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