I've written a script that runs via a Scheduled Task under a user account that has full local admin privileges on all workstations and sufficient access in AD. When I initially tested the script, I ran it as a normal user account that also had full local admin privileges but very limited AD access, yet it ran fine. (The script doesn't rely on or access AD at all actually.) However I've found that when its run via the Scheduled Task it doesn't always complete successfully and I don't know why.
I suspect it has to do with the state the machine is in. I want it to be able to run regardless of the system state:
[ul]
[li]a user logged on locally & actively working[/li]
[li]a user logged on locally but screen locked[/li]
[li]a user logged on remotely & actively working[/li]
[li]a user logged on remotely but screen locked[/li]
[li]at the login screen with no user logged on[/li]
[/ul]
What am I trying to accomplish? Its actually very simple really: to perform a specific type of reboot depending on whether or not the system is being used. My thought process is as follows:
[ul]Script checks for local users:
[li]If found: Run shutdown with the appropriate switches to give them a warning.[/li]
[li]If not found: Check for remote users.[/li]
[ul]
[li]If found: Run shutdown with the appropriate switches to give them a warning.[/li]
[li]If not found: Shutdown the workstation immediately.[/li]
[/ul]
[/ul]
Most of the machines stalled on the local user check, never making it to the remote user check.
On others it got past the local & remote user checks (not sure why) but never rebooted.
So I'm mainly wondering if there are restrictions as to what WMI functions are not exposed or are otherwise not available depending on the state of the machine or who is trying to access it.
Code:
This code I'm using is a mash-up of code acquired from various sources. and while it works when run manually or run under the current user's context, I've seen inconsistent results when run via Scheduled Task.
I suspect it has to do with the state the machine is in. I want it to be able to run regardless of the system state:
[ul]
[li]a user logged on locally & actively working[/li]
[li]a user logged on locally but screen locked[/li]
[li]a user logged on remotely & actively working[/li]
[li]a user logged on remotely but screen locked[/li]
[li]at the login screen with no user logged on[/li]
[/ul]
What am I trying to accomplish? Its actually very simple really: to perform a specific type of reboot depending on whether or not the system is being used. My thought process is as follows:
[ul]Script checks for local users:
[li]If found: Run shutdown with the appropriate switches to give them a warning.[/li]
[li]If not found: Check for remote users.[/li]
[ul]
[li]If found: Run shutdown with the appropriate switches to give them a warning.[/li]
[li]If not found: Shutdown the workstation immediately.[/li]
[/ul]
[/ul]
Most of the machines stalled on the local user check, never making it to the remote user check.
On others it got past the local & remote user checks (not sure why) but never rebooted.
So I'm mainly wondering if there are restrictions as to what WMI functions are not exposed or are otherwise not available depending on the state of the machine or who is trying to access it.
Code:
This code I'm using is a mash-up of code acquired from various sources. and while it works when run manually or run under the current user's context, I've seen inconsistent results when run via Scheduled Task.
Code:
Function UpdateReboot
If (DetectLocalLoggedOnUser) or (DetectRemoteLoggedOnUser) Then
Dim f_iRestartDelaySeconds, f_iRestartDelayMinutes
f_iRestartDelaySeconds = 900
f_iRestartDelayMinutes = f_iRestartDelaySeconds / 60
wscript.echo "User Detected - Notifying user via 'shutdown' & restarting computer in " & f_iRestartDelayMinutes & " minutes (" & f_iRestartDelaySeconds & " seconds)."
wso.Run "cmd /c shutdown -r -f -t " & f_iRestartDelaySeconds & " -c ""COMPUTER UPDATED! Your computer was recently updated & will reboot in " & f_iRestartDelayMinutes & " minutes! Please save your work & prepare for a restart!"""
Else
wscript.echo "No User(s) Detected - Forcing Immediate Shoutdown"
Dim f_oWMIService, f_colOperatingSystem, f_oOperatingSystem
Set f_oWMIService = GetObject("winmgmts:{impersonationLevel=impersonate, (RemoteShutdown)}!\\.\root\cimv2")
Set f_colOperatingSystem = f_oWMIService.ExecQuery("Select * from Win32_OperatingSystem")
'Set f_colOperatingSystem = f_oWMIService.ExecQuery("Select * from Win32_OperatingSystem where Primary=true")
For Each f_oOperatingSystem in f_colOperatingSystem
'Value Meaning
'0 (0x0) Log Off
'4 (0x4) Forced Log Off (0 + 4)
'1 (0x1) Shutdown
'5 (0x5) Forced Shutdown (1 + 4)
'2 (0x2) Reboot
'6 (0x6) Forced Reboot (2 + 4)
'8 (0x8) Power Off
'12 (0xC) Forced Power Off (8 + 4)
f_oOperatingSystem.Win32Shutdown(6)
'f_oOperatingSystem.Reboot()
Next
End If
End Function
Function DetectLocalLoggedOnUser
Dim f_oWMIService, f_colComputer, f_oComputer, f_sLoggedOn, f_sUserName
'Get currently logged on user's username (local)
Set f_oWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set f_colComputer = f_oWMIService.ExecQuery("Select * from Win32_ComputerSystem")
For Each f_oComputer in f_colComputer
If IsNull(f_oComputer.UserName) Then 'If no one is logged On
f_sLoggedOn=False
f_sUserName=f_oComputer.Username
Else
f_sLoggedOn=True
f_sUserName=f_oComputer.Username
End If
Next
DetectLocalLoggedOnUser=f_sLoggedOn
End Function
Function DetectRemoteLoggedOnUser
Dim f_oWMIService, f_colSessions, f_oSession, f_sLoggedOn, f_sUserName
Set f_oWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set f_colSessions = f_oWMIService.ExecQuery("Select * from Win32_LogonSession Where LogonType = 10")
If (f_colSessions.Count = 0) Then
f_sLoggedOn=False
Else
If (f_colSessions.Count = 1) Then debuglog f_colSessions.Count & "Remote User Detected!"
If (f_colSessions.Count > 1) Then debuglog f_colSessions.Count & "Remote Users Detected!"
Dim f_colList, f_oItem
For Each f_oSession in f_colSessions
Set f_colList = f_oWMIService.ExecQuery("Associators of {Win32_LogonSession.LogonId=" & f_oSession.LogonId & "} Where AssocClass=Win32_LoggedOnUser Role=Dependent" )
for each f_oItem In f_colList
f_sLoggedOn=True
next
Next
End If
DetectRemoteLoggedOnUser = f_sLoggedOn
End Function