VBScript & WMI

Soldato
Joined
16 May 2008
Posts
2,579
Location
Bristol
Hi,

I'm writing some batch files, one of which starts an application on a list of IPs.

Currently I am using this workaround:

set h=%TIME:~0,2%
set m=%TIME:~3,2%
set s=%TIME:~6,2%

IF [%m%]==[] SET /a m=02 GOTO settime
IF %m%==58 SET /a m=00 GOTO settime
IF %m%==59 SET /a m=01 GOTO settime
IF %m%==60 SET /a m=02 GOTO settime
set /a m=(m+2)

:settime
set time2=%h%:%m%:%s%

FOR /f %%f in (ips.txt) do (
start schtasks /create /tn "run this program" /ru Administrator /rp Password /tr C:\program.exe /s \\%%f /sc once /sd %date% /st %time2%
)

This schedules a task to run the program 2 minutes in the future and is quite lame. What I would like is a VBS solution which eliminates the 2minute delay but have no experience in VB script..

I found this:

strComputer = InputBox("What computer or IP do you want to use?")
strProg = InputBox("What Program do you want to start?")

Set objWMIService = GetObject _
("winmgmts:\\" & strComputer & "\root\cimv2:Win32_Process")

Error = objWMIService.Create(strProg, null, null, intProcessID)

If Error = 0 Then
Wscript.Echo "Your application was started with a process ID of " _
& intProcessID & "."
Else
Wscript.Echo "Your application could not be started due to error " & _
Error & "."
End If

But it does not account for login credentials. Please could someone advise how to add this functionality?
 
Hi,

As part of a much larger batch file I need to run an .exe file on a list of IPs specified in a txt file without going on to each host individually.

When building it I found sometimes the network lag would cause the task to be created after the %time% value had passed so I added the 2 minute delay. The IF %m%==58 etc is supposed to prevent an invalid %time% value for example 12:61:00 but this has not been tested.

This is my new code (untested) hopefully it will be a bit more efficient:

Dim oFSO : Set oFSO = CreateObject("Scripting.FileSystemObject")

arrLines = Split(oFSO.OpenTextFile("IPs.txt").ReadAll(), VbCrLf)

For i = 0 To UBound(arrLines)

strComputer = arrLines(i)
strProg = "notepad.exe"

Set objWMIService = GetObject _
("winmgmts:\\" & strComputer & "\root\cimv2:Win32_Process")

Next
 
Back
Top Bottom