Link to home
Start Free TrialLog in
Avatar of jhyanagi
jhyanagiFlag for United States of America

asked on

Remote Shutdown by script from text file

I need to run a script to shutdown certain computers on my domain, that I exported in list form from AD.

I have the below script from an earlier and closed post here. It however isn't working. I get the following error.

Script: C:\documents and settings\user\desktop\restarts.vbs
Line:8
Char:1
Error:File not found
Code 800A0035
Source: Microsoft VBScript runtime error

Any assistance would be greatly appreciated.
strInputFile = "C:\PCNames.txt"
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set wshShell = WScript.CreateObject ("WScript.shell")
 
Const intForReading = 1
Set objInputFile = objFSO.OpenTextFile(strInputFile, intForReading, False)
While Not objInputFile.AtEndOfStream
      strComputer = objInputFile.ReadLine
      If Ping(strComputer) = True Then
		wshShell.run "shutdown -s -f -t 01 -m \\" & strComputer , 6, True
	  End If
Wend
objInputFile.Close
Set objInputFile = Nothing
 
 
Function Ping(strComputer)
      Dim objShell, boolCode
      Set objShell = CreateObject("WScript.Shell")
      boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
      If boolCode = 0 Then
            Ping = True
      Else
            Ping = False
      End If
End Function

Open in new window

Avatar of DIPRAJ
DIPRAJ
Flag of India image



you can try this...
**********************
@echo off
FOR /F %%i IN (computers.txt) DO call:sd %%i
goto:eof
:sd
ping -n 1 %* >nul || ping -n 4 %* >nul
if not errorlevel 1 shutdown -m \\%* -f -s -t 300 -c "Remote PC Shutdown in progress"

*************************


or  
you can try this out


********************************
 With VBScript and Windows Management Instrumentation (WMI).

Dim oFSO, oTS, sClient, oWindows, oLocator, oConnection, oSys
Dim sUser, sPassword

'set remote credentials
sUser = "Administrator"
sPassword = "password"

'open list of client names
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oTS = oFSO.OpenTextFile("C:\clients.txt")

Do Until oTS.AtEndOfStream
 
 'get next client name
 sClient = oTS.ReadLine
 
 'get WMI locator
 Set oLocator = CreateObject("WbemScripting.SWbemLocator")

 'Connect to remote WMI
 Set oConnection = oLocator.ConnectServer(sClient, _
   "root\cimv2", sUser, sPassword)

  'issue shutdown to OS
 ' 4 = force logoff
 ' 5 = force shutdown
 ' 6 = force rebooot
 ' 12 = force power off
 Set oWindows = oConnection.ExecQuery("Select " & _
   "Name From Win32_OperatingSystem")
 For Each oSys In oWindows
   oSys.Win32ShutDown(5)
 Next

Loop

'close the text file
oTS.Close
WScript.Echo "Successfully done!"



NOTE:
1. First, you need to have a text file named C:\Clients.txt on the machine where the script runs. That file should contain the list of client names you want to shut down.

2. Instead of using a text file, you can modify this script to use ADSI, enabling it to query computer names from Active Directory or an NT domain. or by running 'net view' and write in a text file in C:\Clients.txt

3. However, as you probably don’t want to run this script against every computer on your network (shutting down all of your servers might be bad), the text file provides you with complete control over which computers the script will affect.

4.You can also pass the Username and password. dont hardcode it.



********************

pls revert back.....
ASKER CERTIFIED SOLUTION
Avatar of johnb6767
johnb6767
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Course you need to be logged in with a user that has rights to the domain systems..... Or youd have to parse the -u domain\user -p P@ssw0rd switches....
Avatar of jhyanagi

ASKER

diprajbasu, both scripts gave different line errors.

johnb6767, the PsExec tools wont load correctly. It flashes open a command prompt and closes before I can do anything. I downloaded and extraced the files to C:\PSTools, and have tried running as a domain admin.
Did you navigate to a cmd prompt at c:\pstools? It will flash if you run it from a run dialog....