#185495 - 2008-02-20 02:27 AM
My logon script - looking for suggestions
|
lukeod
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:
;*************************************************************************
; 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
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
|
|
|
|
#185498 - 2008-02-20 04:26 AM
Re: My logon script - looking for suggestions
[Re: Gargoyle]
|
lukeod
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
|
|
|
|
#185504 - 2008-02-20 12:48 PM
Re: My logon script - looking for suggestions
[Re: Gargoyle]
|
Glenn Barnas
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:
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.
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!
|
Top
|
|
|
|
#185523 - 2008-02-21 01:41 AM
Re: My logon script - looking for suggestions
[Re: Glenn Barnas]
|
lukeod
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
|
|
|
|
#185529 - 2008-02-21 03:02 AM
Re: My logon script - looking for suggestions
[Re: Shawn]
|
lukeod
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
|
|
|
|
#185538 - 2008-02-21 07:51 AM
Re: My logon script - looking for suggestions
[Re: Gargoyle]
|
lukeod
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
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 194 anonymous users online.
|
|
|