Question

I am TRYING (key word) to get a grasp on VBScript, but I am just now getting into it, and I am having issues. I found a script online that I am trying to modify for my use. Ultimately, I would like to query a list of computers to see if a user is logged in. If there is no user logged in, reboot it.

The below script works perfectly fine to check one computer at a time and reboot it. How can I modify this to run on each computer I list in TXT file?

 Dim objShell
 Dim objWMIService, objComputer, colComputer
 Dim strLogonUser, strLogonUser1, strComputer
 Set objShell = WScript.CreateObject ( "WScript.Shell" )

 strComputer = "."
 strComputer = InputBox("Enter Computer name", "Find Logon User", strComputer)
 Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" &_
    strComputer & "\root\cimv2")
 Set colComputer = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")

 For Each objComputer in colComputer
   If (IsNull(objComputer.UserName)) Then
     objShell.Run "psshutdown \\" & strComputer & " -r -t 15"
     WScript.Echo "No users logged on at " & strComputer & ". Computer restarted."
     WScript.Quit
   Else
     strLogonUser = Split(objComputer.UserName,"\")
     strLogonUser(1) = UCase(Left(strLogonUser(1),1)) & Trim(Mid(strLogonUser(1),2,20))
     Wscript.Echo strLogonUser(1) & " is logged on at " & strComputer
   End If
   Set objShell = Nothing
 Next

As always, thanks in advance for any help.

Updated With Aphoria's Suggestion:

Thank you for the suggestion Aphoria, but I am now getting an error "Loop without Do". Any ideas? I have tried a few ideas similar to yours, but I keep getting the Loop without Do error. A Google search shows the most common cause of this is an open "IF", but if I try to add another "End If" it doesn't work either.

EDIT AGAIN: Moved the "NEXT" inside the "Loop", but now I am getting an error on Line 8 Char 1 "Object Required: 'objFile'"

 Dim objShell
 Dim fso, file
 Dim objWMIService, objComputer, colComputer
 Dim strLogonUser, strLogonUser1, strComputer

 Set objFS = CreateObject("Scripting.FileSystemObject")
 strFile = "ComputerList.txt"
 Set objFile = fso.GetFile(strFile)
 Set ts = objFile.OpenAsTextStream(1,-2)
 do until ts.AtEndOfStream
     strComputer = ts.ReadLine
     Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" &_
       strComputer & "\root\cimv2")
     Set colComputer = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")

     For Each objComputer in colComputer
       If (IsNull(objComputer.UserName)) Then
         objShell.Run "psshutdown \\" & strComputer & " -r -c -t 120"
         WScript.Echo "No users logged on at " & strComputer & ". Computer restarted."
         WScript.Quit
       Else
         strLogonUser = Split(objComputer.UserName,"\")
         strLogonUser(1) = UCase(Left(strLogonUser(1),1)) & Trim(Mid(strLogonUser(1),2,20))
         Wscript.Echo strLogonUser(1) & " is logged on at " & strComputer
       End If
   Next
 loop
 ts.Close
Was it helpful?

Solution

This code will open a file and read it a line at a time, storing the current line in a variable called strComputer. You will need to put your rebooting code inside this loop...or, better yet, turn your code into a function that accepts a computer name to reboot.

Set objFS = CreateObject("Scripting.FileSystemObject")
strFile = "computers.txt"
Set objFile = objFS.GetFile(strFile)
Set ts = objFile.OpenAsTextStream(1, -2)
Do Until ts.AtEndOfStream
  strComputer = ts.ReadLine
  ' ----
  ' your code to reboot goes here
  ' ---   
Loop
ts.Close

EDIT

I added your code...this works for me.

Dim objShell
Dim fso, file
Dim objWMIService, objComputer, colComputer
Dim strLogonUser, strLogonUser1, strComputer

Set objFS = CreateObject("Scripting.FileSystemObject")
strFile = "ComputerList.txt"
Set objFile = objFS.GetFile(strFile)
Set ts = objFile.OpenAsTextStream(1, -2)
Do Until ts.AtEndOfStream
  strComputer = ts.ReadLine
  Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & _
      strComputer & "\root\cimv2")
  Set colComputer = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")

  For Each objComputer in colComputer
    If (IsNull(objComputer.UserName)) Then
      objShell.Run "psshutdown \\" & strComputer & " -r -c -t 120"
      WScript.Echo "No users logged on at " & strComputer & ". Computer restarted."
      WScript.Quit
    Else
      strLogonUser = Split(objComputer.UserName,"\")
      strLogonUser(1) = UCase(Left(strLogonUser(1),1)) & Trim(Mid(strLogonUser(1),2,20))
      Wscript.Echo strLogonUser(1) & " is logged on at " & strComputer
    End If
  Next
Loop
ts.Close
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top