Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Notifications & System Behavior When Running as a Different User

Status
Not open for further replies.

Phylum

IS-IT--Management
Aug 16, 2004
36
US
Objective: Notify user of system reboot in 30 minutes & 'schedule' reboot via `shutdown -t XX`. If the system is still up 30 minutes later, issue 'shutdown(6)' via: Win32_OperatingSystem

History:
I've got a scheduled task that runs as a different user (call it user X) with elevated privileges. Under very specific circumstances I want the script to:
[ul]
[li]Display some sort of pop-up notification to the current logged on user (e.g.: user A); a traditional msgbox[/li]
[li]Execute a command like 'cmd /c shutdown -t 300 -f P:2:18 -c "my comment"' among others
[/ul]

Currently when the script runs under user X's context:
[ul]
[li]Its incapable of displaying pop-ups (msgbox or hta based) to the current logged on user. Not sure how to overcome this.[/li]
[li]The commands don't always execute successfully. The log shows 'wso.Run "cmd /c shutdown -t 300 -f P:2:18 -c ""my comment""",0,False' ran, but the lines after it don't print in the log suggesting there's something wrong/hung somewhere. (Are the limitations to what can be executed locally under a different user's context?)[/li]
[/ul]

While I am interested in getting pop-ups displayed, its less important than making sure the commands execute successfully.
 
I'm running through a list of 166 machines and so far nearly a dozen seem to have issues running 'shutdown' via a scheduled task that runs as a different user.

Code looks something like this
Code:
...
	Dim f_iRestartDelaySeconds, f_iRestartDelayMinutes
	f_iRestartDelaySeconds = 1800
	f_iRestartDelayMinutes = f_iRestartDelaySeconds / 60
	
	Dim f_curtime, f_reboottime
	f_curtime = Now()
	f_reboottime = DateAdd("n",f_iRestartDelayMinutes,Now())
	
	debuglog vbTab & "Current time is: " & f_curtime
	debuglog vbTab & "Reboot time is : " & f_reboottime

	debugecho "Notifying via 'shutdown' & restarting computer in " & f_iRestartDelayMinutes & " minutes (" & f_iRestartDelaySeconds & " seconds) at " & f_reboottime
	debuglog vbTab & "Shutdown commandline: cmd /c shutdown -r -f -t " & f_iRestartDelaySeconds & " -d P:2:18 -c ""COMPUTER UPDATED!  Your computer was recently updated and will reboot in " & f_iRestartDelayMinutes & " minutes!  Please save your work and prepare for a restart!"""
	On Error Resume Next
	Dim f_Return
	
	wso.Run "cmd /c shutdown -r -f -t " & f_iRestartDelaySeconds & " -d P:2:18 -c ""COMPUTER UPDATED!  Your computer was recently updated and will reboot in " & f_iRestartDelayMinutes & " minutes!  Please save your work and prepare for a restart!""",0,False
	
	if (Err.Number <> 0) Then
		debugecho "Error " & err.number & " issuing shutdown command: " & err.description
	else
		debuglog "Reboot Funtion: Finished Notifying via 'shutdown'"
	end if
	
	On Error GoTo 0
...


The log on the client side shows
...
2/12/2013 2:31:40 AM -- Preparing to Restart Computer...
2/12/2013 2:31:40 AM -- Current time is: 2/12/2013 2:31:40 AM
2/12/2013 2:31:40 AM -- Reboot time is : 2/12/2013 3:01:40 AM
2/12/2013 2:31:40 AM -- Notifying via 'shutdown' & restarting computer in 30 minutes (1800 seconds) at 2/12/2013 3:01:40 AM
2/12/2013 2:31:40 AM -- Shutdown commandline: cmd /c shutdown -r -f -t 1800 -d P:2:18 -c "COMPUTER UPDATED! Your computer was recently updated and will reboot in 30 minutes! Please save your work and prepare for a restart!"

There's nothing else after that in the log - so it never got past executing the command. The lack of a 'USER32' event in the System log confirms shutdown didn't execute.

Short of trying to capture a return code (if any):
Code:
...
	f_Return = wso.Run("cmd /c shutdown -r -f -t " & f_iRestartDelaySeconds & " -d P:2:18 -c ""COMPUTER UPDATED!  Your computer was recently updated and will reboot in " & f_iRestartDelayMinutes & " minutes!  Please save your work and prepare for a restart!""",0,False)
	f_Return = wso.Run("shutdown -r -f -t " & f_iRestartDelaySeconds & " -d P:2:18 -c ""COMPUTER UPDATED!  Your computer was recently updated and will reboot in " & f_iRestartDelayMinutes & " minutes!  Please save your work and prepare for a restart!""",0,False)
	f_Return = wso.Run("c:\windows\system32\shutdown.exe -r -f -t " & f_iRestartDelaySeconds & " -d P:2:18 -c ""COMPUTER UPDATED!  Your computer was recently updated and will reboot in " & f_iRestartDelayMinutes & " minutes!  Please save your work and prepare for a restart!""",0,False)
...

Or using a different method to call [c:\windows\system32\]shutdown[.exe]:
Code:
dim iProcessID, sComputer, oWMIService
sComputer = "."
Set oWMIService = GetObject("winmgmts:\\" & sComputer & "\root\cimv2:Win32_Process")
oWMIService.Create "my crazy command here", null, null, iProcessID


I don't think I know what the next step would be.
 
Why are you executing "debugecho" instead of "debuglog" if an error occurs? what does "debugecho" do?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top