Page 1 of 2 12>
Topic Options
#198957 - 2010-07-08 01:11 AM Basic Questions
sb1920alk Offline
Fresh Scripter

Registered: 2010-07-08
Posts: 15
Loc: USA
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.

Top
#198958 - 2010-07-08 09:23 AM Re: Basic Questions [Re: sb1920alk]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4672
Loc: The Netherlands
 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]?
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#198960 - 2010-07-08 04:12 PM Re: Basic Questions [Re: Mart]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4545
Loc: USA
- 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

Top
#198965 - 2010-07-08 08:49 PM Re: Basic Questions [Re: Mart]
sb1920alk Offline
Fresh Scripter

Registered: 2010-07-08
Posts: 15
Loc: USA
 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.

Top
#198966 - 2010-07-08 08:51 PM Re: Basic Questions [Re: Allen]
sb1920alk Offline
Fresh Scripter

Registered: 2010-07-08
Posts: 15
Loc: USA
 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?

Top
#198967 - 2010-07-08 09:45 PM Re: Basic Questions [Re: sb1920alk]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4545
Loc: USA
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.]


Edited by Allen (2010-07-08 09:48 PM)

Top
#198968 - 2010-07-08 09:53 PM Re: Basic Questions [Re: Allen]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4545
Loc: USA
I haven't tried it, but Witto posted this to the UDFs...

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

Top
#198969 - 2010-07-08 10:03 PM Re: Basic Questions [Re: Allen]
sb1920alk Offline
Fresh Scripter

Registered: 2010-07-08
Posts: 15
Loc: USA
 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".

Top
#199004 - 2010-07-15 01:23 AM Re: Basic Questions [Re: sb1920alk]
sb1920alk Offline
Fresh Scripter

Registered: 2010-07-08
Posts: 15
Loc: USA
...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.

Top
#199005 - 2010-07-15 02:07 AM Re: Basic Questions [Re: sb1920alk]
sb1920alk Offline
Fresh Scripter

Registered: 2010-07-08
Posts: 15
Loc: USA
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?

Top
#199006 - 2010-07-15 02:19 AM Re: Basic Questions [Re: sb1920alk]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4545
Loc: USA
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.

Top
#199007 - 2010-07-15 02:48 AM Re: Basic Questions [Re: sb1920alk]
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
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

Top
#199008 - 2010-07-15 02:51 AM Re: Basic Questions [Re: NTDOC]
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
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

Top
#199009 - 2010-07-15 09:55 AM Re: Basic Questions [Re: NTDOC]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
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.

Top
#199010 - 2010-07-15 01:19 PM Re: Basic Questions [Re: NTDOC]
sb1920alk Offline
Fresh Scripter

Registered: 2010-07-08
Posts: 15
Loc: USA
@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!

Top
#199041 - 2010-07-19 07:38 PM Re: Basic Questions [Re: sb1920alk]
sb1920alk Offline
Fresh Scripter

Registered: 2010-07-08
Posts: 15
Loc: USA
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?

Top
#199042 - 2010-07-19 07:58 PM Re: Basic Questions [Re: sb1920alk]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4545
Loc: USA
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.

Top
#199044 - 2010-07-19 08:31 PM Re: Basic Questions [Re: Allen]
sb1920alk Offline
Fresh Scripter

Registered: 2010-07-08
Posts: 15
Loc: USA
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"?

Top
#199047 - 2010-07-19 11:30 PM Re: Basic Questions [Re: sb1920alk]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4545
Loc: USA
Be careful of that "true"... it's not doing exactly what you think it is. It's only working because Kix is so forgiving.
Top
#199050 - 2010-07-20 12:39 PM Re: Basic Questions [Re: Allen]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
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

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 557 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.073 seconds in which 0.024 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