Script Apagado equipos remotos

 

'Este script sirve para apagar una lista de equipos remotos
'listados en un archivo de texto en grupos de dos lineas
'primera linea el nombre del equipo
'segunda linea la ip del equipo

'(c) Juansa 9-10-2008


Function fl_responde_al_ping (StrEquipo)

'aquí intentaremos comprobar que el equipo está conectado
'mediante un ping a su IP.

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\" & strComputer & "rootcimv2")
Set colPingedComputers = objWMIService.ExecQuery _
    ("Select * from Win32_PingStatus Where Address = '" & StrEquipo & "'")

For Each objComputer in colPingedComputers
    If objComputer.StatusCode = 0 Then
        
    fl_responde_al_ping = True
    Else
        
    fl_responde_al_ping = false
   End If
Next

End Function

'cuerpo del script

Const ForReading = 1, ForWriting = 2
Dim TabStop, NewLine
TabStop = Chr(9)
NewLine = Chr(10)


Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFSO2 = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:scriptsjuansa.txt", ForReading)
Set objFile2 = objFSO2.OpenTextFile("C:scriptsLogApagados.txt", ForWriting, True)
Do Until objFile.AtEndOfStream
StrEquipo = objFile.ReadLine
StrIP = objFile.ReadLine

If fl_responde_al_ping(StrIP) then
    Wscript.Echo StrEquipo & " responde. Se intentará realizar el apagado."
    objFile2.WriteLine Date & TabStop & StrIP & TabStop & StrEquipo & TabStop & "APAGADO"

    'establecemos control de errores
    On Error Resume Next

     Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate,(Shutdown)}!\" & StrEquipo & "rootcimv2")
    Set colOperatingSystems = objWMIService.ExecQuery _
("Select * from Win32_OperatingSystem")
    For Each objOperatingSystem in colOperatingSystems
        ObjOperatingSystem.Win32Shutdown(1)
    Next
    if Err.Number <> 0 Then

    'mostramos el error o lo guardamos en el log
    WScript.Echo vbCrLf & vbCrLf & _
                     Err.Number & ": " & Err.Description
    objFile2.WriteLine Date & TabStop & StrIP & StrEquipo & TabStop & "Hay un Error!" & Err.Number & ": " & Err.Description

    'vacíamos el objeto Err
    Err.Clear
    end if 

else
    Wscript.Echo StrEquipo & " NO responde. Se saltará éste equipo del apagado."
    objFile2.WriteLine Date & TabStop & StrIP & TabStop & StrEquipo & TabStop & " NO APAGADO"
end if
Loop
objFile2.Close
objFile.Close
Wscript.Echo "Puedes comprobar el Log en C:scriptsLogApagados.txt"
----------------

Lo que tienen los archivos:

C:scriptsjuansa.txt

juansa
192.168.0.10
ONO
192.168.0.1
Noexiste
192.168.0.11

 

C:scriptsLogApagados

09/10/2008    192.168.0.10    juansa        APAGADO
09/10/2008    192.168.0.1      ONO          APAGADO
09/10/2008    192.168.0.11    Noexiste     NO APAGADO

Este script funciona si el que lo lanza es administrador, de hecho está pensado para lanzarlo un administrador, pero mi amigo Fernando dice que puede usarse http://urpiano.wordpress.com/2007/04/26/vbscript-como-conectar-a-wmi-con-credenciales-alternativas/ para redondearlo.

Por supuesto es mejorable, pero lo publico por sí sirve de ejemplo y ayuda.

2 comentarios sobre “Script Apagado equipos remotos”

Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *