Sign in to follow this  
Followers 0
Chronodark

How to create error message

2 posts in this topic

Hello,

I have a script that allows you to type in a computer name and then write to certain paths in HKCU or HKLM on that computer. The problem I am having is that if a computer isnt found or the path on the computer doesnt exist the script gives an error after one of the buttons is clicked. Any ideas on how I can create an error message instead of recieving the script error (which appears on this line when it does occur: $colProcesses = $objWMIService.ExecQuery("Select Name from Win32_Process Where Name = 'Explorer.exe' AND SessionId = 0") )

code is listed below, Thanks!

#include <GUIConstants.au3>
#include <EzSkin.au3>

$EzGUI = EzSkinGUICreate("MyEzSkinDemo",400,400)
$EzIcon = EzSkinIcon($EzGUI)




$Computername = InputBox("Remote Prep tool", "What is the Computer name?", "Type PC Name Here", "", _
    -1, -1, 0, 0)

Func _GetConsoleUser($strComputer = $Computername)
    Local $objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $strComputer & "\Root\CIMV2")
    Local $colProcesses, $objProcess
    Local $strUser = "", $strDomain = ""
    Local $strAccount, $lReturn

    $colProcesses = $objWMIService.ExecQuery("Select Name from Win32_Process Where Name = 'Explorer.exe' AND SessionId = 0")
    For $objProcess In $colProcesses
        $lReturn = $objProcess.GetOwner($strUser, $strDomain)
        If $lReturn = 0 Then
            $strAccount = $strUser
        EndIf
    Next

    If $strAccount <> "" Then
        Return $strAccount
    
    Else
        Return SetError(1, 0, 0)
    EndIf
EndFunc 

Func _UserSID()
    Local $iStart = 0, $sVal
    Local $sReg = ("\\" & $Computername & "\HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList")
    While 1
        $iStart += 1
        $sKey = RegEnumKey($sReg, $iStart)
        If @Error Then ExitLoop
        If StringMid($sKey, 7, 2) <> 21 Then ContinueLoop
        $sVal = RegRead($sReg & "\" & $sKey, "ProfileImagePath")
        If StringRegExpReplace($sVal, "^.+\\(.+)$", "$1") = _GetConsoleUser() Then Return $sKey
    WEnd
    Return SetError(1, 0, "No Match")
EndFunc



$Button = EzSkinButton("Enable Auto Local Logon", 100, 100, 200, 30) ;, $font_color)
$Button2 = EzSkinButton("Disable Auto Login for VPN", 100, 200, 200, 30) ;, $font_color)
$Button3 = EzSkinButton("AllowWindow Reuse", 100, 300, 200, 30) ;, $font_color)
$Button4 = EzSkinButton("Info", 300, 300, 50, 30) ;, $font_color)
GUISetState()

While 1
    EzSkinOver()
    $msg = GUIGetMsg()
    If $msg = $EzIcon[1] Then Exit
    If $msg = $EzIcon[2] Then GuiSetstate(@SW_MINIMIZE, $EzGUI)
    If $msg = $Button Then RegWrite("\\" & $Computername & "\HKEY_USERS\" & _UserSID() & "\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\1", "1A00", "REG_DWORD", 0)
    If $msg = $Button Then RegWrite("\\" & $Computername & "\HKEY_USERS\" & _UserSID() & "\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\2", "1A00", "REG_DWORD", 0)
    If $msg = $Button Then MsgBox(4096, "Operation Complete on PC", "Operation is complete on: " & $Computername)
    If $msg = $Button2 Then RegWrite("\\" & $Computername & "\HKEY_LOCAL_MACHINE\SOFTWARE\CHECKPOINT\SecuRemote", "SSOEnabled", "REG_DWORD", 0)
    If $msg = $Button2 Then MsgBox(4096, "Complete", "Automatic Login has been disabled")
    If $msg = $Button3 Then RegWrite("\\" & $Computername & "\HKEY_USERS\" & _UserSID() & "\Software\Microsoft\Internet Explorer\Main", "AllowWindowReuse", "REG_DWORD", 1)
    If $msg = $Button3 Then MsgBox(4096, "Operation Complete on PC", "Allow Window Reuse is complete on: " & $Computername)
    If $msg = $Button4 Then Msgbox(4096, "Info", "This button will allow users to open up multiple windows in internet explorer instead of just reusing the same one, it is the equivalent of going to Tools-->Advanced Options-->Reuse Windows for launching shortcuts")

WEnd

#endregion

Share this post


Link to post
Share on other sites



Put this into your script and modify accordingly:

#Region ; **** Error Handler ****
Global $oMyError = ObjEvent("AutoIt.Error", "_ErrFunc")
Func _ErrFunc()
    ConsoleWrite(@LF &  "=============================================" & @LF & _
                        "! AutoItCOM Test: We intercepted a COM error!"       & @LF & @TAB & _
                        "description   : "   & $oMyError.description          & @LF & @TAB & _
                        "windescription: "   & $oMyError.windescription       & @LF & @TAB & _
                        "number     : 0x" & Hex($oMyError.number, 8)      & @LF & @TAB & _
                        "lastdllerror  : "   & $oMyError.lastdllerror       & @LF & @TAB & _
                        "scriptline : ("  & $oMyError.scriptline    & ')' & @LF & @TAB & _
                        "source     : "   & $oMyError.source              & @LF & @TAB & _
                        "helpfile   : "   & $oMyError.helpfile            & @LF & @TAB & _
                        "helpcontext   : "   & $oMyError.helpcontext          & @LF & _
                        "=============================================" & @LF)
EndFunc
#EndRegion ; **** Error Handler ****

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