FN-GM Posted November 23, 2014 Posted November 23, 2014 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
nev104 Posted November 26, 2014 Posted November 26, 2014 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 1
FN-GM Posted November 26, 2014 Author Posted November 26, 2014 Thanks very much, I will give that a go.
FN-GM Posted November 26, 2014 Author Posted November 26, 2014 Hi, Just testing this it seems to have an issue at Line 6 Character 1. Any thoughts please? Thanks
nev104 Posted November 27, 2014 Posted November 27, 2014 (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 November 27, 2014 by nev104 1
FN-GM Posted November 27, 2014 Author Posted November 27, 2014 No more errors. But its just rebooting when the computer is logged off.
nev104 Posted November 27, 2014 Posted November 27, 2014 Sorry i had made a mistake, change line 27 to if strUsername = "" then
FN-GM Posted November 27, 2014 Author Posted November 27, 2014 Done that and still the same Sorry to be a pain
FN-GM Posted December 10, 2014 Author Posted December 10, 2014 @nev104 I was wondering if you still help please? Thanks
Iain Posted December 10, 2014 Posted December 10, 2014 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.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now