Jump to content

Run command and Error handling


Recommended Posts

OK, so maybe I am a complete DA, but I can't seem to figure out how to capture an error output for a run command that actually "runs", but spits out an error at the DOS console. I have written a script to facilitate scheduling reboots on remote servers by the server "owner" that is a local admin on the server. But if they try to schedule it on a server that they are not an admin on, the script currently doesn't error out in any way, and appears to work to the end user (scheduler). Can anyone help me out here and show me what I can do to capture the "Access is denied." error that would display on the DOS console, then use that to let the user know that the job did not get scheduled...

Thanks in advance...code is below...

#Include <Date.au3>

#Include <GUICombo.au3>

#Include <GUIConstants.au3>

#Include <Process.au3>

Global $GUI

Global $SHELL32DLL = @SystemDir & "\SHELL32.dll"

Global $gciHostNames, $gccDay, $gccHour, $gccMinute, $gcbApply, $gceSummary, $gcbSubmit, $gcbExit

Global $HostName, $val, $LogFile

#NoTrayIcon

Opt('TrayIconDebug', 0); 0=no info, 1=debug line info

Opt('GUICloseOnESC', 0); 1 = ESC closes / 0 = ESC won't close

;Opt('TrayMenuMode', 1); 0=append, 1=no default menu, 2=no automatic check, 4=menuitemID not return

Opt('TrayOnEventMode', 1); 0=disable, 1=enable

Opt('RunErrorsFatal', 0); 1=fatal, 0=silent set @error to 1

;********************************************************************************

;* Script body *

;********************************************************************************

$GUI = GUICreate('Reboot Scheduler v2.0', 265, 410)

GUISetIcon($SHELL32DLL, 27, $GUI)

GUICtrlCreateGroup('Step 1:', 10, 10, 245, 225)

GUICtrlCreateLabel('Host Name(s):' & @CRLF & '(comma seperated)', 20, 28, 70, 120)

$gciHostNames = GUICtrlCreateInput('', 90, 25, 150, 120, BitOr($WS_VSCROLL, $ES_UPPERCASE, $ES_MULTILINE))

GUICtrlCreateLabel('Day of the Week:', 20, 153, 90, 20)

$gccDay = GUICtrlCreateCombo('Please Select', 110, 150, 130, 20, $CBS_DROPDOWNLIST)

GUICtrlSetData($gccDay, 'Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday')

GUICtrlCreateLabel('Time of Day to Schedule:', 20, 178, 130, 20)

$gccHour = GUICtrlCreateCombo('', 150, 175, 40, 20, $CBS_DROPDOWNLIST)

GUICtrlSetData($gccHour, '00|01|02|03|04|05|06|07|08|09|10|11|12|13|14|15|16|17|18|19|20|21|22|23')

$gccMinute = GUICtrlCreateCombo('', 200, 175, 40, 20, BitOR($CBS_DROPDOWNLIST, $WS_VSCROLL))

GUICtrlSetData($gccMinute, '00|01|02|03|04|05|06|07|08|09|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|

32|33|34|35|36|37|38|39|40|41|42|43|44|45|46|47|48|49|50|51|52|53|54|55|56|57|58|59')

$gcbApply = GUICtrlCreateButton('Apply', 97, 205, 60, 20)

GUICtrlSetTip($gcbApply, 'Press to update the summary of the reboot task that you would like to submit.')

GUICtrlCreateGroup('Step 2:', 10, 245, 245, 125)

$gceSummary = GUICtrlCreateEdit('Please enter the server name(s) and select the day of the week and the time of the day for the scheduled reboot to occur.', 20, 260, 225, 70, $ES_READONLY)

$gcbSubmit = GUICtrlCreateButton('Submit', 97, 340, 60, 20)

GUICtrlSetTip($gcbSubmit, 'Press to submit the scheduled task to the server.')

$gcbExit = GUICtrlCreateButton('Exit', 97, 380, 60, 20)

GUICtrlSetTip($gcbExit, 'Press to exit the application.')

GUISetState (@SW_SHOW, $GUI)

While 1

Switch GUIGetMsg()

Case $GUI_EVENT_CLOSE

GUIDelete($GUI)

Exit

Case $gcbApply

$HostName = StringSplit(StringStripWS(GUICtrlRead($gciHostnames), 8), ',')

For $count = 1 to $HostName[0] Step 1

Ping($HostName[$count], 1000)

If @error = 0 Then

If GUICtrlRead($gciHostNames) = '' Then

GUICtrlSetData($gceSummary, 'Please type in the name of the server(s)!')

$val = 0

ElseIf GUICtrlRead($gccDay) = 'Please Select' Then

GUICtrlSetData($gceSummary, 'Please select a day of the week to schedule the reboot on!')

$val = 0

ElseIf GUICtrlRead($gccHour) = '' Or GUICtrlRead($gccMinute) = '' Then

GUICtrlSetData($gceSummary, 'Please select the time of day that the server(s) will reboot!')

$val = 0

Else

GUICtrlSetData($gceSummary, 'The task that you have prepared to submit will reboot ' & _

'the server(s) listed above every ' & GUICtrlRead($gccDay) & ' at ' & GUICtrlRead($gccHour) & _

':' & GUICtrlRead($gccMinute) & '. Please press the "Submit" button below to submit the task to the server.')

$val = 1

EndIf

Else

GUICtrlSetData($gceSummary, 'The server (' & $HostName[$count] & ') is not available! Please remove it from the above list.')

$val = 0

EndIf

Next

Case $gcbSubmit

If $val = 0 Then

GUICtrlSetData($gceSummary, 'You must complete Step 1 properly before you can submit the task to the server(s)!')

Else

$HostName = StringSplit(StringStripWS(GUICtrlRead($gciHostnames), 8), ',')

For $count = 1 to $HostName[0] Step 1

Run('at \\' & $HostName[$count] & ' ' & GUICtrlRead($gccHour) & ':' & GUICtrlRead($gccMinute) & _

' /every:' & GUICtrlRead($gccDay) & ' shutdown /r /m \\' & $HostName[$count] & _

' /t 0 /c "Scheduled weekly reboot task submitted by ' & @UserName & '" /f /d p:4:1', @SystemDir, @SW_HIDE)

$LogFile = FileOpen('\\DALMON7\RebootScheduler$\logfile.txt', 1)

FileWriteLine($LogFile, @UserName & ',' & 'Submitted a scheduled reboot' & ',' & $HostName[$count] & ',' & _

'Every ' & GUICtrlRead($gccDay) & ' at ' & GUICtrlRead($gccHour) & ':' & GUICtrlRead($gccMinute) & ',' & _

@ComputerName & ',' & _Now() & @CRLF)

FileClose($LogFile)

GUICtrlSetData($gceSummary, 'The task has been successfully submitted to ' & $HostName[$count] & ', as you requested!')

Sleep(2000)

Next

EndIf

GUICtrlSetData($gceSummary, 'The task has been successfully submitted to all of the servers you requested!')

Case $gcbExit

GUIDelete($GUI)

Exit

EndSwitch

WEnd

Link to comment
Share on other sites

Run('at \\' & $HostName[$count] & ' ' & GUICtrlRead($gccHour) & ':' & GUICtrlRead($gccMinute) & _
                        ' /every:' & GUICtrlRead($gccDay) & ' shutdown /r /m \\' & $HostName[$count] & _
                        ' /t 0 /c "Scheduled weekly reboot task submitted by ' & @UserName & '" /f /d p:4:1', @SystemDir, @SW_HIDE)
Attach a handle to the output streams with the $STDOUT_CHILD and $STDERR_CHILD flags of the Run() function. Then read the output with StdOutRead() and StdErrRead. One simplifying option is to include the redirection parameter in the DOS command line so ERROUT is redirected to STDOUT, then you only have to monitor the one stream. The option is '2>&1' on the DOS command line.

:shocked:

P.S. SchTasks.exe might be another way to set up your scheduled tasks, and more widely available across the newer versions of Windows.

:(

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Attach a handle to the output streams with the $STDOUT_CHILD and $STDERR_CHILD flags of the Run() function. Then read the output with StdOutRead() and StdErrRead. One simplifying option is to include the redirection parameter in the DOS command line so ERROUT is redirected to STDOUT, then you only have to monitor the one stream. The option is '2>&1' on the DOS command line.

:shocked:

P.S. SchTasks.exe might be another way to set up your scheduled tasks, and more widely available across the newer versions of Windows.

:(

That did the trick...Thanks!

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...