grimmlock reacted to
DicatoroftheUSA in HTA to AutoIT
December 3, 2012
Here are some suggestions
#include<guiconstants.au3>
#include<File.au3>
Global $goCommReturn[2]
Global $goCommError = ObjEvent("AutoIt.Error", "CommError")
;put main script in main for easier delmination of global declations,registers, includes etc.
;DIM is not ideal. Use global for variables meant to be used ubiquitously, local for variables in a function. Add [localglobal] const for variables that do not change
;I prefer not using oneventmode so that nonstandard global variables are not required. and I think it is easier to debug cause/effect.
; put "&" on labels and buttons before their corresponding forms to make keyboard navigation easier.
_main()
Func _Main()
while 1
_gui()
WEnd
EndFunc ;==>_Main
Func _gui()
Local $sUsername = @UserName ; User Name
Local $sComputername = @ComputerName ; Computer Name
Local $sTexts = ""; Output text for message box.
Local $sText2 = ""; Output text for message box.
Local $combo1 = ""; Output text for combo box.
Local Const $sDomain = 'Acme.com'
Local Const $sServer = "MailServerName" & '.' & $sDomain
Local Const $sSupportEmail = "support" & '@' & $sDomain
ClipPut($sServer)
$sTexts = "Username = " & $sUsername & @CRLF ; The & character concentrates (joins) two values together.
$sText2 = "Computer Name = " & $sComputername & @CRLF ; The &= adds to the variable. The same as doing $var = $var & "extra stuff"
Local $hMainwindow = GUICreate("HelpDesk App", 300, 600)
GUICtrlCreateLabel("Helpdesk App", 30, 10, 290, 40)
GUICtrlSetFont(-1, 20, 800); bold
GUICtrlCreateLabel($sTexts, 25, 60)
GUICtrlCreateLabel($sText2, 25, 78)
$hResetButton = GUICtrlCreateButton("&Reset", 115, 565, 80); *Commented Out; Would like to learn how to create a form and clear the data
Local $hOkButton = GUICtrlCreateButton("&Send", 200, 565, 80)
GUISetState(@SW_SHOW)
; _AddHorzSep(10, 40, 290)
_AddHorzSep(10, 105, 280)
_AddHorzSep(10, 210, 280)
GUICtrlCreateLabel("*Please provide the above information when you contact the Help Desk for any hardware or software problems.", 20, 120, 250, 50)
GUICtrlCreateLabel("Helpdesk #: (555) 555-5555", 85, 173)
GUICtrlCreateLabel("&Helpdesk Email:", 70, 188)
$hEmail = GUICtrlCreateLabel($sSupportEmail, 148, 188);helpdesk@domain.com"
GUICtrlSetFont($hEmail, 8.5, -1, 4) ; underlined
GUICtrlSetColor($hEmail, 0x0000ff)
GUICtrlSetCursor($hEmail, 0)
GUICtrlCreateLabel("Helpdesk Request Form", 30, 220, 270, 50)
GUICtrlSetFont(-1, 15, 800); bold
GUICtrlCreateLabel("&Request Type:", 25, 273, 75)
Local $hCombo = GUICtrlCreateCombo("", 100, 270, 160) ; create first item
GUICtrlSetData(-1, "Keyboard/Mouse|Monitor|Tower", "Keyboard/Mouse") ; add other item snd set a new default
GUICtrlCreateLabel("Su&bject:", 25, 303, 75)
Local $hInputText = GUICtrlCreateInput("", 100, 300, 160)
GUICtrlCreateLabel("&Details:", 25, 330, 75)
GUICtrlCreateInput("", 25, 345, 235, 200)
While 1
Switch GUIGetMsg()
Case $hOkButton
$msg = GUICtrlRead($hInputText) & @CRLF & "----------------" & @CRLF & $sTexts & @CRLF & $sText2 & @CRLF & _wmigetcompinfo()
OKButton($sServer, $sUsername, $sUsername & '@' & $sDomain, $sSupportEmail, "Issue:" & GUICtrlRead($hCombo), $msg)
Case $hEmail
OnEmail($sSupportEmail)
case $hResetButton
ExitLoop
Case $GUI_EVENT_CLOSE
Exit
;~ Send()
EndSwitch
WEnd
EndFunc
Func _AddHorzSep($iX, $iY, $iW)
GUICtrlCreateLabel("", $iX, $iY, $iW, 1)
GUICtrlSetBkColor(-1, 0x000000)
EndFunc ;==>_AddHorzSep
Func _wmigetcompinfo($strComputer = "localhost")
; Generated by AutoIt Scriptomatic November 30, 2012
$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$sOutput = ""
$sOutput &= "Computer: " & $strComputer & @CRLF
$sOutput &= "==========================================" & @CRLF
$objWMIService = ObjGet("winmgmts:" & $strComputer & "rootCIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem", "WQL", _
$wbemFlagReturnImmediately + $wbemFlagForwardOnly)
If IsObj($colItems) Then
For $objItem In $colItems
;~ $sOutput &= "AdminPasswordStatus: " & $objItem.AdminPasswordStatus & @CRLF
;~ $sOutput &= "AutomaticResetBootOption: " & $objItem.AutomaticResetBootOption & @CRLF
;~ $sOutput &= "AutomaticResetCapability: " & $objItem.AutomaticResetCapability & @CRLF
;~ $sOutput &= "BootOptionOnLimit: " & $objItem.BootOptionOnLimit & @CRLF
;~ $sOutput &= "BootOptionOnWatchDog: " & $objItem.BootOptionOnWatchDog & @CRLF
;~ $sOutput &= "BootROMSupported: " & $objItem.BootROMSupported & @CRLF
;~ $sOutput &= "BootupState: " & $objItem.BootupState & @CRLF
$sOutput &= "Caption: " & $objItem.Caption & @CRLF
;~ $sOutput &= "ChassisBootupState: " & $objItem.ChassisBootupState & @CRLF
;~ $sOutput &= "CreationClassName: " & $objItem.CreationClassName & @CRLF
$sOutput &= "CurrentTimeZone: " & $objItem.CurrentTimeZone & @CRLF
$sOutput &= "DaylightInEffect: " & $objItem.DaylightInEffect & @CRLF
$sOutput &= "Description: " & $objItem.Description & @CRLF
$sOutput &= "Domain: " & $objItem.Domain & @CRLF
$sOutput &= "DomainRole: " & $objItem.DomainRole & @CRLF
$sOutput &= "EnableDaylightSavingsTime: " & $objItem.EnableDaylightSavingsTime & @CRLF
;~ $sOutput &= "FrontPanelResetStatus: " & $objItem.FrontPanelResetStatus & @CRLF
;~ $sOutput &= "InfraredSupported: " & $objItem.InfraredSupported & @CRLF
;~ $strInitialLoadInfo = $objItem.InitialLoadInfo(0)
;~ $sOutput &= "InitialLoadInfo: " & $strInitialLoadInfo & @CRLF
$sOutput &= "InstallDate: " & WMIDateStringToDate($objItem.InstallDate) & @CRLF
;~ $sOutput &= "KeyboardPasswordStatus: " & $objItem.KeyboardPasswordStatus & @CRLF
$sOutput &= "LastLoadInfo: " & $objItem.LastLoadInfo & @CRLF
$sOutput &= "Manufacturer: " & $objItem.Manufacturer & @CRLF
$sOutput &= "Model: " & $objItem.Model & @CRLF
$sOutput &= "Name: " & $objItem.Name & @CRLF
$sOutput &= "NameFormat: " & $objItem.NameFormat & @CRLF
$sOutput &= "NetworkServerModeEnabled: " & $objItem.NetworkServerModeEnabled & @CRLF
;~ $sOutput &= "NumberOfLogicalProcessors: " & $objItem.NumberOfLogicalProcessors & @CRLF
;~ $sOutput &= "NumberOfProcessors: " & $objItem.NumberOfProcessors & @CRLF
;~ $strOEMLogoBitmap = $objItem.OEMLogoBitmap(0)
;~ $sOutput &= "OEMLogoBitmap: " & $strOEMLogoBitmap & @CRLF
$strOEMStringArray = $objItem.OEMStringArray(0)
$sOutput &= "OEMStringArray: " & $strOEMStringArray & @CRLF
$sOutput &= "PartOfDomain: " & $objItem.PartOfDomain & @CRLF
;~ $sOutput &= "PauseAfterReset: " & $objItem.PauseAfterReset & @CRLF
;~ $strPowerManagementCapabilities = $objItem.PowerManagementCapabilities(0)
;~ $sOutput &= "PowerManagementCapabilities: " & $strPowerManagementCapabilities & @CRLF
;~ $sOutput &= "PowerManagementSupported: " & $objItem.PowerManagementSupported & @CRLF
;~ $sOutput &= "PowerOnPasswordStatus: " & $objItem.PowerOnPasswordStatus & @CRLF
;~ $sOutput &= "PowerState: " & $objItem.PowerState & @CRLF
;~ $sOutput &= "PowerSupplyState: " & $objItem.PowerSupplyState & @CRLF
;~ $sOutput &= "PrimaryOwnerContact: " & $objItem.PrimaryOwnerContact & @CRLF
;~ $sOutput &= "PrimaryOwnerName: " & $objItem.PrimaryOwnerName & @CRLF
;~ $sOutput &= "ResetCapability: " & $objItem.ResetCapability & @CRLF
;~ $sOutput &= "ResetCount: " & $objItem.ResetCount & @CRLF
;~ $sOutput &= "ResetLimit: " & $objItem.ResetLimit & @CRLF
;~ $strRoles = $objItem.Roles(0)
;~ $sOutput &= "Roles: " & $strRoles & @CRLF
;~ $sOutput &= "Status: " & $objItem.Status & @CRLF
;~ $strSupportContactDescription = $objItem.SupportContactDescription(0)
;~ $sOutput &= "SupportContactDescription: " & $strSupportContactDescription & @CRLF
;~ $sOutput &= "SystemStartupDelay: " & $objItem.SystemStartupDelay & @CRLF
;~ $strSystemStartupOptions = $objItem.SystemStartupOptions(0)
;~ $sOutput &= "SystemStartupOptions: " & $strSystemStartupOptions & @CRLF
;~ $sOutput &= "SystemStartupSetting: " & $objItem.SystemStartupSetting & @CRLF
$sOutput &= "SystemType: " & $objItem.SystemType & @CRLF
$sOutput &= "ThermalState: " & $objItem.ThermalState & @CRLF
$sOutput &= "TotalPhysicalMemory: " & $objItem.TotalPhysicalMemory & @CRLF
;~ $sOutput &= "UserName: " & $objItem.UserName & @CRLF
;~ $sOutput &= "WakeUpType: " & $objItem.WakeUpType & @CRLF
;~ $sOutput &= "Workgroup: " & $objItem.Workgroup & @CRLF
Next
Else
Return ("WMI not functional")
EndIf
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystemProduct", "WQL", _
$wbemFlagReturnImmediately + $wbemFlagForwardOnly)
If IsObj($colItems) Then
For $objItem In $colItems
MsgBox(0,"","SerialNumber" & $objItem.IdentifyingNumber)
$sOutput &= "SerialNumber: " & $objItem.IdentifyingNumber & @CRLF
Next
Return ($sOutput)
EndIf
EndFunc ;==>_wmigetcompinfo
Func WMIDateStringToDate($dtmDate)
Return (StringMid($dtmDate, 5, 2) & "/" & _
StringMid($dtmDate, 7, 2) & "/" & StringLeft($dtmDate, 4) _
& " " & StringMid($dtmDate, 9, 2) & ":" & StringMid($dtmDate, 11, 2) & ":" & StringMid($dtmDate, 13, 2))
EndFunc ;==>WMIDateStringToDate
Func OKButton($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject, $s_Body)
$rc = _INetSmtpMailCom($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject, $s_Body)
If @error Then
MsgBox(0, "Error sending message", "Error code:" & @error & " Description:" & $rc)
Else
MsgBox(0, "HelpDesk App", "Your ticket has been submitted successfully")
;~ else
EndIf
EndFunc ;==>OKButton
Func OnEmail($sSupportEmail)
Run(@ComSpec & " /c " & 'start mailto:' & $sSupportEmail, "", @SW_HIDE)
EndFunc ;==>OnEmail
Func CLOSEClicked()
;Note: at this point @GUI_CTRLID would equal $GUI_EVENT_CLOSE,
;and @GUI_WINHANDLE would equal $hMainwindow
Exit
EndFunc ;==>CLOSEClicked
;
; The UDF
Func _INetSmtpMailCom($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject = "", $sBody = "", $s_AttachFiles = "", $s_CcAddress = "", $s_BccAddress = "", $s_Importance = "Normal", $s_Username = "", $s_Password = "", $IPPort = 25, $ssl = 0)
Local $objEmail = ObjCreate("CDO.Message")
$objEmail.From = '"' & $s_FromName & '" <' & $s_FromAddress & '>'
$objEmail.To = $s_ToAddress
Local $i_Error = 0
Local $i_Error_desciption = ""
If $s_CcAddress <> "" Then $objEmail.Cc = $s_CcAddress
If $s_BccAddress <> "" Then $objEmail.Bcc = $s_BccAddress
$objEmail.Subject = $s_Subject
If StringInStr($sBody, "<") And StringInStr($sBody, ">") Then
$objEmail.HTMLBody = $sBody
Else
$objEmail.Textbody = $sBody & @CRLF
EndIf
If $s_AttachFiles <> "" Then
Local $S_Files2Attach = StringSplit($s_AttachFiles, ";")
For $x = 1 To $S_Files2Attach[0]
$S_Files2Attach[$x] = _PathFull($S_Files2Attach[$x])
;~ ConsoleWrite('@@ Debug : $S_Files2Attach[$x] = ' & $S_Files2Attach[$x] & @LF & '>Error code: ' & @error & @LF) ;### Debug Console
If FileExists($S_Files2Attach[$x]) Then
ConsoleWrite('+> File attachment added: ' & $S_Files2Attach[$x] & @LF)
$objEmail.AddAttachment($S_Files2Attach[$x])
Else
ConsoleWrite('!> File not found to attach: ' & $S_Files2Attach[$x] & @LF)
SetError(1)
Return 0
EndIf
Next
EndIf
$objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
$objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = $s_SmtpServer
If Number($IPPort) = 0 Then $IPPort = 25
$objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = $IPPort
;Authenticated SMTP
If $s_Username <> "" Then
$objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
$objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = $s_Username
$objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = $s_Password
EndIf
If $ssl Then
$objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
EndIf
;Update settings
$objEmail.Configuration.Fields.Update
; Set Email Importance
Switch $s_Importance
Case "High"
$objEmail.Fields.Item("urn:schemas:mailheader:Importance") = "High"
Case "Normal"
$objEmail.Fields.Item("urn:schemas:mailheader:Importance") = "Normal"
Case "Low"
$objEmail.Fields.Item("urn:schemas:mailheader:Importance") = "Low"
EndSwitch
$objEmail.Fields.Update
; Sent the Message
$objEmail.Send
If @error Then
SetError(2)
Return $goCommReturn[1]
EndIf
$objEmail = ""
EndFunc ;==>_INetSmtpMailCom
;
;
; Com Error Handler
Func CommError()
$HexNumber = Hex($goCommError.number, 8)
$goCommReturn[0] = $HexNumber
$goCommReturn[1] = StringStripWS($goCommError.description, 3)
ConsoleWrite("### COM Error ! Number: " & $HexNumber & " ScriptLine: " & $goCommError.scriptline & " Description:" & $goCommReturn[1] & @LF)
SetError(1); something to check for when this function returns
Return
EndFunc ;==>CommError
I would also suggest looking into a system to create serialized ticket #.
Ideally ticket systems would be tied into a database, so that backreferencing is easy and liability/ownership is easy to prove.