Re: HELP! Shutdown / ping script
- From: Ian <ian@xxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 15 Jun 2005 17:01:33 +0100
Hi,
This is the way I found that worked for me.
On Error Resume Next
Dim strFilename
strFilename = "c:\file.txt"
Dim objFSO, objTS, strComputer
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTS = objFSO.OpenTextFile(strFilename)
Do Until objTS.AtEndOfStream
strComputer = objTS.ReadLine
strComputer = UCase(strComputer)
Set colItems = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery("SELECT * FROM Win32_PingStatus where address = '" & strComputer & "'")
For Each objItem in colItems
if objItem.StatusCode = 0 Then
PC= "ON"
Else
PC = "OFF"
End if
Next
If PC = "ON" then
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem")
For Each objItem in colItems
strdomuser = ""&objItem.UserName
dpos = InStr(strdomuser,"\")
strlen = Len(strdomuser)
strdomuser = Right(strdomuser,strlen-dpos)
strdomuser = UCase(strdomuser)
if strdomuser ="" Then
Set wshshl = WScript.CreateObject("wscript.Shell")
' PSShutdown.exe is available from SYSInternals.com FREE!!!!!
' if you want to close apps remotley use PSKILL.EXE here
strrunme = "cmd /c psshutdown -f -k \\" &strComputer
wshshl.run strrunme,0
end if
Next
End If
Loop
objTS.Close
On Tue, 14 Jun 2005 11:49:01 -0700, "magician" <magician@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
>Hey all!
>
>Can anyone help at all? I am doing my head in! Can't quite get the hang of
>all this scripting mallarkey.
>
>Here's what I'm currently doing:
>
>I need to shutdown all my PC's overnight. At the moment I have a list taken
>from the DHCP scope of all the PC's on the network. I've made that list into
>one huge batch file that uses PSShutdown to shutdown/restart EACH address in
>the list regardless of whether it is switched on or off. This takes ages (so
>we split it between 4 servers and they each do about 500 addresses) :)
>
>Here's what I'm trying to do:
>
>I want to continue using PSShutdown for now. I want to ping all the PC's in
>the DHCP scope and create a text file of all those that reply. I then want to
>run PSShutdown against the "active" PC's.
>
>I have the first bit sorted and am getting a list of "active" PC's using this:
>
>'--------------------8<----------------------
>
>Const ForReading = 1
>Const OpenAsASCII = 0
>Const FailIfNotExist = 0
>Const OverwriteIfExist = -1
>
>Set objShell = Wscript.CreateObject("Wscript.Shell")
>Set objFSO = CreateObject("Scripting.FileSystemObject")
>
>Set objFile = objFSO.OpenTextFile("d:\ComputerList.txt", _
> ForReading, FailIfNotExist, OpenAsASCII)
>
>Set objOutputFile = objFSO.CreateTextFile _
> ("d:\PingLog.txt", OverwriteIfExist, OpenAsASCII)
>
>Do Until objFile.AtEndOfStream
>
> strComputer = Trim(objFile.ReadLine)
> If strComputer <> "" Then
> If IsConnectible(strComputer, 1, "") Then
> objOutputFile.WriteLine strComputer
> End If
> End If
>Loop
>
>objFile.Close
>objOutputFile.Close
>
>
>Function IsConnectible(sHost, iPings, iTO)
> ' Returns True or False based on the output from ping.exe
> '
> ' Author: Alex Angelopoulos/Torgeir Bakken
> ' Works an "all" WSH versions
> ' sHost is a hostname or IP
> ' iPings is number of ping attempts
> ' iTO is timeout in milliseconds
> ' if values are set to "", then defaults below used
>
> Const OpenAsASCII = 0
> Const FailIfNotExist = 0
> Const ForReading = 1
> Dim oShell, oFSO, sTempFile, fFile
>
> If iPings = "" Then iPings = 2
> If iTO = "" Then iTO = 750
>
> Set oShell = CreateObject("WScript.Shell")
> Set oFSO = CreateObject("Scripting.FileSystemObject")
>
> sTempFile = oFSO.GetSpecialFolder(2).ShortPath & "\" & oFSO.GetTempName
>
> oShell.Run "%comspec% /c ping.exe -n " & iPings & " -w " & iTO _
> & " " & sHost & ">" & sTempFile, 0 , True
>
> Set fFile = oFSO.OpenTextFile(sTempFile, ForReading, _
> FailIfNotExist, OpenAsASCII)
>
> sResults = fFile.ReadAll
> fFile.Close
> oFSO.DeleteFile(sTempFile)
> IsConnectible = CBool(InStr(sResults, "TTL="))
>
>End Function
>
>'--------------------8<----------------------
>
>(Thanks Torgeir/Alex! :))
>
>I just can't figure out how to get PSShutdown to run against pinglog.txt!
>
>The command line for PSShutdown looks something like this:
>psshutdown -f -r -n 3 \\192.168.0.1
>
>At the moment I am trying to do this in a second script. My idea is to call
>the second script from the first one. I guess it is better to do it all in
>one!? Either way I am completely lost for now.
>
>Any help would be greatly appreciated.
>
>Many tia
>Tom
.
- Follow-Ups:
- Re: HELP! Shutdown / ping script
- From: magician
- Re: HELP! Shutdown / ping script
- References:
- HELP! Shutdown / ping script
- From: magician
- HELP! Shutdown / ping script
- Prev by Date: Re: calling a user login script from a global login script
- Next by Date: Re: HELP! Shutdown / ping script
- Previous by thread: HELP! Shutdown / ping script
- Next by thread: Re: HELP! Shutdown / ping script
- Index(es):
Relevant Pages
|