Page 1 of 1 1
Topic Options
#209774 - 2015-01-05 09:48 AM problem extracting stuff from AD (2008R2) and compare to file with readline
Curious Offline
Getting the hang of it

Registered: 2004-02-16
Posts: 61
Hey folks!

Happy New year btw.

I've been using - for some time - a script that makes some default outlook signatures based on predefined criterias. This has been working fine.

My problem is that this is running EVERY time a user logs on their system, meaning (since it's opening word, writing ++) this takes some time each time the script is running. And.. all of us wants logon scripts to be as fast and minimal as possible.

In this regard I'm modifying the script. What happens is that the first time the user logs on; things are collected from AD, to be written to a file. Next time the script checks for this existing file; comparing it to changes in AD. If changes detected; new signature is being generated, if else - nothing, except for some logging. There's also a routine to check on which physical machines the signature has been generated. If it's a machine not listed in the file, the signature is also generated.

This means that most of the time the script is executed within a sec or two.

Everything works pretty much as I intended; apart from one thing:
When I extract the mobilephone (mobile in AD, found under 'Telephones' when looking in AD Users and Comp), comparing this value against the file, I'm ALWAYS getting the value to be different from the one in AD, even though I'm comparing it against the value in the file which was WRITTEN from the value in AD the last time.

Obviously I'm doing something wrong, but just can't see what it is. BTW I have the same problem with the value of the user's homepage (wwwhomepage in properties AD).

I'm definately not the best scripter, so you bright heads would probably do things better and easier, but the script works as intended - except from this things that are really bugging me.

Here's an extraction of the code:

 Code:
$x = WriteLine( 1 , @time + " ----> " + "Starter seksjonen for å lage standardisert signatur i Outlook" + @CRLF)
If NOT Exist ("\\BK-Dc1\netlogon") $AdServer = '\\BK01'
	Else $AdServer = '\\BK-DC1'
EndIf
$NetLogon = $AdServer + '\Netlogon'
$Netlogon = $AdServer +'\Netlogon'
$Filer = $NetLogon + '\Filer'
$Log = $Netlogon + "\Logger"
$klokkeslett = @Time
$klokkeslett = replace($klokkeslett, ":", ".", 1, -1)
If NOT @LOGONMODE
	Break on
EndIf

CLS
At (1,0) "Lager standardisert outlook-signatur..."
Sleep 0.1





;Get data from AD
;Open ADSystemInfo object
$objSysInfo = CreateObject("ADSystemInfo")
;Get the username from AD.
$strUser 	= $objSysInfo.UserName
;Get the users deatils using LDAP.
$objUser	= GetObject("LDAP://" + $strUser)
;Get the users full name.
;$strName 		= $objUser.FullName
;Stokket litt om på hvordan navn skal vises : Fornavn Etternavn
$strName 		= $objUser.Givenname + " " + $objUser.sn
;Get the users title.
$strTitle 		= $objUser.Title
;Get the users default reply e-mail address.
$strMail 		= $objUser.mail
;Get the users department.
$strDepartment = $objUser.Department
;Get the users company name.
$strCompany 	= $objUser.Company
;Get the users phone number.
$strPhone 		= $objUser.telephoneNumber
;Get the users mobile phone number
$strMobile		= $objUser.mobile
;Get the users homepage.
$strWeb		 	= $objUser.wWWHomePage

; In a terminalserver-environment we generate new signature if the control-file doesn't exist
if not exist ("%appdata%\OutlookSignature.txt")
	Run "%comspec% /c print > %appdata%\OutlookSignature.txt"
	$x = MakeNewSignature() ; Call the function to generate signature
EndIf

;Check for file identifying the user, to use this as reference for comparing values
if exist ("$Log\OutlookSignature\@userid.txt")


		if open (8, "$Log\OutlookSignature\@userid.txt") = 0
					;$x = ReadLine(8)
				do 
				;WHILE @ERROR = 0
				$counter = $Counter + 1
				? $Counter
				 		$x = ReadLine(8)
						? "Line read: [" + $x + "]"
						vartypename ($x)

						 if $x = $strName or $x = $strTitle or $x = $strMail or $x = $strDepartment or $x = $strCompany or $x = $strPhone or $x == strMobile or $x = strWeb
						
							$Change = "no"
						else
							$Change = "yes"
							$MakeNewSignature = "yes"
						Endif
						? "$$x : " + $x
						? "$$Change : " + $Change
						?"$$MakeNewSignature :" + $MakeNewSignature
						
				sleep 3
				cls
				Until $Counter = 8
				
					while @error = 0
						$x = ReadLine(8)
						? "Line read: [" + $x + "]"
					if $x = @wksta or left(@wksta,5) = "BK-TS"
						$MachineExist = "yes"
					endif
					LOOP
				
			Close (8)

						

		
			? "$$MakeNewSignature : " $MakeNewSignature
			? "$$MachineExist :" $MachineExist
			sleep 0.5
			
			; Has something changed, or does the machine exist in the file?
			if $MakeNewSignature <> "yes" and $MachineExist = "yes" ; Nothing has changed, just write info to the log-file
				? "Informasjon fra fil er lik informasjon i AD, og " + @wksta + " er det laget signatur for tidligere; ingenting gjøres.."
				sleep 0.5
				if open (5, "$Log\OutlookSignatur.txt",5) = 0
					$Tekst = left (left ( "@YEAR" + "-" + "@MONTHNO" + "-" +"@MDAYNO" + "-" + "     ",11)  + $klokkeslett + " ---> " + left(@wksta + " " + left(%clientname% + "       ",6) + " " + @userid + "        ",20) + " - " + left(@fullname + "          ",29) + "             ",75)	
					if $LagetNySignatur <> "yes"
						writeline (5, $Tekst + "Informasjon er som før, igenting gjøres (" + $StrTitle + ")" + @CRLF)
					endif
				EndIf
				Close (5)
				sleep 0.5
				cls
				return
			else ; File-content is different from AD; generate new signature
			;debug on
				? "Ny signatur må opprettes.."
				sleep 0.5
				if $MachineExist <> "yes" ; Only machine has changed; write the name of the machine in the users control-file
					if open (9, "$Log\OutlookSignature\@userid.txt",5) = 0
						writeline (9,@wksta + @CRLF)
					
						close(9)
					endif
					$x = MakeNewSignature() ; Call the function to generate signature
					return
				endif	
				;Something else than the machine has changed: delete existing file and write info from AD in new file
				del "$Log\OutlookSignature\@userid.txt" ;
				if open (9, "$Log\OutlookSignature\@userid.txt",5) = 0
					writeline (9,$strName + @CRLF)
					writeline (9, $strTitle + @CRLF)
					writeline (9, $strMail + @CRLF)
					writeline (9, $strDepartment + @CRLF)
					writeline (9, $strCompany + @CRLF)
					writeline (9, $strPhone + @CRLF)
					writeline (9, $strMobile + @CRLF)
					writeline (9, $strWeb + @CRLF)
					writeline (9, "----------" + @CRLF)
					writeline (9, "Signatur er opprettet på følgende maskiner:" + @CRLF)
					writeline (9,@wksta + @CRLF)
					close(9)
				endif
				if $LagetNySignatur = "yes" ; Check if signature has already has been made (can happen if terminalserver-check file has been deleted). Routine to prevent signature to happen twice
					return
				EndIf
				$x = MakeNewSignature() ; Call the function to generate signature
				return
			Endif
		Endif	
else ; File doesn't exist. New signature must be generated

		? "Ny signatur må opprettes"
		sleep 0.5

		if exist ("$Log\OutlookSignature\@userid.txt")
			del "$Log\OutlookSignature\@userid.txt"
		endif
		if open (9, "$Log\OutlookSignature\@userid.txt",5) = 0
			writeline (9,$strName + @CRLF)
			writeline (9, $strTitle + @CRLF)
			writeline (9, $strMail + @CRLF)
			writeline (9, $strDepartment + @CRLF)
			writeline (9, $strCompany + @CRLF)
			writeline (9, $strPhone + @CRLF)
			writeline (9, $strMobile + @CRLF)
			writeline (9, $strWeb + @CRLF)
			writeline (9, "----------" + @CRLF)
			writeline (9, "Signatur er opprettet på følgende maskiner:" + @CRLF)
			writeline (9,@wksta + @CRLF)
			close(9)
		endif
		if $LagetNySignatur = "yes"
			return
		EndIf
		$x = MakeNewSignature()
		return
endif


Here's an extraction of one of the file's to compare to:

 Code:
Users name
Users job-title
username@company.com
Department

11111111
99999999
http://www.company.com
----------
Signature is generated on the following machines:
PC100


11111111 - Office number
99999999 - Moible number

Then the function is being defined.
The script is being called from another mainscript, so the reference to a log file will be wrong, possible some other things too.

Hope some of you smart guys (once again) can help me out \:\)
_________________________
I was placed on earth to do a job. Right now I have so much to do, I will never die..

Top
#209775 - 2015-01-05 10:56 AM Re: problem extracting stuff from AD (2008R2) and compare to file with readline [Re: Curious]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
First I would make sure all variables and macros are not enclosed in strings. This may not solve the issues but it is best practice to do so.

For example:
 Code:
...
if exist ($Log\OutlookSignature\@userid.txt")
...


will become:
 Code:
...
if exist ($Log + "\OutlookSignature\" + @userid + ".txt")
...


Second thing I notice is that you do a case sensitive check on the mobile number. Normally this is just numbers and possibly a + and () which are all not affected by lower or upper case.
What happens if you change the double == to a single = on this line? The last two variables are also missing the $ character.

 Code:
...
if $x = $strName or $x = $strTitle or $x = $strMail or $x = $strDepartment or $x = $strCompany or $x = $strPhone or $x == strMobile or $x = strWeb
...


Edited by Mart (2015-01-05 12:15 PM)
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#209776 - 2015-01-05 11:37 AM Re: problem extracting stuff from AD (2008R2) and compare to file with readline [Re: Mart]
Curious Offline
Getting the hang of it

Registered: 2004-02-16
Posts: 61
Thanks for the feedback Mart \:\)

The
 Code:
$x == strMobile


was just a test (which I've forgotten to reverse)
when the $x = strMobile wasn't working.

So.. no change, both giving me the same problem.

//JE
_________________________
I was placed on earth to do a job. Right now I have so much to do, I will never die..

Top
#209777 - 2015-01-05 12:07 PM Re: problem extracting stuff from AD (2008R2) and compare to file with readline [Re: Curious]
Curious Offline
Getting the hang of it

Registered: 2004-02-16
Posts: 61
It's just incredible.

As you pointed out; the two last variables missed the $ character.

I've looked at the code over and over again (it is to be said: I've been working remote on a little laptop, but then again..)I've missed those two BIG details \:\)

As simple as that, thanks again \:\)

But another question:
Does readline hold information in memory, even after the script has terminated? In that case; is there a way to insure that nothing resides in memory from previous runs?

//JE
_________________________
I was placed on earth to do a job. Right now I have so much to do, I will never die..

Top
#209778 - 2015-01-05 12:10 PM Re: problem extracting stuff from AD (2008R2) and compare to file with readline [Re: Curious]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
As far as I know all info in the system's memory is gone as soon as the kixtart session that put the info there is terminated.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#209781 - 2015-01-05 05:15 PM Re: problem extracting stuff from AD (2008R2) and compare to file with readline [Re: Curious]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4403
Loc: New Jersey
Check out the KGEN / Kix Development Suite on my web site.. KGen employs the Sanity() UDF which would find things like "vars in strings" and mismatched objects, as well as undeclared variables that would then become GLOBALs. This is a free tool available for download with simple registration.

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

Top
Page 1 of 1 1


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

Who's Online
1 registered (Henriques) and 2516 anonymous users online.
Newest Members
fgarcia, Viginette, ManuvdWielNL, Sir_Barrington, batdk82
17889 Registered Users

Generated in 0.153 seconds in which 0.088 seconds were spent on a total of 13 queries. Zlib compression enabled.

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