Showing posts with label AutoIt. Show all posts
Showing posts with label AutoIt. Show all posts

Tuesday, April 01, 2014

taking mifare card data and importing the UID into equitrac express

So this is not really going to appeal to many people as its kind of specific, but at my work we use MiFare cards to log onto our printers and print out work. The printers are Ricoh machines and we use their software (equitrac) to run the system and database.

We needed a way to automatically assign the UID number from the card to the user that it is assigned to in the equitrac database. I wrote the code in AutoIt because its awesome.

Here it is

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <MsgBoxConstants.au3>

; create GUI window
$Form1 = GUICreate("Test Gui", 269, 150, 193, 115)
;create two labels for the input boxes
GUICtrlCreateLabel("Card UID", 20, 16, 50, 21)
GUICtrlCreateLabel("Username", 20, 50, 50, 21)
; create two input controls
$Input1 = GUICtrlCreateInput("", 72, 16, 121, 21)
$Input2 = GUICtrlCreateInput("", 72, 50, 121, 21)
; create a button which says "press me"
$Button1 = GUICtrlCreateButton("Press Me", 96,80, 75, 25, 0)
; set the GUI to to show, so you can see it
GUISetState(@SW_SHOW)

While 1 ; loop until expression is false
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE ; if the user clicks on X (closes the window)
            Exit ;  the script will exit.
         Case $button1 ; the user clicks on the button
            $sString = GUICtrlRead($Input1);UID added to variable
            $username = GUICtrlRead($Input2);username added to variable
            $sString1 = StringStripWS($sString,8) ;remove spaces from UID
            $sReversed = StringRegExpReplace($sString1, "(.{2})(.{2})(.{2})(.{2})", "$4$3$2$1") ;rearrange the hex value
                   
                     Local $iDec = Dec($sReversed, 2) ;convert hex to decimal
                     Run(@comspec & ' /c "c:\program Files\equitrac\Express\Tools\eqcmd.exe" -sSERVERNAME modify ur '& $username &' ! ! ! ! '& $iDec) ;run the command prompt to add to equitrac
                   
                   
    EndSwitch
WEnd; end of loop


So after the include section you have the part that builds the GUI. Then comes the code. What it does is take the UID, move it around, convert into hex and then add it into the database. The only bit you'll need to change is the line after the 'run' command where i've put SERVERNAME. You put your database server name in there (there is NO space between the -s and the name) and that's that.

Hope this help someone!

Thursday, May 24, 2012

Autoit - Output selection of drop down box to text file

This one took me a little while as I had trouble outputting the selection to the text file, but its working now so here's the code

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GuiComboBoxEx.au3>


$Form1 = GUICreate("Form1", 197, 66, 192, 124)
$Label1 = GUICtrlCreateLabel("Choose number:", 10, 12, 93, 17)
$Combo1 = GUICtrlCreateCombo("Choose here...", 106, 10, 81, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
GUICtrlSetData(-1, "225|226|227|228|229|230")
$Button1 = GUICtrlCreateButton("Start", 50, 32, 97, 25)
Global $file = FileOpen('c:\ChangeMe.txt', 0)
GUISetState(@SW_SHOW)


While 1
    $chosen = GUICtrlRead($Combo1)
    $nMsg = GUIGetMsg()
    Switch $nMsg
               Case $GUI_EVENT_CLOSE
                     Exit
                Case $Button1
                  $chosen = GUICtrlRead($Combo1)
                  Switch $chosen
                Case "225"
                        $LogStore = FileRead($file)
                        FileClose($file)
                        $file = FileOpen('c:\ChangeMe.txt', 2)
                        FileClose($file)
                        $file = FileOpen('c:\ChangeMe.txt', 1)
                        FileWrite($file, "225 - "& @CRLF)
                        FileWrite($file, $LogStore & @CRLF)
                Case "226"
                        $LogStore = FileRead($file)
                        FileClose($file)
                        $file = FileOpen('c:\ChangeMe.txt', 2)
                        FileClose($file)
                        $file = FileOpen('c:\ChangeMe.txt', 1)
                        FileWrite($file, "226 - "& @CRLF)
                        FileWrite($file, $LogStore & @CRLF)
                Case "227"
                        $LogStore = FileRead($file)
                        FileClose($file)
                        $file = FileOpen('c:\ChangeMe.txt', 2)
                        FileClose($file)
                        $file = FileOpen('c:\ChangeMe.txt', 1)
                        FileWrite($file, "227 - "& @CRLF)
                        FileWrite($file, $LogStore & @CRLF)
                Case "228"
                        $LogStore = FileRead($file)
                        FileClose($file)
                        $file = FileOpen('c:\ChangeMe.txt', 2)
                        FileClose($file)
                        $file = FileOpen('c:\ChangeMe.txt', 1)
                        FileWrite($file, "228 - "& @CRLF)
                        FileWrite($file, $LogStore & @CRLF)
                Case "229"
                        $LogStore = FileRead($file)
                        FileClose($file)
                        $file = FileOpen('c:\ChangeMe.txt', 2)
                        FileClose($file)
                        $file = FileOpen('c:\ChangeMe.txt', 1)
                        FileWrite($file, "229 "& @CRLF)
                        FileWrite($file, $LogStore & @CRLF)
               Case "230"
                        $LogStore = FileRead($file)
                        FileClose($file)
                        $file = FileOpen('c:\ChangeMe.txt', 2)
                        FileClose($file)
                        $file = FileOpen('c:\ChangeMe.txt', 1)
                        FileWrite($file, "230 - "& @CRLF)
                        FileWrite($file, $LogStore & @CRLF)
    EndSwitch
Endswitch   
WEnd

Thursday, April 19, 2012

Autoit - Moving Computers in Active Directory

We've just had a new building built, with lots of nice new rooms for members of staff to live in. Naturally current staff have already staked their claims to rooms, and as such its up to IT Services to move the computer equipment over to the new offices.

Obviously we've done this a few times before and we usually forget to change where the computers are in Active Directory. If you've got a structure based on room layout, then when a computer moves rooms or even to a different building then it needs moving in Active Directory as well. Like I said, we usually forget to move them, as we're normally very busy.

So I've been going round the net trying to find some free software that will pop up when one of IT log on to ask if we want to move the computer. Personally I didn't like any of them, so I decided to write my own in AutoIt.

This little program is based on the examples given in AD.au3 pack that I've talked about before. The program will start when a member of IT services logs on, and will initially ask if the computer they have logged on to has been moved. 'No' will exit the program, but 'yes' will taken them to a GUI with their Active Directory tree structure. If you then browse to the OU that you want the computer to be moved to and click 'Move Computer' it will move it for you. If you click Exit instead, the program will end. It will pop up with 2 confirmation boxes, the first checking that it is correct and the second will tell you either if its successful, or if it failed. Here's the code


#AutoIt3Wrapper_AU3Check_Parameters= -d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#AutoIt3Wrapper_AU3Check_Stop_OnWarning=Y

#include <AD.au3>
#include <TreeviewConstants.au3>
#include <WindowsConstants.au3>
#include <GUIConstants.au3>
#include <GuiTreeView.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>

_AD_Open()
Global $Reply = MsgBox(4,"Moved computer", "Have you moved this computer?")
If $Reply <> 6 Then Exit
  
Global $newOU = ""
Global $sTitle = "Move that stupid Computer v1.3"
Global $hMain = GUICreate($sTitle, 743, 683, -1, -1)
Global $IObject = GUICtrlCreateInput(_AD_SamAccountNameToFQDN(@ComputerName& "$"), 8, 30, 559, 21)

Global $hTree = GUICtrlCreateTreeView(6, 100, 600, 400, -1, $WS_EX_CLIENTEDGE)
Global $bExit = GUICtrlCreateButton("Exit", 624, 8, 97, 33)
Global $BOK = GUICtrlCreateButton("Move Computer", 624, 200, 97, 33)
Global $aTreeView = _AD_GetOUTreeView($newOU, $hTree)

GUICtrlCreateLabel("Object to move (FQDN or sAMAccountName):", 8, 10, 231, 17)
GUISetState(@SW_SHOW)

While 1
    Global $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE, $BExit
            Exit
        Case $BOK
            Global $sObject = GUICtrlRead($IObject)
           
            $hSelection = _GUICtrlTreeView_GetSelection($hTree)
            $sSelection = _GUICtrlTreeView_GetText($hTree, $hSelection)
            For $i = 1 To $aTreeView[0][0]
                If $hSelection = $aTreeView[$i][2] Then ExitLoop
            Next
            $sOU = $aTreeView[$i][1]
            msgbox(1,"","Currently - "&$sObject&" target - "&$sOU)
            ExitLoop
    EndSwitch
WEnd

; Move object
Global $iValue = _AD_MoveObject($sOU, $sObject)
If $iValue = 1 Then
    MsgBox(64, "Active Directory Functions - Example 1", "Object '" & $sObject & "' successfully moved to '" & $sOU & "'")
ElseIf @error = 1 Then
    MsgBox(64, "Active Directory Functions - Example 1", "Target OU '" & $sOU & "' does not exist")
ElseIf @error = 2 Then
    MsgBox(64, "Active Directory Functions - Example 1", "Object '" & $sObject & "' does not exist")
Else
    MsgBox(64, "Active Directory Functions - Example 1", "Return code '" & @error & "' from Active Directory")
EndIf
_AD_Close()


Func _AD_GetOUTreeView($sAD_OU, $hAD_TreeView, $bAD_IsADOpen = True)

    If $bAD_IsADOpen = False Then
        _AD_Open()
        If @error Then Return SetError(@error, @extended, 0)
    EndIf
    $sSeparator = "\"
    Local $aAD_OUs = _AD_GetAllOUs($sAD_OU, $sSeparator)
    If @error <> 0 Then Return SetError(@error, @extended, 0)
    Local $aAD_TreeView[$aAD_OUs[0][0] + 1][3] = [[$aAD_OUs[0][0], 3]]
    For $i = 1 To $aAD_OUs[0][0]
        $aAD_Temp = StringSplit($aAD_OUs[$i][0], $sSeparator)
        $aAD_TreeView[$i][0] = StringFormat("%" & $aAD_Temp[0] - 1 & "s", "") & "#" & $aAD_Temp[$aAD_Temp[0]]
        $aAD_TreeView[$i][1] = $aAD_OUs[$i][1]
    Next
    If $bAD_IsADOpen = False Then _AD_Close()

    _GUICtrlTreeView_BeginUpdate($hAD_TreeView)
    Local $ahAD_Node[50]
    For $iAD_Index = 1 To $aAD_TreeView[0][0]
        $sAD_Line = StringSplit(StringStripCR($aAD_TreeView[$iAD_Index][0]), @TAB)
        $iAD_Level = StringInStr($sAD_Line[1], "#")
        If $iAD_Level = 0 Then ExitLoop
        If $iAD_Level = 1 Then
            $ahAD_Node[$iAD_Level] = _GUICtrlTreeView_Add($hAD_TreeView, 0, StringMid($sAD_Line[1], $iAD_Level + 1))
            $aAD_TreeView[$iAD_Index][2] = $ahAD_Node[$iAD_Level]
        Else
            $ahAD_Node[$iAD_Level] = _GUICtrlTreeView_AddChild($hAD_TreeView, $ahAD_Node[$iAD_Level - 1], StringMid($sAD_Line[1], $iAD_Level + 1))
            $aAD_TreeView[$iAD_Index][2] = $ahAD_Node[$iAD_Level]
        EndIf
    Next
    _GUICtrlTreeView_EndUpdate($hAD_TreeView)
    Return $aAD_TreeView

EndFunc

It should work for you, I see no reason why not, but please remember that this will change your Active Directory. Please make sure you test it first. I'm not being held responsible for messing up your AD.

Monday, April 02, 2012

AutoIt - Add Users to Active Directory / Creating New Users

So I'm progressing with Autoit quite nicely and I'm trying to get it to add new users to Active Directory. There isn't much info about this on google. I'm guessing that most people use professional software for this task. Well we're cheap here so I've had to come up with my own way of doing it.

So if you want to use the code below, the first thing you'll have to do is download the AD.au3 files and put them all in your 'include' directory.

Now this script will alter your active directory. Please test it first before you do anything. I'm not being held responsible for you messing up your AD.

As you can see in the code below, it takes a list of names from the 'c:\test.txt' file (each name should be on its own line). It then takes this list, creates each one a password and creates a userarea a network drive (I've used Z:\). Then outputs each user a text file containing a welcome message that you can edit, their username and their password. Finally it will add the user to Active Directory.

So here's the code

#include <file.au3>
#include <AD.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

  _AD_Open()
   If @error Then Exit MsgBox(16, "Active Directory Script", "Function _AD_Open encountered a problem. @error = " & @error & ", @extended = " & @extended)
   Global $Reply = MsgBox(308, "Active Directory Functions", "This script creates a new user in the specified OU."  "Are you sure you want to change the Active Directory?")
If $Reply <> 6 Then Exit
  
$fileopen = "C:\test.txt"
$file = FileOpen($fileopen,0)

for $x = 1 to _FileCountlines($fileopen)
   Local $line = FileReadLine($file)
   _FileCreate("C:\"&$line&".txt")
   $pwd = ""
   Dim $aSpace[3]
   $digits = 8
   For $i = 1 To $digits
   $aSpace[0] = Chr(Random(65, 90, 1)) ;A-Z
   $aSpace[1] = Chr(Random(97, 122, 1)) ;a-z
   $aSpace[2] = Chr(Random(48, 57, 1)) ;0-9
   $pwd &= $aSpace[Random(0, 2, 1)]
   DirCreate("z:\"&$line&"\Application Data")
   DirCreate("z:\"&$line&"\My Documents")
   DirCreate("z:\"&$line&"\My Pictures")
   DirCreate("z:\"&$line&"\My Videos")
   DirCreate("z:\"&$line&"\My Music")
   Next
  
   local $WriteFile = FileOpen("C:\"&$line&".txt",1)
   $name = StringStripWS($line,8)
   FileWriteLine($writefile, "Welcome to WHAT EVER YOUR BUSINESS IS CALLED")
   FileWriteLine($writefile, "")
   FileWriteLine($writefile, "Username = "& $name)
   FileWriteLine($writefile, "Password = "& $Pwd)
   FileClose($Writefile)

  
      local $OU = "OU=Staff,OU=Active Users,DC=dcname,DC=local"
   Global $iValue = _AD_CreateUser($sOU, $name, $line)
If $iValue = 1 Then
 MsgBox(64, "Active Directory Functions", "User '" & $line & "' in OU '" & $sOU & "' successfully created")
ElseIf @error = 1 Then
 MsgBox(64, "Active Directory Functions", "User '" & $line & "' already exists")
ElseIf @error = 2 Then
 MsgBox(64, "Active Directory Functions", "OU '" & $sOU & "' does not exist")
ElseIf @error = 3 Then
 MsgBox(64, "Active Directory Functions", "Value for CN (e.g. Lastname Firstname) is missing")
ElseIf @error = 4 Then
 MsgBox(64, "Active Directory Functions", "Value for User is missing")
Else
 MsgBox(64, "Active Directory Functions", "Return code '" & @error & "' from Active Directory")
 EndIf
Next
AD_Close()

Now a few things to talk about. First it doesn't set permissions on the users folders. You'll have to either write that bit in yourself or do it manually. Secondly it doesn't set the password on Active directory. I haven't worked out how to do this yet, let me know if you figure out how. Thirdly the active directory section, only fills in the basic information. If you look through the AD functions you'll be able to see what else you can add, but this should start you off.

Have fun and try not to break anything.

Friday, March 30, 2012

AutoIT RSS Feeder

After my last post on how to get the Windows 7 RSS Feeder gadget to start on logon, I started to write my own RSS feed in Autoit. Im sorry to say that it was slightly more than I could do on my own so I had a bit of a Google on it. There were several good examples available and I found one that showed you the basic ways to do it using the XMLDomWapper.au3 file and the appbarhelper.au3 files.

Here's the code.

#include <GUIConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <inet.au3>
#include <array.au3>
#include <_XMLDomWrapper.au3>
#include <appbarHelper.au3>

Local $update_freq = 18000
Local $rss_source = "http://RESTOFTHEURLTOTHEXMLFILE"
Global $rss_gui = _AppbarNew("RSS FEED", "NEWS")

GUISetBkColor(0xFFFFFF)
Local $rss_text = _GetFeedData($rss_source)
Global $rss_label = GUICtrlCreateLabel($rss_text, -1, -1, @DesktopWidth, 40)
GUICtrlSetFont(-1, 14, 400, 2, "Verdana")
GUICtrlSetColor(-1, 0x000000)
GUICtrlSetResizing(-1, $GUI_DOCKLEFT + $GUI_DOCKTOP + $GUI_DOCKSIZE)
GUISwitch($rss_gui)
_AppbarSetDockingEdges(False, False, True, True)
_AppbarDock($rss_gui, $ABE_TOP, 40, 10)
GUISetState(@SW_SHOW, $rss_gui)
While 1
 Sleep(200)
 If Not isMouseOverRSS() Then
  Local $TempText = GUICtrlRead($rss_label)
  GUICtrlSetData($rss_label, StringTrimLeft($TempText, 1) & StringLeft($TempText, 1))
 EndIf
WEnd
Func _GetFeedData($rss_source)
 Local $strXML = _INetGetSource($rss_source)
 _XMLLoadXML($strXML, "")

 Local $title_node = "//rss/channel/item/title";
 Local $title_array = _XMLGetValue($title_node)
 Local $descr_node = "//rss/channel/item/description";
 Local $descr_array = _XMLGetValue($descr_node)
 Local $link_node = "//rss/channel/item/link";
 Local $link_array = _XMLGetValue($link_node)
 Local $feed_data = ""
 For $i = 1 To $title_array[0]
  $feed_data = $feed_data & $title_array[$i] & ": " & $descr_array[$i] & " - "
  If $i = $title_array[0] Then $feed_data = $feed_data & " --END-- "
 Next
 Return $feed_data
EndFunc
Func isMouseOverRSS()
 Local $mouse_info = GUIGetCursorInfo($rss_gui)
 If $mouse_info[4] = $rss_label Then Return True
 Return False
EndFunc

Its pretty simple really and I really should have been able to do it on my own. Still have fun playing with it.

Monday, March 19, 2012

AutoIt countdown timer

So i've been playing around with AutoIt recently. Its a great little piece of software for writting automated programs. Its like a cross between C and python. But the best part about it is that as its comunity based anyone can write their own functions for it and add them to the site. Great!

Anyway, we've selected a few open access computers that are going to be used for electronic submission only. The higher manangement wanted a piece of software that would instruct the student logged on that they would have a maximum of 15 minutes on the PC, and then get logged off. After a bit of discussion I managed to get them to accept giving the students a warning that their time was up and then the counter would count backwards in big red writting.

So this is the code

#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <EditConstants.au3>

$ms = 900250 ;This is the time in milliseconds Adjust as needed
$timer = TimerInit()
traysetstate(2)
$labeltext = "This computer is primarily for electronic submission. You will be automatically logged off after 15 minutes"
GUICreate("Electronic Submission", 350, 330, -1, -1, $WS_DLGFRAME, $WS_EX_TOPMOST)
$Label = GUICtrlCreateLabel($labeltext, 10, 10, 330, 200, $SS_CENTER)
GUICtrlSetFont(-1, 18, 400, $labeltext)
$Input = GUICtrlCreateInput("", 125, 150, 90, 40, BitOr($SS_CENTER, $ES_READONLY, $WS_CLIPCHILDREN))
GUICtrlSetFont(-1, 18, 400)
GUISetState (@SW_SHOW)
WinSetTrans("Electronic Submission","", 127)
While (TimerDiff($timer) < $ms)
   $seconds = TimerDiff($timer)/1000
   $diff = $seconds - ($ms/1000);
   $minutes = Int($diff / 60)
   $secondsRem = $diff - ($minutes * 60)
   $minutes = $minutes * -1
   $secondsRem = $secondsRem * -1
   $time = StringFormat("%02d", $minutes) & ":" & StringFormat("%02d", $secondsRem)
   GUICtrlSetData($Input, $time)
   WEnd
 
   Global $reply = MsgBox(1, "TIMES UP", &@UserName " you have had 15 minutes on this computer. If someone is waiting please let them use it. CLICK OK TO LOG OFF",30)
   If $reply = 1  then run("shutdown.exe -l")
  
   $Input = GUICtrlCreateInput("", 25, 125, 300, 100, BitOr($SS_CENTER, $ES_READONLY, $WS_CLIPCHILDREN))
   GUICtrlSetColor(-1, 0xff0000)
   GUICtrlSetFont(-1, 50, 400)
  
   While (TimerDiff($timer) > $ms)
   $seconds = TimerDiff($timer)/1000
   $diff = $seconds - ($ms/1000)
   $minutes = Int($diff / 60)
   $secondsRem = $diff - ($minutes * 60)
   $minutes = $minutes * -1
   $secondsRem = $secondsRem * -1
   $time = StringFormat("%02d", $minutes) & ":" & StringFormat("%02d", $secondsRem)
   GUICtrlSetData($Input, $time)
   WEnd
   run("shutdown.exe -l")

Its not the best code, but im just learning it. feel free to adjust and use it for yourselves.