caleb41610 Posted September 16, 2012 Share Posted September 16, 2012 Looking for a way to write "on the go" commands and have it execute. Say I have a GUI with nothing but a big edit box and a run button. I'd like to write autoit script inside the edit box, click the button, and have it run as if it were its own script. What's a quick and easy way I can achieve that? Ideally, it will compile the code in to its own script and then run itself. I seen a post a year or so ago with this exact thing. I've searched a bit but haven't found what I was looking for. Multi-Connection TCP Server Link to comment Share on other sites More sharing options...
Spiff59 Posted September 16, 2012 Share Posted September 16, 2012 (edited) Welcome to the forum! It sounds like you're already well on the way to having a working script. Have you looked into the Execute() command in the helpfile? You should be able to read your edit control, StringSplit() it on the @CRLF;s, then use a loop to Execute() each line. If you have any trouble, post what code you have so far here, and I'm sure someone will be glad to help. Edit: You'll likely want your style parameter on the GUICtrlCreateEdit() statement to be $ES_MULTILINE+$ES_WANTRETURN Edited September 16, 2012 by Spiff59 Link to comment Share on other sites More sharing options...
AZJIO Posted September 16, 2012 Share Posted September 16, 2012 My other projects or all Link to comment Share on other sites More sharing options...
Spiff59 Posted September 16, 2012 Share Posted September 16, 2012 I'd thown this together halfway out of boredom, and halfway anticipating the OP might need further assiastance. It does seem to function correctly without involving disk I/O or launching additional processes. #Include <EditConstants.au3> #include <GUIConstantsEx.au3> $sCommands = "Beep(800,100)" & @CRLF & "Sleep(500)" & @CRLF & "Beep(900,100)" & @CRLF & "Sleep(500)" & @CRLF & "Beep(800,100)" GuiCreate("GUI",400,400) $cEdit = GuiCtrlCreateEdit($sCommands,5,5,390,360,$ES_MULTILINE+$ES_WANTRETURN) $cButton = GUICtrlCreateButton("EXECUTE", 160, 373, 80, 20) GuiSetState() GUICtrlSetState($cButton, $GUI_FOCUS) While 1 $msg = GUIGetMsg() Switch $msg Case $cButton Execute_Commands(GUICtrlRead($cEdit)) Case -3 ExitLoop EndSwitch WEnd Func Execute_Commands($sCommands) $aCommands = StringSplit($sCommands, @CRLF) For $x = 1 to $aCommands[0] Execute($aCommands[$x]) Next EndFunc Link to comment Share on other sites More sharing options...
caleb41610 Posted September 16, 2012 Author Share Posted September 16, 2012 Welcome to the forum!It sounds like you're already well on the way to having a working script.Have you looked into the Execute() command in the helpfile?You should be able to read your edit control, StringSplit() it on the @CRLF;s, then use a loop to Execute() each line.If you have any trouble, post what code you have so far here, and I'm sure someone will be glad to help.Edit: You'll likely want your style parameter on the GUICtrlCreateEdit() statement to be $ES_MULTILINE+$ES_WANTRETURNTogether I've achieved my goal. Thanks guys! :-) Multi-Connection TCP Server Link to comment Share on other sites More sharing options...
caleb41610 Posted September 16, 2012 Author Share Posted September 16, 2012 I'd thown this together halfway out of boredom, and halfway anticipating the OP might need further assiastance. It does seem to function correctly without involving disk I/O or launching additional processes. #Include <EditConstants.au3> #include <GUIConstantsEx.au3> $sCommands = "Beep(800,100)" & @CRLF & "Sleep(500)" & @CRLF & "Beep(900,100)" & @CRLF & "Sleep(500)" & @CRLF & "Beep(800,100)" GuiCreate("GUI",400,400) $cEdit = GuiCtrlCreateEdit($sCommands,5,5,390,360,$ES_MULTILINE+$ES_WANTRETURN) $cButton = GUICtrlCreateButton("EXECUTE", 160, 373, 80, 20) GuiSetState() GUICtrlSetState($cButton, $GUI_FOCUS) While 1 $msg = GUIGetMsg() Switch $msg Case $cButton Execute_Commands(GUICtrlRead($cEdit)) Case -3 ExitLoop EndSwitch WEnd Func Execute_Commands($sCommands) $aCommands = StringSplit($sCommands, @CRLF) For $x = 1 to $aCommands[0] Execute($aCommands[$x]) Next EndFunc This will definitely further my plans, I really appreciate it. My goal is to be able to update my client with new commands and features in a client/server remote assistance program, without having to update the server for each person in my client list. My logic is that it will save lots of time and resources when compared to remote desktop. And when clients request additional features, I can easily add them to the client program alone. Currently how it's set up, anyone with access can run any autoit command on any server. Now I just need to make a "white-list" for commands with keywords such as traytip, msgbox, and system tools for assisting customers, without allowing technicians full-on access to the clients system. Multi-Connection TCP Server Link to comment Share on other sites More sharing options...
Exit Posted September 16, 2012 Share Posted September 16, 2012 If there are any logic constructs like If/Then/Else or For/Next in the string, it does not work correctly. See this sample. It should beep forever. #include <EditConstants.au3> #include <GUIConstantsEx.au3> $sCommands = "While 1" & @CRLF & "Beep(800, 100) " & @CRLF & " Sleep(500) " & @CRLF & " Beep(900, 100) " & @CRLF & " Sleep(500) " & @CRLF & " Beep(800, 100) " & @CRLF & " Wend" GUICreate("GUI", 400, 400) $cEdit = GUICtrlCreateEdit($sCommands, 5, 5, 390, 360, $ES_MULTILINE + $ES_WANTRETURN) $cButton = GUICtrlCreateButton("EXECUTE", 160, 373, 80, 20) GUISetState() GUICtrlSetState($cButton, $GUI_FOCUS) While 1 $msg = GUIGetMsg() Switch $msg Case $cButton Execute_Commands(GUICtrlRead($cEdit)) Case -3 ExitLoop EndSwitch WEnd Func Execute_Commands($sCommands) $aCommands = StringSplit($sCommands, @CRLF) For $x = 1 To $aCommands[0] Execute($aCommands[$x]) Next EndFunc ;==>Execute_Commands App: Au3toCmd       UDF: _SingleScript()                 Link to comment Share on other sites More sharing options...
Spiff59 Posted September 17, 2012 Share Posted September 17, 2012 You're right. Even a single-line If/Then like "If FileExists(@ScriptFullPath) Then Beep(800,100)" does not work Apparently invoking the full Autoit interpreter is the only way to process other than the most basic commands. Link to comment Share on other sites More sharing options...
viraco Posted October 16, 2012 Share Posted October 16, 2012 (edited) This has helped a lot. However, I am also looking for a way to generate a gui, sub form to the main gui. i have combo box that when certain selections are made a sub form is displayed. my script has become very cumbersome and continues to grow. my script pulls information from active directory, sql server, and web sites, does CRC32 checks on existing files and uses iNet to download new files if needed. im looking to find a way that my script can download a text file and create the gui from said text file. Edit: forgot to mention that script is compiled to run on machines that dont have autoit installed. Edited October 16, 2012 by viraco Link to comment Share on other sites More sharing options...
BrewManNH Posted October 16, 2012 Share Posted October 16, 2012 Viraco, you're going to need to provide a LOT more information about what you want to do. If all you want to do is download a file that's an AutoIt3 script and run it, look at the command line parameters in the help file, specifically /AutoIt3ExecuteScript and the macro @AutoItExe. If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator Link to comment Share on other sites More sharing options...
viraco Posted October 16, 2012 Share Posted October 16, 2012 (edited) Sorry about that wasnt sure how much more information to add. Started off by using the fileread command example. Script: expandcollapse popup#include <ButtonConstants.au3> #include <ComboConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #include <GuiIPAddress.au3> #include <Array.au3> #include <Constants.au3> #include <WinAPI.au3> #include <GuiToolBar.au3> Func _ExecForm() Local $file = FileOpen(@ScriptDir & "TxtFile.txt", 0) If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf Local $chars = FileRead($file), $i Local $cLines = StringSplit($chars,@CRLF,1) For $i = 1 to $cLines[0] Execute($cLines[$i]) Next FileClose($file) EndFunc _ExecForm() While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd TxtFile: $Form25 = GUICreate("Monitoring Service", 290, 211,default,default,BitOR($WS_POPUPWINDOW,$WS_CAPTION)) $F25Label0 = GUICtrlCreateLabel("Generic User Should Be Selected.", 8, 8, 274, 17) $F25Label1 = GUICtrlCreateLabel("Contact Email", 8, 33, 69, 17) $F25Input1 = GUICtrlCreateInput("user@website.com", 88, 33, 193, 21) $F25Label2 = GUICtrlCreateLabel("Contact Phone", 8, 57, 75, 17) $F25Input2 = GUICtrlCreateInput("555-555-5555", 88, 57, 193, 21) $F25Label3 = GUICtrlCreateLabel("Site Code", 8, 81, 50, 17) $F25Input3 = GUICtrlCreateInput("", 88, 81, 105, 21) $F25Label4 = GUICtrlCreateLabel("Device Name", 8, 105, 69, 17) $F25Input4 = GUICtrlCreateInput("", 88, 105, 193, 21) $F25Label5 = GUICtrlCreateLabel("Ticket Number", 8, 129, 103, 16) $F25Input5 = GUICtrlCreateInput("", 8, 145, 273, 21) $F25Button1 = GUICtrlCreateButton("Apply", 200, 177, 81, 25) GUICtrlSetColor($F25Label0,0x0000ff) GUICtrlSetColor($F25Label1,0xff0000) GUICtrlSetColor($F25Label2,0xff0000) GUICtrlSetColor($F25Label3,0xff0000) GUICtrlSetColor($F25Label4,0xff0000) GUICtrlSetColor($F25Label5,0xff0000) GUISetState(@SW_SHOW, $Form25) Edited October 16, 2012 by viraco Link to comment Share on other sites More sharing options...
kylomas Posted October 16, 2012 Share Posted October 16, 2012 cable41610, The scriptomatic script does something similar to what you are looking for. expandcollapse popup; AutoIt ScriptOMatic ; ------------------- ; ; AutoIt's counterpart of Microsoft's Scriptomatic ; ; Author: SvenP ; Date/version: 2005-04-17 ; See also: [url="http://www.microsoft.com/technet/scriptcenter/tools/scripto2.mspx"]http://www.microsoft.com/technet/scriptcenter/tools/scripto2.mspx[/url] ; Requires: AutoIt beta version 3.1.1.8 or higher (COM support!!) ; ; GUI generated by AutoBuilder 0.5 Prototype #include <GuiConstants.au3> #include <windowsConstants.au3> ;************************ ;* Global State Variables ;************************ $g_strCurrentNamespace = "rootCIMV2" $g_iCurrentNamespaceIndex = 0 $g_strWMISource = "localhost" $g_strOutputFormat = "Dialog" ;************************ ;* Main GUI ;************************ GuiCreate("AutoIt Scriptomatic Tool", 684, 561,(@DesktopWidth-684)/2, (@DesktopHeight-561)/2 , $WS_OVERLAPPEDWINDOW + $WS_VISIBLE + $WS_CLIPSIBLINGS) $GUI_AST_MainGroup = GuiCtrlCreateGroup("", 10, 10,660,530) $GUI_WMI_NamespaceLabel = GuiCtrlCreateLabel("WMI Namespace", 20, 30,150, 20) $GUI_WMI_Namespace = GuiCtrlCreateCombo("WMI_Namespaces", 20, 50,280, 80) $GUI_WMI_ClassLabel = GuiCtrlCreateLabel("WMI Class", 320, 30,240, 20) $GUI_WMI_Classes = GuiCtrlCreateCombo("WMI_Classes", 320, 50,340, 80) $GUI_AST_ButtonGroup = GuiCtrlCreateGroup("", 10, 80,660, 50) $GUI_AST_Run = GuiCtrlCreateButton("Run", 20,100, 50, 20) $GUI_AST_CIMv2 = GuiCtrlCreateButton("CIMv2", 80,100, 50, 20) $GUI_AST_WMISource = GuiCtrlCreateButton("WMISource", 140,100, 70, 20) $GUI_AST_Open = GuiCtrlCreateButton("Open", 220,100, 60, 20) $GUI_AST_Save = GuiCtrlCreateButton("Save", 290,100, 60, 20) $GUI_AST_Quit = GuiCtrlCreateButton("Quit", 360,100, 60, 20) $GUI_AST_OptionGroup = GuiCtrlCreateGroup("Output", 430, 80,240, 50) $GUI_AST_RadioDialog = GuiCtrlCreateRadio("Dialog", 440,100, 50, 20) $GUI_AST_RadioText = GuiCtrlCreateRadio("Text", 510,100, 50, 20) $GUI_AST_RadioHTML = GuiCtrlCreateRadio("HTML", 570,100, 50, 20) $GUI_AST_ScriptCode = GuiCtrlCreateEdit("One moment...", 20,140,640,390) GuiSetState() ; Initial GUI Settings GUICtrlSetState($GUI_AST_RUN, $GUI_DISABLE) GUICtrlSetState($GUI_AST_SAVE, $GUI_DISABLE) GUICtrlSetState($GUI_AST_RadioDialog,$GUI_CHECKED) ; Fill the WMI_Namespaces Combobox LoadWMINamespaces() ; Fill the WMI_Classes Combobox HandleNamespaceChange() While 1 $msg = GuiGetMsg() Select Case $msg = $GUI_EVENT_CLOSE ExitLoop Case $msg = $GUI_AST_QUIT ExitLoop Case $msg = $GUI_WMI_Namespace HandleNameSpaceChange() Case $msg = $GUI_WMI_Classes ComposeCode() Case $msg = $GUI_AST_Run RunScript() Case $msg = $GUI_AST_Save SaveScript() Case $msg = $GUI_AST_Open OpenScript() Case $msg = $GUI_AST_CIMv2 SetNamespaceToCIMV2() Case $msg = $GUI_AST_WMISource SetWMIRepository() Case $msg = $GUI_AST_RadioDialog or _ $msg = $GUI_AST_RadioText or _ $msg = $GUI_AST_RadioHTML HandleOutputChange() EndSelect WEnd GUIDelete() Exit ;******************************************************************** ;* LoadWMINamespaces ;******************************************************************** Func LoadWMINamespaces() $strCsvListOfNamespaces = "" $strNameSpacesCombo="" $strWaitNamespaces="Please wait, Loading WMI Namespaces" GUICtrlSetData($GUI_WMI_Namespace,$strWaitNamespaces,$strWaitNamespaces) EnumNameSpaces("root", $strCsvListOfNamespaces) $arrNamespaces = StringSplit($strCsvListOfNamespaces, ",") For $strNamespace in $arrNamespaces $strNameSpacesCombo = $strNameSpacesCombo & "|" & $strNamespace Next GUICtrlSetData($GUI_WMI_Namespace,$strNameSpacesCombo,"ROOTCIMV2") EndFunc ;******************************************************************** ;* EnumNamespaces ;******************************************************************** Func EnumNamespaces($strNamespace, ByRef $tmpCsvListOfNamespaces) If $tmpCsvListOfNamespaces = "" Then $tmpCsvListOfNamespaces = $strNamespace Else $tmpCsvListOfNamespaces = $tmpCsvListOfNamespaces & "," & $strNamespace EndIf $strComputer = $g_strWMISource $objWMIService = ObjGet("winmgmts:" & $g_strWMISource & "" & $strNameSpace) If not @error Then $colNameSpaces = $objWMIService.InstancesOf("__NAMESPACE") For $objNameSpace In $colNameSpaces EnumNamespaces($strNameSpace & "" & $objNameSpace.Name, $tmpCsvListOfNamespaces) Next Else $tmpCsvListOfNamespaces="" EndIf EndFunc ;******************************************************************** ;* HandleNamespaceChange ;******************************************************************** Func HandleNamespaceChange() ;''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ; Clear the WMI classes pulldown location. ;''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' $strSelectedNamespace = GUICtrlRead ( $GUI_WMI_Namespace ) ; Disable the namespace combobox until class load has been completed GUICtrlSetState($GUI_WMI_Namespace, $GUI_DISABLE) $strWMIWaitMsg = "Please wait, trying to load WMI Classes in namespace " & $strSelectedNamespace GUICtrlSetData($GUI_WMI_Classes, $strWMIWaitMsg, $strWMIWaitMsg) GUICtrlSetData($GUI_AST_ScriptCode,"One moment...","") LoadWMIClasses() $g_strCurrentNamespace = "" & $strSelectedNamespace ;''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ; Clear the code textarea and disable run and save. ;''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' GUICtrlSetData($GUI_AST_ScriptCode,"","") GUICtrlSetState($GUI_WMI_Namespace, $GUI_ENABLE) GUICtrlSetState($GUI_AST_RUN, $GUI_DISABLE) GUICtrlSetState($GUI_AST_SAVE, $GUI_DISABLE) EndFunc ;******************************************************************** ;* LoadWMIClasses ;* ;* Fetch all the classes in the currently selected namespace, and ;* populate the keys of a dictionary object with the names of all ;* dynamic (non-association) classes. Then we transfer the keys to ;* an array, sort the array, and finally use the sorted array to ;* populate the WMI classes pulldown. ;******************************************************************** Func LoadWMIClasses() Const $SORT_KEYS = 1 Const $SORT_ITEMS = 2 $objClassDictionary = ObjCreate("Scripting.Dictionary") $objQualifierDictionary = ObjCreate("Scripting.Dictionary") $strComputer = "." $objWMIService = ObjGet("winmgmts:" & $strComputer & $g_strCurrentNamespace) If not @error Then For $objClass in $objWMIService.SubclassesOf() For $objQualifier In $objClass.Qualifiers_() ; Dummy (), because it ends with an underscore ! $objQualifierDictionary.Add(StringLower($objQualifier.Name), "") Next If $objQualifierDictionary.Exists("dynamic") Then ;$TempVar = $objClass.Path_.Class ;$objClassDictionary.Add($TempVar, "") ; Can't use object in arguments ?!! $objClassDictionary.Add($objClass.Path_.Class, "") EndIf $objQualifierDictionary.RemoveAll Next $objQualifierDictionary = "" ;''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ; If the current namespace contains dynamic classes... ;''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' If $objClassDictionary.Count Then ;'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ; Sort the dictionary. ;'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' SortDictionary($objClassDictionary, $SORT_KEYS) ;'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ; Populate the WMI classes pulldown with the sorted dictionary. ;'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' $strClassesCombo="|Select a WMI class" For $strWMIClass in $objClassDictionary ; method .Keys is not an object ?? $strClassesCombo = $strClassesCombo & "|" & $strWMIClass Next GUICtrlSetData($GUI_WMI_Classes,$strClassesCombo,"Select a WMI class") EndIf EndIf If @error Or $objClassDictionary.Count = 0 Then ;'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ; And if the current namespace doesn't contain dynamic classes. ;'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' GUICtrlSetData($GUI_WMI_Classes,"|No dynamic classes found in current namespace.|Select a different namespace","") EndIf $objClassDictionary = "" EndFunc ;******************************************************************** ;* SortDictionary ;* ;* Shell sort based on: ;* [url="http://support.microsoft.com/support/kb/articles/q246/0/67.asp"]http://support.microsoft.com/support/kb/articles/q246/0/67.asp[/url] ;******************************************************************** Func SortDictionary(ByRef $objDict, $intSort) Const $dictKey = 1 Const $dictItem = 2 Dim $strDict[1][3] $intCount = $objDict.Count If $intCount > 1 Then ReDim $strDict[$intCount][3] $i = 0 For $objKey In $objDict $strDict[$i][$dictKey] = String($objKey) $strDict[$i][$dictItem] = String($objDict($objKey)) $i = $i + 1 Next ;'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ; Perform a shell sort of the 2D string array ;'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' For $i = 0 To ($intCount - 2) For $j = $i To ($intCount - 1) If $strDict[$i][$intSort] > $strDict[$j][$intSort] Then $strKey = $strDict[$i][$dictKey] $strItem = $strDict[$i][$dictItem] $strDict[$i][$dictKey] = $strDict[$j][$dictKey] $strDict[$i][$dictItem] = $strDict[$j][$dictItem] $strDict[$j][$dictKey] = $strKey $strDict[$j][$dictItem] = $strItem EndIf Next Next ;'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ; Erase the contents of the dictionary object ;'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' $objDict.RemoveAll ;'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ; Repopulate the dictionary with the sorted information ;'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' For $i = 0 To ($intCount - 1) $objDict.Add($strDict[$i][$dictKey], $strDict[$i][$dictItem]) Next EndIf EndFunc ;******************************************************************** ;* ComposeCode ;******************************************************************** Func ComposeCode() $objClass="" $strSelectedClass = GUICtrlRead ( $GUI_WMI_Classes ) ; Check if a valid class has been selected If $strSelectedClass <> "Select a WMI class" Then $bHasDates = false ; Flag: output has date fields $strHeaderStart=Chr(34) $strRowStart=Chr(34) $strColumnSeparator=": " $strRowEnd=" & @CRLF" $strComputerCommand = "$strComputer = " & Chr(34) & $g_strWMISource & Chr(34) ;$objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!" & @COMPUTERNAME & $g_strCurrentNamespace) $objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!." & $g_strCurrentNamespace) $objClass = $objWMIService.Get($strSelectedClass) If IsObj($objClass) Then $strScriptCode = "" $strScriptCode = $strScriptCode & "; Generated by AutoIt Scriptomatic" & @CRLF & @CRLF $strScriptCode = $strScriptCode & "$wbemFlagReturnImmediately = 0x10" & @CRLF $strScriptCode = $strScriptCode & "$wbemFlagForwardOnly = 0x20" & @CRLF $strScriptCode = $strScriptCode & '$colItems = ""' & @CRLF $strScriptCode = $strScriptCode & $strComputerCommand & @CRLF & @CRLF $strScriptCode = $strScriptCode & '$Output=""' & @CRLF If $g_strOutputFormat = "HTML" then $strScriptCode = $strScriptCode & "$Output = $Output & '<html><head><title>Scriptomatic HTML Output</title></head><body> " & _ "<style>table {font-size: 10pt; font-family: arial;} th {background-color: buttonface; font-decoration: bold;} " & _ "</style><table BORDER=" & Chr(34) & "1" & Chr(34) & "><tr><th>Property</th><th>Value</th></tr>'" & @CRLF $strRowStart = Chr(34) & "<tr><td>" $strHeaderStart = "'<tr bgcolor=" & Chr(34) & "yellow" & Chr(34) & "><td>' & " & Chr(34) $strColumnSeparator = "</td><td> " $strRowEnd = " & " & Chr(34) & "</td></tr>" & Chr(34) & " & @CRLF" EndIf $strScriptCode = $strScriptCode & "$Output = $Output & " & $strHeaderStart & "Computer" & $strColumnSeparator & Chr(34) & " & $strComputer " & $strRowEnd & @CRLF If $g_strOutputFormat = "Dialog" then $strScriptCode = $strScriptCode & "$Output = $Output & " & Chr(34) & "==========================================" & Chr(34) & $strRowEnd & @CRLF EndIf $strScriptCode = $strScriptCode & "$objWMIService = ObjGet(" & Chr(34) & "winmgmts:" & Chr(34) & " & $strComputer & " & Chr(34) & $g_strCurrentNamespace & Chr(34) & ")" & @CRLF $strScriptCode = $strScriptCode & "$colItems = $objWMIService.ExecQuery(" & Chr(34) & "SELECT * FROM " & $strSelectedClass & Chr(34) & ", " & Chr(34) & "WQL" & Chr(34) & ", _" & @CRLF $strScriptCode = $strScriptCode & " $wbemFlagReturnImmediately + $wbemFlagForwardOnly)" & @CRLF & @CRLF $strScriptCode = $strScriptCode & "If IsObj($colItems) then" & @CRLF $strScriptCode = $strScriptCode & " For $objItem In $colItems" & @CRLF For $objProperty in $objClass.Properties_() ; Must use (), because method ends with an underscore If $objProperty.IsArray = True Then $strScriptCode = $strScriptCode & " $str" & $objProperty.Name & " = $objItem." & $objProperty.Name & "(0)" & @CRLF $strScriptCode = $strScriptCode & " $Output = $Output & " & $strRowStart & $objProperty.Name & $strColumnSeparator & Chr(34) & " & $str" & $objProperty.Name & $strRowEnd & @CRLF ElseIf $objProperty.CIMTYPE = 101 Then $bHasDates = True $strScriptCode = $strScriptCode & " $Output = $Output & " & $strRowStart & $objProperty.Name & $strColumnSeparator & Chr(34) & " & WMIDateStringToDate($objItem." & $objProperty.Name & ")" & $strRowEnd & @CRLF Else $strScriptCode = $strScriptCode & " $Output = $Output & " & $strRowStart & $objProperty.Name & $strColumnSeparator & Chr(34) & " & $objItem." & $objProperty.Name & $strRowEnd & @CRLF EndIf Next If $g_strOutputFormat = "Dialog" then $strScriptCode = $strScriptCode & ' if Msgbox(1,"WMI Output",$Output) = 2 then ExitLoop' & @CRLF $strScriptCode = $strScriptCode & ' $Output=""' & @CRLF Endif $strScriptCode = $strScriptCode & " Next" & @CRLF if $g_strOutputFormat = "Text" then $strScriptCode = $strScriptCode & ' ConsoleWrite($Output)' & @CRLF $strScriptCode = $strScriptCode & ' FileWrite(@TempDir & "' & $strSelectedClass & '.TXT", $Output )' & @CRLF $strScriptCode = $strScriptCode & ' Run(@Comspec & " /c start " & @TempDir & "' & $strSelectedClass & '.TXT" )' & @CRLF Elseif $g_strOutputFormat = "HTML" then $strScriptCode = $strScriptCode & ' FileWrite(@TempDir & "' & $strSelectedClass & '.HTML", $Output )' & @CRLF $strScriptCode = $strScriptCode & ' Run(@Comspec & " /c start " & @TempDir & "' & $strSelectedClass & '.HTML" )' & @CRLF Endif $strScriptCode = $strScriptCode & "Else" & @CRLF $strScriptCode = $strScriptCode & ' Msgbox(0,"WMI Output","No WMI Objects Found for class: " & ' & Chr(34) & $strSelectedClass & Chr(34) & ' )' & @CRLF $strScriptCode = $strScriptCode & "Endif" & @CRLF $strScriptCode = $strScriptCode & @CRLF & @CRLF If $bHasDates Then $strScriptCode = $strScriptCode & "Func WMIDateStringToDate($dtmDate)" & @CRLF $strScriptCode = $strScriptCode & @CRLF $strScriptCode = $strScriptCode & Chr(9) & "Return (StringMid($dtmDate, 5, 2) & ""/"" & _" & @CRLF $strScriptCode = $strScriptCode & Chr(9) & "StringMid($dtmDate, 7, 2) & ""/"" & StringLeft($dtmDate, 4) _" & @CRLF $strScriptCode = $strScriptCode & Chr(9) & "& "" "" & StringMid($dtmDate, 9, 2) & "":"" & StringMid($dtmDate, 11, 2) & "":"" & StringMid($dtmDate,13, 2))" & @CRLF $strScriptCode = $strScriptCode & "EndFunc" EndIf Else $strScriptCode = "Error: No Class properties found for " & $g_strCurrentNamespace & "" & $strSelectedClass EndIf GUICtrlSetData($GUI_AST_ScriptCode,$strScriptCode) ;''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ; Once the code is successfully composed and put into the ; textarea, ensure that the run and save buttons are enabled. ;''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' GUICtrlSetState($GUI_AST_RUN, $GUI_ENABLE) GUICtrlSetState($GUI_AST_SAVE, $GUI_ENABLE) Else ; Disable Run and Save buttons, because no valid code has been generated GUICtrlSetState($GUI_AST_RUN, $GUI_DISABLE) GUICtrlSetState($GUI_AST_SAVE, $GUI_DISABLE) EndIf EndFunc ;******************************************************************** ;* RunScript ;******************************************************************** Func RunScript() ;''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ; Create a temporary script file named "temp_script.au3". ;''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' $strTmpName = @TempDir & "temp_script.au3" if FileExists($strTmpName) then FileDelete($strTmpName) FileWrite($strTmpName,GUICtrlRead($GUI_AST_Scriptcode)) ;''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ; Start constructing the command line that will run the script... ;''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' $strCmdLine = @AutoItExe & " " & $strTmpName RunWait($strCmdLine) FileDelete($strTmpName) EndFunc ;******************************************************************** ;* SaveScript ;******************************************************************** Func SaveScript() $strTmpName=FileSaveDialog("Save Script",@DesktopDir,"AutoIt3 Scripts (*.au3)",16, GUICtrlRead ( $GUI_WMI_Classes )) if not @error and $strTmpName <> "" then if StringRight($strTmpName,4) <> ".AU3" then $strTmpName=$strTmpName & ".AU3" if FileExists($strTmpName) then FileDelete($strTmpName) FileWrite($strTmpName,GUICtrlRead($GUI_AST_Scriptcode)) EndIf EndFunc ;******************************************************************** ;* OpenScript ;******************************************************************** Func OpenScript() $strTmpName=FileOpenDialog("Open Script",@DesktopDir,"AutoIt3 Scripts (*.au3)") If not @error and $strTmpName <> "" then If FileExists($strTmpName) then GUICtrlSetData($GUI_AST_ScriptCode,FileRead($strTmpName,FileGetSize($strTmpName))) EndIf EndIf EndFunc ;**************************************************************************** ;* SetNamespaceToCIMV2 ;**************************************************************************** Func SetNamespaceToCIMV2() If StringUpper(GUICtrlRead($GUI_WMI_Namespace)) <> "ROOTCIMV2" Then GUICtrlSetData($GUI_WMI_Namespace,"ROOTCIMV2","ROOTCIMV2") HandleNamespaceChange() EndIf EndFunc ;**************************************************************************** ;* SetWMIRepository ;**************************************************************************** Func SetWMIRepository() $strWMISourceName = InputBox("Set WMI Repository Source", _ "Please enter the computer whose WMI repository you want to read from: ", _ $g_strWMISource) If $strWMISourceName <> "" Then $g_strWMISource = StringStripWS($strWMISourceName,1+2) ;target_computers.Value = $g_strWMISource LoadWMINamespaces() Endif EndFunc ;**************************************************************************** ;* HandleOutputChange ;**************************************************************************** Func HandleOutputChange() $ChosenFormat = $g_strOutputFormat if GUICtrlRead($GUI_AST_RadioDialog)=$GUI_CHECKED then $ChosenFormat="Dialog" if GUICtrlRead($GUI_AST_RadioText)=$GUI_CHECKED then $ChosenFormat="Text" if GUICtrlRead($GUI_AST_RadioHTML)=$GUI_CHECKED then $ChosenFormat="HTML" if $ChosenFormat <> $g_strOutputFormat Then $g_strOutputFormat = $ChosenFormat ComposeCode() EndIf EndFunc kylomas Forum Rules        Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
viraco Posted October 16, 2012 Share Posted October 16, 2012 thank kylomas for the information. However, i started reading about execut, assign, eval, IniRead, and INIReadSection more then started coding. INIFile: expandcollapse popup[GUIVar] FormVar = Form25 ErrorVar = ErrorTxt ApplyVar = F25Button1 [GUIGen] Form25 = GUICreate('Monitoring Service', 290, 211,default,default,BitOR($WS_POPUPWINDOW,$WS_CAPTION)) F25Label0 = GUICtrlCreateLabel('Generic User Should Be Selected In SM7.', 8, 8, 274, 17) F25Label1 = GUICtrlCreateLabel('Contact Email', 8, 33, 69, 17) F25Input1 = GUICtrlCreateInput('user@website.com', 88, 33, 193, 21) F25Label2 = GUICtrlCreateLabel('Contact Phone', 8, 57, 75, 17) F25Input2 = GUICtrlCreateInput('555-555-5555', 88, 57, 193, 21) F25Label3 = GUICtrlCreateLabel('Site Code', 8, 81, 50, 17) F25Input3 = GUICtrlCreateInput('', 88, 81, 105, 21) F25Label4 = GUICtrlCreateLabel('Device Name', 8, 105, 69, 17) F25Input4 = GUICtrlCreateInput('', 88, 105, 193, 21) F25Label5 = GUICtrlCreateLabel('Ticket Number', 8, 129, 103, 16) F25Input5 = GUICtrlCreateInput('', 8, 145, 273, 21) F25Button1 = GUICtrlCreateButton('Apply', 200, 177, 81, 25) [GUICmd] c1 = GUICtrlSetColor($F25Label0,0x0000ff) c2 = GUICtrlSetColor($F25Label1,0xff0000) c3 = GUICtrlSetColor($F25Label2,0xff0000) c4 = GUICtrlSetColor($F25Label3,0xff0000) c5 = GUICtrlSetColor($F25Label4,0xff0000) c6 = GUICtrlSetColor($F25Label5,0xff0000) c7 = GUISetState(@SW_SHOW, $Form25) c8 = $ErrorTxt = '' [GUIChkIf] c1 = StringStripWS(GUICtrlRead($F25Input1),3) = '' c2 = StringStripWS(GUICtrlRead($F25Input2),3) = '' c3 = StringStripWS(GUICtrlRead($F25Input3),3) = '' c4 = StringStripWS(GUICtrlRead($F25Input4),3) = '' c5 = StringStripWS(GUICtrlRead($F25Input5),3) = '' [GUIChkThn] c1 = $ErrorTxt & GUICtrlRead($F25Label1) & @CRLF c2 = $ErrorTxt & GUICtrlRead($F25Label2) & @CRLF c3 = $ErrorTxt & GUICtrlRead($F25Label3) & @CRLF c4 = $ErrorTxt & GUICtrlRead($F25Label4) & @CRLF c5 = $ErrorTxt & GUICtrlRead($F25Label5) & @CRLF AU3 Code: expandcollapse popup#include <ButtonConstants.au3> #include <ComboConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #include <GuiIPAddress.au3> #include <Array.au3> #include <Constants.au3> #include <WinAPI.au3> #include <GuiToolBar.au3> Global $gRead ;GUI Global $cRead ; Global $vRead Global $mRead Global $aRead Global $kRead Global $tRead _ExecForm() ;~ _ChkForm() Func _ExecForm() $vRead = IniRead(@ScriptDir & "RelatedFormsCiscoMonitoringService.ini", "GUIVar","FormVar",0) ;Read Form Var Name $mRead = IniRead(@ScriptDir & "RelatedFormsCiscoMonitoringService.ini", "GUIVar","ErrorVar",0) ;Read Error Var Name $aRead = IniRead(@ScriptDir & "RelatedFormsCiscoMonitoringService.ini", "GUIVar","ApplyVar",0) ;Read Apply Var Name Assign($mRead,"",2) ;Set Error Var Name to blank $gRead = IniReadSection(@ScriptDir & "RelatedFormsCiscoMonitoringService.ini", "GUIGen") For $i = 1 To $gRead[0][0] Assign($gRead[$i][0],execute($gRead[$i][1]),2) Next $cRead = IniReadSection(@ScriptDir & "RelatedFormsCiscoMonitoringService.ini", "GUICmd") For $i = 1 to $cRead[0][0] Execute($cRead[$i][1]) Next GUISetState(@SW_SHOW,eval($vRead)) EndFunc Func _ChkForm() Local $Temp $kRead = IniReadSection(@ScriptDir & "RelatedFormsCiscoMonitoringService.ini", "GUIChkIf") For $i = 1 To $kRead[0][0] ;~ msgbox(0,$kRead[$i][0],$kRead[$i][1]) $tRead = IniRead(@ScriptDir & "RelatedFormsCiscoMonitoringService.ini", "GUIChkThn",$kRead[$i][0],0) $Temp = Execute($kRead[$i][1]) if not @error = 0 then msgbox(0,$mRead,"Error Executing: " & $tRead) if $Temp then Assign($mRead,Execute($tRead)) if not @error = 0 then msgbox(0,$mRead,"Error Executing: " & $tRead) Next MsgBox(0,$mRead, eval($mRead)) Assign($mRead,"") EndFunc While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case eval($aRead) _ChkForm() EndSwitch WEnd Link to comment Share on other sites More sharing options...
kylomas Posted October 16, 2012 Share Posted October 16, 2012 viraco, Excellent, you are on the path to wisdom. The scriptomatic code was to provide an example of dynamically constructing and executing a script. kylomas Forum Rules        Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now