Sign in to follow this  
Followers 0
commenti

Function InGroup and function tempfile

11 posts in this topic

Hi,

on Juni, 23th (shortly after I had started coding with autoit) I made my first post in the forum and asked how to find out in which group a user is. The answer was there was no build in function like I was used to from, KIX-Script. Nobby gave me a hint and a short coding example how to do this with cmd net. I improved that and here are the first autoit functions I wrote until now, perhaps someone else has use for them, too. Because this are my first functions I would be happy if some of the more experienced coders would give feedback if they find errors or have suggestions how to improve code quallity.

Short example on how to use the function

if InGroup ("Test") Then
   MsgBox (0, "Info", "User is in group")
Else
   MsgBox (0, "Info", "User is not in group")
EndIf

Now here are the functions

Func InGroup($GROUP)
  ;Get the user details and pipe to a text file
   $tempfile = tempfile()
   RunWait(@ComSpec & " /c net user /Domain" & " " & @UserName & " >" & $tempfile, "", @SW_HIDE)
  ; Open textfile output from cmd net user
   $FILE = FileOpen($tempfile, 0)
   
  ; Check if file opened for reading OK
   If $FILE = -1 Then
      MsgBox(0, "Error", "Unable to open file.")
      Exit
   EndIf
   
  ; Read in file until the EOF is reached, if user is in group return 1 (true) or  else 0 (false)
   While 1
      $LINE = FileReadLine($FILE)
      If @error = -1 Then
         FileClose($FILE)
         FileDelete($tempfile)
         ExitLoop
      EndIf
      If StringInStr($LINE, "*" & $GROUP & " ") Then
         FileClose($FILE)
         FileDelete($tempfile)
         Return 1
      EndIf
   Wend
   
EndFunc  ;==>InGroup

; ----------------------------------------------------------------------------
; Function tempfile
; Returns temppath including a random named tempfile + creates  the empty file. 
; If functions fails, it returns 0
; ----------------------------------------------------------------------------

Func tempfile()
   For $TRY = 1 To 100                            ; Try 100 times to create a tempfile, if no success
      $TEMPNAME = "~"                             ; create 7 random chars and ad them to the tempfile name
      For $I = 0 To 6                            
         $RND = Random(97, 122)
         $RND = Round($RND, 0)
         $TEMPNAME = $TEMPNAME & Chr($RND)
      Next
      
      $TEMPNAME = @TempDir & "\" & $TEMPNAME & ".tmp"; add path and ".tmp" to the tempfilename.
      
      $TEMPFH = FileOpen($TEMPNAME, 0) 
      
      If $TEMPFH = -1 Then
         $TEMPFH = FileOpen($TEMPNAME, 2)
         If $TEMPFH = -1 Then                    ; if can not open the file then something is gone wrong.   
         Else
            FileClose($TEMPFH)      
            Return ($TEMPNAME)
         EndIf
      EndIf
      
      If $TEMPFH = 1 Then                         ; If file allready exists, its not the file just created 
         FileClose($TEMPFH)                      ; opened a file from something else. Close it
      EndIf
      
   Next
EndFunc  ;==>tempfile

commenti

Share this post


Link to post
Share on other sites



It looks good to me.

Share this post


Link to post
Share on other sites

hey, very cool, I forsee myself using this at some point in the future. Thanks!


"I'm not even supposed to be here today!" -Dante (Hicks)

Share this post


Link to post
Share on other sites

Hi *.* :(

I just found a bug in my function InGroup. :ph34r:

So, here is the updated function:

Func InGroup($GROUP)
   $groupnamelength = stringlen ($group)
   if $groupnamelength > 21 then 
      $groupnamelength = 21
   EndIf
   $groupnamearray = StringSplit ($GROUP,"")
   
   $group = ""
   for $groupcharactercount = 1 to $groupnamelength
      $group=$group & $groupnamearray[$groupcharactercount]
   Next
  ;Get the user details and pipe to a text file
   $tempfile = tempfile()
   RunWait(@ComSpec & " /c net user /Domain" & " " & @UserName & " >" & $tempfile, "", @SW_HIDE)
  ; Open textfile output from cmd net user
   $FILE = FileOpen($tempfile, 0)
   
  ; Check if file opened for reading OK
   If $FILE = -1 Then
      MsgBox(0, "Error", "Unable to open file.")
      Exit
   EndIf
   
  ; Read in file until the EOF is reached, if user is in group return 1 (true) or  else 0 (false)
   While 1
      $LINE = FileReadLine($FILE) & " "
      If @error = -1 Then
         FileClose($FILE)
         FileDelete($tempfile)
         ExitLoop
      EndIf
      If StringInStr($LINE, "*" & $GROUP & " ") Then
         FileClose($FILE)
         FileDelete($tempfile)
         Return 1
      EndIf
   Wend
   
EndFunc  ;==>InGroup

(Function needs function tempfile(), posted in my first post to work)

Known limitations: If your groupname is larger than 21 characters and you have another groupname that is larger or = 21 characters + the first 21 charakters are fully equal than the function cannot see a difference between these two groupnames.

Example:

First group name is:

my-special-group-with-a-lot-of-cool-members

Second group name is:

my-special-group-with-a-lot-of-lazy-members

For the functions there is no difference between this two groupnames because the first 21 characters are equal: "my-special-group-with"

Fixed bugs:

All group names with more or equal 21 characters did not work. Reason was that "net user" only outputs the first 21 characters of a groupname + I used the last space in the outputline to find out the end of the groupname. A 21 charakters long groupname had no ending space in the output line, so it did not work. Groupnames longer than 21 charakters were truncated by net user and so the functions never found a match. This is now soluted by comparing only 21 charakters. So:

InGroup ("my-group-is-here-an-knowhere-else")

will now give a true if user is in this group but this results in the above mentioned limitation. (I think this is not really a problem if you know about that).

A last warning: Function only tested on german Win XP and Win 2000 systems. If the command "net user" has an output with another grouplength limitation in other language windows version, the function will not work. (I could not test this, cause I only have german versions of windows).

commenti

Share this post


Link to post
Share on other sites

:)

I have tested it on Windows95 and 98.... dont work! the command "net user" is not recognized on that windows versions.

anyone knows another alternative?

i´m doing a brave resistence to come back to KIX

Share this post


Link to post
Share on other sites

Windows 9x uses winpop instead.

<{POST_SNAPBACK}>

I thought that the winpopup command was only an equivelent to the net send command. I am almost positive it will not work as expected here.

*** Matt @ MPCS

Share this post


Link to post
Share on other sites

I have tested it on Windows95 and 98....  dont work! the command "net user" is not recognized on that windows versions.

anyone knows another alternative?

Here is the equivalent function for Windows 9x:

Func InGroup($GROUP)
   Return 1
EndFunc

since Windows 9x does not do groups. :)


My Projects:DebugIt - Debug your AutoIt scripts with DebugIt!

Share this post


Link to post
Share on other sites

This is not true, Win9Xcan be configured to sign into a workgroup server which can then issue group policies. I have no idea how to capture this policy, and I don;t see this being useful on Win9X anyway.

*** Matt @ MPCS

Share this post


Link to post
Share on other sites

You're talking about the server, which if it is running Windows 2000+, of course understands groups. A Windows 9x machine does not.


My Projects:DebugIt - Debug your AutoIt scripts with DebugIt!

Share this post


Link to post
Share on other sites

Hi,

I don´t know if anyone is using my functions for loginscripts with autoit.

However I added another function to determine to which group the pc belongs.

Func PCInGroup($PCGROUP)
   $groupnamelength = stringlen ($pcgroup)
   if $groupnamelength > 21 then 
      $groupnamelength = 21
   EndIf
   $groupnamearray = StringSplit ($PCGROUP,"")
   
   $pcgroup = ""
   for $groupcharactercount = 1 to $groupnamelength
      $pcgroup=$pcgroup & $groupnamearray[$groupcharactercount]
   Next
  ;Get the computer details and pipe to a text file
   $tempfile = tempfile()
   RunWait(@ComSpec & " /c net group /Domain" & " " & $PCGROUP & " >" & $tempfile, "", @SW_HIDE)
  ; Open textfile output from cmd net user
   $FILE = FileOpen($tempfile, 0)
   
  ; Check if file opened for reading OK
   If $FILE = -1 Then
      MsgBox(0, "Error", "Unable to open file.")
      Exit
   EndIf
   
  ; Read in file until the EOF is reached, if user is in group return 1 (true) or  else 0 (false)
   While 1
      $LINE = FileReadLine($FILE) & " "
      If @error = -1 Then
         FileClose($FILE)
         FileDelete($tempfile)
         ExitLoop
      EndIf
      If StringInStr($LINE, @ComputerName & "$") Then
         FileClose($FILE)
         FileDelete($tempfile)
         Return 1
      EndIf
   Wend
   Return 0 
EndFunc  ;==>PCInGroup

Needs my tempfile function also included in the loginscript-functions.au3 collection.

Get the loginscript-functions collection here:

http://www.autoitscript.com/fileman/index.php?act=list&op=get&target=commenti%2Floginscript-functions.au3

commenti

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0