Monday, April 4, 2011

VB Script to Reboot Computers from a text file

The VB Script below reboots machines in C:\temp\computers.txt

' Read Arguments from a Text File

Const ForReading = 1
Set objDictionary = CreateObject("Scripting.Dictionary")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("c:\temp\computers.txt", ForReading)
i = 0
WScript.Echo "Reboot Computers in c:\temp\computers.txt"
Do Until objTextFile.AtEndOfStream
    strNextLine = objTextFile.Readline
    objDictionary.Add i, strNextLine
    i = i + 1
Loop

For Each objItem in objDictionary
    Set objFSO = CreateObject("Scripting.FileSystemObject")
  If objFSO.FolderExists("\\"& objDictionary.Item(objItem) & "\Admin$") Then
      Set objFolder = objFSO.GetFolder("\\" & objDictionary.Item(objItem) & "\Admin$")
      'Reboot Desktops
      'strComputer = objDictionary.Item(objItem)
      WScript.Echo objDictionary.Item(objItem)
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate,(Shutdown)}!\\" & _
           objDictionary.Item(objItem) & "\root\cimv2")

    Set colOperatingSystems = objWMIService.ExecQuery _
        ("Select * from Win32_OperatingSystem")

    For Each objOperatingSystem in colOperatingSystems
        objOperatingSystem.Reboot()
    Next
    
      Else
      WScript.Echo "Can't Connect to " & objDictionary.Item(objItem) & chr(10) & chr(13) & "Please verify it is contactable via DNS and you have Admin rights"
      strCantConnect = "1"
  End If
Next
If strCantConnect = "1" Then
WScript.Echo "Script Finished!"
Else WScript.Echo "All Desktops have been Rebooted!"
End if

No comments:

Post a Comment