Jump to content

Recommended Posts

Posted

Hi,

 

I have this script that doesn't seem to work quite right. It does the following

 

  • If within an ip range is quits
  • If no user is logged on shutdown the computer
  • If someone is logged on reboot the computer

 

 

When the computer is logged off the computer is rebooting. I am running this as a scheduled task.

 

Any ideas please?

 

Thanks

 

Set objNetwork = CreateObject("Wscript.Network")
set objShell = CreateObject("WScript.Shell") 

dim NIC1, Nic, StrIP, CompName
Set NIC1 = GetObject("winmgmts:").InstancesOf("Win32_NetworkAdapterConfiguration")
For Each Nic in NIC1
	if Nic.IPEnabled then
	StrIP = Nic.IPAddress(i)
	Set WshNetwork = WScript.CreateObject("WScript.Network")
		If ip2num(StrIP) >= ip2num("10.115.197.1") And ip2num(StrIP) <= ip2num("10.115.197.254") Then
		''ipaddress in range
		wscript.quit
		End If
	end if
next

if objNetwork.UserName = NULL then
			''No User logged on Force shutdown
			strCmd = "shutdown -s -t 300 -f -c ""This computer is performing a scheduled shutdown. Please save all your work. PRESSING CLOSE WILL NOT ABORT SHUTDOWN"""
			objShell.Run strCmd 
			else
			''user logged in
			strCmd = "shutdown -r -t 300 -f -c ""This computer is performing a scheduled shutdown. Please save all your work. PRESSING CLOSE WILL NOT ABORT SHUTDOWN""" 
			objShell.Run strCmd
end if
Public Function ip2num(ip)
Dim i, a, N
a = Split(ip, ".")
N = CDbl(0)
For i = 0 To UBound(a)
 N = N * 256 + a(i)
Next
ip2num = N
End Function

Posted

Hi the reason this isn't working is because objNetwork.UserName is picking up the user that is running the scheduled task.

 

I have changed it to use WMI to get the current logged in user instead of the user that is running the script, give the below ago and it should work.

 

 

Set objNetwork = CreateObject("Wscript.Network")

set objShell = CreateObject("WScript.Shell")

 

dim NIC1, Nic, StrIP, CompName

Set NIC1 = GetObject("winmgmts:").InstancesOf("Win32_NetworkAdapterConfiguration")

For Each Nic in NIC1

if Nic.IPEnabled then

StrIP = Nic.IPAddress(i)

Set WshNetwork = WScript.CreateObject("WScript.Network")

If ip2num(StrIP) >= ip2num("10.115.197.1") And ip2num(StrIP) <= ip2num("10.115.197.254") Then

''ipaddress in range

wscript.quit

End If

end if

next

 

strUsername = ""

strComputer = "."

 

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")

Set colItems = objWMIService.ExecQuery( _

"SELECT * FROM Win32_ComputerSystem",,48)

For Each objItem in colItems

strUsername = objItem.UserName

Next

 

if objNetwork.UserName = "" then

''No User logged on Force shutdown

strCmd = "shutdown -s -t 300 -f -c ""This computer is performing a scheduled shutdown. Please save all your work. PRESSING CLOSE WILL NOT ABORT SHUTDOWN"""

objShell.Run strCmd

else

''user logged in

strCmd = "shutdown -r -t 300 -f -c ""This computer is performing a scheduled shutdown. Please save all your work. PRESSING CLOSE WILL NOT ABORT SHUTDOWN"""

objShell.Run strCmd

end if

Public Function ip2num(ip)

Dim i, a, N

a = Split(ip, ".")

N = CDbl(0)

For i = 0 To UBound(a)

N = N * 256 + a(i)

Next

ip2num = N

End Function

  • Thanks 1
Posted (edited)

Might be to do with the formatting for quoting the code? It work's fine for me, try again with me quoting it as code this time.

 

Set objNetwork = CreateObject("Wscript.Network")
set objShell = CreateObject("WScript.Shell") 

dim NIC1, Nic, StrIP, CompName
Set NIC1 = GetObject("winmgmts:").InstancesOf("Win32_NetworkAdapterConfiguration")
For Each Nic in NIC1
	if Nic.IPEnabled then
	StrIP = Nic.IPAddress(i)
	Set WshNetwork = WScript.CreateObject("WScript.Network")
		If ip2num(StrIP) >= ip2num("10.115.197.1") And ip2num(StrIP) <= ip2num("10.115.197.254") Then
		''ipaddress in range
		wscript.quit
		End If
	end if
next

strUsername = ""
strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
Set colItems = objWMIService.ExecQuery( _
   "SELECT * FROM Win32_ComputerSystem",,48) 
For Each objItem in colItems 
   strUsername = objItem.UserName
Next

if strUsername = "" then
			''No User logged on Force shutdown
			strCmd = "shutdown -s -t 300 -f -c ""This computer is performing a scheduled shutdown. Please save all your work. PRESSING CLOSE WILL NOT ABORT SHUTDOWN"""
			objShell.Run strCmd 
			else
			''user logged in
			strCmd = "shutdown -r -t 300 -f -c ""This computer is performing a scheduled shutdown. Please save all your work. PRESSING CLOSE WILL NOT ABORT SHUTDOWN""" 
			objShell.Run strCmd
end if
Public Function ip2num(ip)
Dim i, a, N
a = Split(ip, ".")
N = CDbl(0)
For i = 0 To UBound(a)
 N = N * 256 + a(i)
Next
ip2num = N
End Function

Edited by nev104
  • Thanks 1
  • 2 weeks later...
Posted

In the above script the value returned for strUsername would be Null, rather than an empty string, if no user is logged on and in VBScript the empty string "" != Null

 

If you change your test to isNull(strUsername) it should work, e.g.

 

strUsername = ""
strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
Set colItems = objWMIService.ExecQuery( _
   "SELECT * FROM Win32_ComputerSystem",,48) 
For Each objItem in colItems 
   strUsername = objItem.UserName
Next

If isNull(strUsername) Then
               ''No User logged on Force shutdown
               strCmd = "shutdown -s -t 300 -f -c ""This computer is performing a scheduled shutdown. Please save all your work. PRESSING CLOSE WILL NOT ABORT SHUTDOWN"""
               objShell.Run strCmd 
Else
               ''user logged in
               strCmd = "shutdown -r -t 300 -f -c ""This computer is performing a scheduled shutdown. Please save all your work. PRESSING CLOSE WILL NOT ABORT SHUTDOWN""" 
               objShell.Run strCmd
End If

 

Iain.

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



×
×
  • Create New...