Enumerating the If... Then: Scripts

Monday, December 11, 2006

VBScript: Script Shell to read PC names out of file and perform something on them

Not Tested

This example issues a shutdown command.

Syntax whatever.vbs [Filename contain list of PCs]


''''
''''
''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''' Issues shutdown on PCs given in a list
''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''
''''
''''
' 1) Read Remote PC name out of file. One per line, NO \\s!!!!
' 2) pings target machine
' 3) issues shutdown method
''''


Option Explicit



Dim strFilename
Dim objFSO, objOpenFileForRead, objWmiService, objPing, objitem
dim wshArguments
Dim strHostname


Set wshArguments = WScript.Arguments

strFilename = wsharguments(0)




Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2")

Set objFSO = CreateObject("scripting.FileSystemObject")




If objFSO.FileExists(strFileName) then


Set objOpenFileForRead = objFSO.OpenTextFile(strFileName, 1)



Do Until objOpenFileForRead.AtEndOfStream

''' STEP 1
strHostname = objOpenFileForRead.ReadLine
strHostname = Trim(strHostname)
strHostname = UCase(strHostname)


if left(strHostname, 2) = "\\" then
wscript.echo "Please remove any \\s in the text file. Ending."
wscript.quit
end if


''' STEP 2
Set objPing = objWMIService.ExecQuery("SELECT * FROM Win32_PingStatus WHERE Address = '" & strhostname & "'")



for each objItem in objPing


If objitem.statuscode = 0 then 'ping response

'''''''
'' START doing stuff to the PC (strHostName) here
'''''''


' Issue shutdown command upon strhostname
''' for quickness objshell will allow you execute a shell command
' dim objShell
' set objShell = CreateObject("wscript.shell")
' objShell.run "notepad.exe"

''' STEP 3

dim objShutdown

Set objWMIService = GetObject("winmgmts:\\"& strhostname & "\root\CIMV2")

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

For Each objItem in objShutdown
objItem.Shutdown(1)
Next

'''''
'' STOP doing stuff to the PC (strHostName) here
'''''

end if


Next


Else

MsgBox "Could not find file " & strFileName & "." & vbnewline & "This needs to be the file with the target PC names."

wscript.quit

End If


wscript.echo "Finished shutdown task."

0 Comments:

Post a Comment

<< Home