sb1920alk
(Fresh Scripter)
2010-07-08 01:11 AM
Basic Questions

Hello,
I am new to Kixtart. I've used vbscript for years and also some autoit, but now I need to learn Kixtart.

I read the manual and have gotten the hang of some basic variable and string manipulation, but I'm having some difficulty using some AD scripting, including the example for FUNCTION in the manual.
My script is test2.kix:
 Code:
messagebox(ReadNC(%computername%),"")
Function ReadNC( ServerName )
  $ReadNC = ""
  $Root = GetObject( "LDAP://" + ServerName + "/rootDSE" )
  If @ERROR = 0
     $ReadNC = $Root.defaultNamingContext
  Endif
EndFunction

When I run kix32.exe test2.kix, I get
ERROR : error in parameterdefinition of [readnc]!
Script: C:\...path to test file...test2.kix

Here is a script I'm trying to convert to KIX from vbscript.
 Code:
Set $objItem = GetObject("LDAP://***Distinguised User Name***")
$msExchHomeServerName = $objItem.msExchHomeServerName
$EmailServer = Right($msExchHomeServerName,Len($msExchHomeServerName) - InstrRev($msExchHomeServerName,"="))
MessageBox($EmailServer,"")


When I run it, the error message is:
ERROR : IDispatch pointers not allowed in expressions!
Script: C:\...path to test script
Line : 2

...not sure what's going on.


Mart
(KiX Supporter)
2010-07-08 09:23 AM
Re: Basic Questions

 Originally Posted By: sb1920alk

....
My script is test2.kix:
 Code:
messagebox(ReadNC(%computername%),"")
Function ReadNC( ServerName )
  $ReadNC = ""
  $Root = GetObject( "LDAP://" + ServerName + "/rootDSE" )
  If @ERROR = 0
     $ReadNC = $Root.defaultNamingContext
  Endif
EndFunction

When I run kix32.exe test2.kix, I get
ERROR : error in parameterdefinition of [readnc]!
Script: C:\...path to test file...test2.kix
....


You have just one parameter in your function and when you call it you sue two parameters. That’s why you get that error message. Instead of %computername% you can also use @WKSTA. @WKSTA is a macro build into kix and gets filled with the name of the current computer.

 Originally Posted By: sb1920alk

....
Here is a script I'm trying to convert to KIX from vbscript.
 Code:
Set $objItem = GetObject("LDAP://***Distinguised User Name***")
$msExchHomeServerName = $objItem.msExchHomeServerName
$EmailServer = Right($msExchHomeServerName,Len($msExchHomeServerName) - InstrRev($msExchHomeServerName,"="))
MessageBox($EmailServer,"")


When I run it, the error message is:
ERROR : IDispatch pointers not allowed in expressions!
Script: C:\...path to test script
Line : 2

...not sure what's going on.


When you do a VarTypeName of $EmailServer I guess you will get some kind of array. What happens if you use $EmailServer[0]?


AllenAdministrator
(KiX Supporter)
2010-07-08 04:12 PM
Re: Basic Questions

- Missing $ in front of Servername, 2 places
- $ReadNC="" is doing nothing. The function name (ReadNC) is automatically dimmed, and each time the function is called redimmed.
- Dim all other vars not in your function line.

 Code:
Function ReadNC($ServerName)
  Dim $Root
  $Root = GetObject( "LDAP://" + $ServerName + "/rootDSE" )
  If @ERROR = 0
     $ReadNC = $Root.defaultNamingContext
  Endif
EndFunction


sb1920alk
(Fresh Scripter)
2010-07-08 08:49 PM
Re: Basic Questions

 Originally Posted By: Mart
You have just one parameter in your function and when you call it you sue two parameters. That’s why you get that error message. Instead of %computername% you can also use @WKSTA. @WKSTA is a macro build into kix and gets filled with the name of the current computer.

Sorry, but how am I using two parameters? The line is messagebox(ReadNC(%computername%),""). %computername% should be the only paramenter for ReadNC. The "" is the second parameter for messagebox.

 Originally Posted By: Mart
When you do a VarTypeName of $EmailServer I guess you will get some kind of array. What happens if you use $EmailServer[0]?

Same error. Regardless of what the variable type is, the error is occuring on line 2.


sb1920alk
(Fresh Scripter)
2010-07-08 08:51 PM
Re: Basic Questions

 Originally Posted By: Allen
- Missing $ in front of Servername, 2 places
- $ReadNC="" is doing nothing. The function name (ReadNC) is automatically dimmed, and each time the function is called redimmed.
- Dim all other vars not in your function line.

 Code:
Function ReadNC($ServerName)
  Dim $Root
  $Root = GetObject( "LDAP://" + $ServerName + "/rootDSE" )
  If @ERROR = 0
     $ReadNC = $Root.defaultNamingContext
  Endif
EndFunction

Ok. I'm not getting an error now, but the message box is blank. Also, I took the ReadNC function straight out of the manual in the FUNCTION section. Are the examples in the manual not reliable to use?


AllenAdministrator
(KiX Supporter)
2010-07-08 09:45 PM
Re: Basic Questions

No idea what manual you are talking about... do you have a link?

Try this... if it doesn't work, do a board search for rootdse or defaultnamingcontext, as I know it on here somewhere.

 Code:
Function ReadNC()
  Dim $Root
  $Root = GetObject("LDAP://rootDSE" )
  If @ERROR = 0
     $ReadNC = $root.get("DefaultNamingContext")
  Endif
EndFunction



[edit: I found the link: http://www.kixtart.org/manual/Commands/Function.htm and yes that is a terrible example. I'm not sure who is responsible for it, but it is wrong. I would hope the other examples are better.]


AllenAdministrator
(KiX Supporter)
2010-07-08 09:53 PM
Re: Basic Questions

I haven't tried it, but Witto posted this to the UDFs...

GetDefaultNamingContext() -
http://www.kixtart.org/forums/ubbthreads...true#Post195808


sb1920alk
(Fresh Scripter)
2010-07-08 10:03 PM
Re: Basic Questions

 Originally Posted By: Allen
No idea what manual you are talking about... do you have a link?

Try this... if it doesn't work, do a board search for rootdse or defaultnamingcontext, as I know it on here somewhere.

 Code:
Function ReadNC()
  Dim $Root
  $Root = GetObject("LDAP://rootDSE" )
  If @ERROR = 0
     $ReadNC = $root.get("DefaultNamingContext")
  Endif
EndFunction



[edit: I found the link: http://www.kixtart.org/manual/Commands/Function.htm and yes that is a terrible example. I'm not sure who is responsible for it, but it is wrong. I would hope the other examples are better.]

That worked. Thanks. I'm glad you found the link, and that it's not "just me".


sb1920alk
(Fresh Scripter)
2010-07-15 01:23 AM
Re: Basic Questions

...Another basic question. Again, I'm used to scripting in vbscript... If I want to overwrite a file in vbscript, I would do something like:
 Code:
On Error Resume Next
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\MyFile.txt",2,True)
objFile.Write "The contents of MyFile.txt"
objFile.Close

I see the OPEN and WRITELINE commands, but I don't see anything that will let me overwrite a file. Same question for appending a file (not just a single line). In vbscript, appending would be the same as the overwrite example, except the 2 would be an 8.


sb1920alk
(Fresh Scripter)
2010-07-15 02:07 AM
Re: Basic Questions

BTW, the example in the manual for WRITELINE is missing a )
 Code:
IF Open( 3 , "C:\TEMP\LOG.TXT" , 5 )  = 0
   $x = WriteLine( 3 , "KiXtart started at " +  @TIME + @CRLF
ELSE
   BEEP
   ? "failed to open file, error code : [" + @ERROR + "]"
ENDIF

Is there a Wiki version of the manual that I can edit to fix these types of things?


AllenAdministrator
(KiX Supporter)
2010-07-15 02:19 AM
Re: Basic Questions

LOL. Dude... You must be the first person to actually read the manual in years. ;\) Congrats are in order. Hard to believe no one has noticed that stuff all these years.

Might I suggest using the UDF section as a guide... not all, but most meet a defined standard, and in general are very well tested.

http://www.kixtart.org/forums/ubbthreads.php?ubb=postlist&Board=7&page=1

There is also the manual that is my link... and there is also a book on Kixtart. Honestly though, your best reference is right here.

Hang in there, kix is worth the effort, especially when you take advantage of all the premade UDFs.


NTDOCAdministrator
(KiX Master)
2010-07-15 02:48 AM
Re: Basic Questions

No the OPEN and WRITEFILE do not support overwriting the file. You would need to check if the file exists and remove it and create a new one.

Below is an example of checking for a folder and creating it if it does not exist and then writing a new file if it does not exist. If the file does exist it will append to it.

 Code:
Break On
Dim $SO,$Pause
$SO=SetOption('Explicit','On')
$SO=SetOption('NoVarsInStrings','On')
$SO=SetOption('WrapAtEOL','On')

Dim $Folder, $BackupLog
$Folder='C:\UTILS\'
;Check if the folder exists, if not create it otherwise continue on.
If GetFileAttr($Folder) & 16
  ;Folder found do nothing
Else
  MD $Folder
  If @ERROR
    Quit @ERROR
  EndIf
EndIf

$BackupLog = CreateBackupFileLog('C:\UTILS\PSTBACKUPS.TXT','hello')
If @ERROR 
  'Error creating file: ' + @ERROR + ' - ' + @SERROR ?
EndIf

Function CreateBackupFileLog($PathAndFile,$Data)
  Dim $Handle,$OpenFile,$WL,$CloseFile
  If Not Exist($PathAndFile)
    $Handle = FreeFileHandle()
    If $Handle > 0
      $OpenFile = Open($Handle,$PathAndFile,5)
      $WL = WriteLine($Handle, $Data)
      $CloseFile = Close($Handle)
    EndIf
  EndIf
EndFunction


NTDOCAdministrator
(KiX Master)
2010-07-15 02:51 AM
Re: Basic Questions

We also have this forum to view which might provide some help for those new to KiXtart.

http://www.kixtart.org/forums/ubbthreads.php?ubb=cfrm&c=3


Richard H.Administrator
(KiX Supporter)
2010-07-15 09:55 AM
Re: Basic Questions

A few comments:
  • If you are comfortable with FSO then just keep using it, KiXtart supports most of the methods / properties as long as you don't need to support pass by reference, events or data types which are not KiXtart native.
  • OPEN with write (mode=4) will open the file in append mode.
  • There is no OPEN with truncate option. The closest you can get is to check if the file exists and delete it before opening, but of course that will lose any attributes associated with the original file.
  • KiXtart file semantics are designed for simple small text files. The entire file is read into memory when it is opened and written out when it is closed.


sb1920alk
(Fresh Scripter)
2010-07-15 01:19 PM
Re: Basic Questions

@Allen. I suppose it was me hoping it would be the equivalent of script56.chm for vbscript, which helped me out a great deal. The UDFs are good if it's what you're looking for.

@NTDOC, I've read a few of them. I'm not sure I have the time to participate in recreational scripting yet. Maybe some time.

@Richard. The same thing crossed my mind. Here's the same piece of code I posted above, only in kix.
 Code:
$objFSO = CreateObject("Scripting.FileSystemObject")
$objFile = $objFSO.OpenTextFile("C:\MyFile.txt",2,True)
$objFile.Write("The contents of MyFile.txt")
$objFile.Close

Yes, I'm very comfortable with FileSystemObject for reading, overwriting, appending files, getting information about files (size, date motified/created, existence) etc. I suppose if I can master the syntax differences between vbscript and kix, I'll be good to go. Little things like putting a $ at the beginning of variables and using + instead of & are no big deal. Where I'm getting thrown off is things like the FileSystemObject...there's no "set" anywhere!


sb1920alk
(Fresh Scripter)
2010-07-19 07:38 PM
Re: Basic Questions

How do I exit a loop? For example, sometimes I use an infinite loop to do something and put a check in it when I want to break out. Specifically, I don't want to put the check in the loop definition itself.

In vbscript, I would do something like this:
 Code:
Do While True
	LoopCount = LoopCount + 1
	MsgBox(LoopCount)
	If LoopCount = 5 Then
		Exit Do
	End If
Loop
MsgBox("Final Loop Count: " & LoopCount)


The closest I've gotten in kix is:
 Code:
Do
	$LoopCount = $LoopCount + 1
	MessageBox($LoopCount,"")
	If $LoopCount = 5
		Exit Do
	EndIf
Until 0
MessageBox("Final Loop Count: " + $LoopCount,"")

...but the Exit is exiting the script, not the loop.
Side question, how do I avoid the console window entirely? I still get it when I drag and drop the test kix script onto wkix32.exe. And why does a "1" appear in the console on each loop?

Oh, and how do I set my forum account to email me whenever there's a reply to this thread?


AllenAdministrator
(KiX Supporter)
2010-07-19 07:58 PM
Re: Basic Questions

There is no equivalent of ExitFor or ExitDo. It's been something that has been requested over and over. In all honesty, the coder should code better to handle that kind of thing. One of the ways I've gotten around it is to add a $found variable. And as part of the loop check to see if $found is set or not.

(these are untested)
 Code:
$i="abcdefghigklmnopqrstuvwxyz"
while $i and $found=0
  $j=left($i,1)
  $i=right($i,-1)
  if $j = "n"
    $found=1
  endif
loop


Using your example:
 Code:
Do
	$LoopCount = $LoopCount + 1
	MessageBox($LoopCount,"")
	;If $LoopCount = 5
	;	Exit Do
	;EndIf
Until $LoopCount=5
MessageBox("Final Loop Count: " + $LoopCount,"")


0's and 1s...
http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=81941#Post81941

All the FAQs...
http://www.kixtart.org/forums/ubbthreads.php?ubb=postlist&Board=5&page=1

The console is appearing because you have output. Cleaning up the 1's and 0's is a step in the right direction.


sb1920alk
(Fresh Scripter)
2010-07-19 08:31 PM
Re: Basic Questions

Ok. That helps.

 Code:
Do
	$LoopCount = $LoopCount + 1
	$NoOutput = MessageBox($LoopCount,"")
	If $LoopCount = 5
		$ExitLoop = True
	EndIf
Until $ExitLoop
$NoOutput = MessageBox("Final Loop Count: " + $LoopCount,"")

The only problem I can see is the loop doesn't exit until the end of the current iteration. There may be more code after the EndIf that I wouldn't want executed. Oh well, this is better than nothing.

To avoid the console entirely, what are my options? Send all output to dummy variables and use wkix32.exe? Use SETCONSOLE("HIDE") at the top of the script? Is there a "best practice"?


AllenAdministrator
(KiX Supporter)
2010-07-19 11:30 PM
Re: Basic Questions

Be careful of that "true"... it's not doing exactly what you think it is. It's only working because Kix is so forgiving.

Richard H.Administrator
(KiX Supporter)
2010-07-20 12:39 PM
Re: Basic Questions

You can use GOTO to force an exit from a loop but you should in almost every case be able to code without it.

Using GOTO is a really bad idea so avoid it as apart from producing unreadable unreliable code you will be constantly told how bad an idea it is and how you shouldn't be using it.

One caveat - don't ever use GOTO to jump into a loop.

 Code:
; Permanent loop
While "True"
	$i=$i+1
	"Counter is now "+$i+@CRLF
	; Bail at counter=5
	If $i=5
		"Bailing..."+@CRLF
		GoTo BailOut
	EndIf
Loop

:BailOut
"Loop done."+@CRLF


Glenn BarnasAdministrator
(KiX Supporter)
2010-07-20 03:14 PM
Re: Basic Questions

Here's an alternative to GOTO to exit a loop:
 Code:
; permanent loop with conditional exit
$Tag = 1
While $Tag     ; Loop forever while Tag is true
  ; do stuff here...
  If <EXIT CONDITION TEST>  ; if some condition exists where we should exit
    $Tag = 0
  EndIf
Loop
Tag represents a TRUE condition upon entry to the loop, but can be set to FALSE within the loop by any number of tests and conditons. Another common condition - perform a loop until any of several conditions exist.. simply add multiple exit condition tests. Any that sets Tag to false will cause an exit from the loop.

If you need to test for exit conditions at the top of the loop, simply move the tests to the top, and enclose your code in If $Tag / EndIf. The exit status will be tested upon entry, and the code executed only if Tag is true.

Glenn


sb1920alk
(Fresh Scripter)
2010-07-22 05:01 PM
Re: Basic Questions

 Originally Posted By: Allen
Be careful of that "true"... it's not doing exactly what you think it is. It's only working because Kix is so forgiving.

Can you be more specific?

@Richard...ok I see what you mean.

@Glenn...that's pretty much what my first thought was. The problem is it doesn't exit the loop immidiately. For example:
 Code:
$Tag = 1
While $Tag     ; Loop forever while Tag is true
  ; do stuff here that should happen on every loop...
  If <EXIT CONDITION TEST>  ; if some condition exists where we should exit
    $Tag = 0
  EndIf
  ; do MORE stuff here that depends on the stuff earlier in the loop, but needs to not happen after the tag to exit the loop has been triggered.
Loop

It's not going to happen often, but it just seems unnecessary to force a certain style of coding in a free-format scripting language.


AllenAdministrator
(KiX Supporter)
2010-07-22 05:21 PM
Re: Basic Questions

Kix does not have a built in true and false... This script may help explain it... give it a whirl to see the results... or change the words in your code to see it always resolve to true.

 Code:
$exitloop=true
? $exitloop
$exitloop=false
? $exitloop
$exitloop=foobar
? $exitloop


Basically anything you put there will make exitloop evaluate to "true".

The good news... it's easy to work around... just add these to your script and the true and false evaluate properly.

 Code:
function true()
  $true=not 0
endfunction

function false()
  $false=not 1
endfunction




Glenn BarnasAdministrator
(KiX Supporter)
2010-07-22 05:48 PM
Re: Basic Questions

Pretty simple, actually..
 Code:
$Tag = 1
While $Tag
  ; always do this stuff...
  If <EXIT CONDITION TEST> ; put your test here!
    $Tag = 0
  Else
    ; do this stuff unless we've been terminated
  EndIf
Loop
Glenn