PDA

View Full Version : scheduling an event whenever i connect to a wired network


yawnmoth
September 26th 09, 06:57 PM
Events can be scheduled to run at a certain time via the Windows
Scheduler. What if you wanted to trigger an event to run during a
certain "event". ie. make it so one event triggers another?

Pegasus [MVP]
September 26th 09, 07:43 PM
"yawnmoth" > wrote in message
...
> Events can be scheduled to run at a certain time via the Windows
> Scheduler. What if you wanted to trigger an event to run during a
> certain "event". ie. make it so one event triggers another?

Here you go:
@echo off
set BatchName=d:\temp\test.bat
set Scr=c:\TempVBS.vbs
set VB=echo^>^>%Scr%
cd 1>nul 2>%Scr%
%VB% Set oWshShell = CreateObject("WScript.Shell")
%VB% Set oWMIService = GetObject("winmgmts:\\.\root\wmi")
%VB% Set cMonitoredEvents = oWMIService.ExecNotificationQuery _
%VB% ("Select * from MSNDIS_StatusMediaConnect")
%VB% Do While True
%VB% Set sLatestEvent = cMonitoredEvents.NextEvent
%VB% oWshShell.Run "cmd.exe /c %BatchName% " _
%VB% ^& sLatestEvent.InstanceName
%VB% Loop
start /b cscript //nologo %Scr%
ping localhost -n 10 > nul
del %Scr%

Line 2 contains a reference to the command you wish to execute when you plug
in your network cable, d:\temp\test.bat in this case. It might contain the
following lines:
@echo off
echo %date% %time:~0,5% %* >> d:\test.txt

To make maintenance easier, you could combine the two batch files in a
single batch file and invoke either one component or the other, depending on
the parameter you supply.

yawnmoth
September 26th 09, 10:39 PM
On Sep 26, 1:43*pm, "Pegasus [MVP]" > wrote:
> "yawnmoth" > wrote in message
>
> ...
>
> > Events can be scheduled to run at a certain time via the Windows
> > Scheduler. *What if you wanted to trigger an event to run during a
> > certain "event". *ie. make it so one event triggers another?
>
> Here you go:
> @echo off
> set BatchName=d:\temp\test.bat
> set Scr=c:\TempVBS.vbs
> set VB=echo^>^>%Scr%
> cd 1>nul 2>%Scr%
> %VB% Set oWshShell = CreateObject("WScript.Shell")
> %VB% Set oWMIService = GetObject("winmgmts:\\.\root\wmi")
> %VB% Set cMonitoredEvents = oWMIService.ExecNotificationQuery _
> %VB% * ("Select * from MSNDIS_StatusMediaConnect")
> %VB% Do While True
> %VB% * Set sLatestEvent = cMonitoredEvents.NextEvent
> %VB% * oWshShell.Run "cmd.exe /c %BatchName% " _
> %VB% * ^& sLatestEvent.InstanceName
> %VB% Loop
> start /b cscript //nologo %Scr%
> ping localhost -n 10 > nul
> del %Scr%

Interesting. So I guess the vbscript just runs constantly, in the
background? Are there situations where it could bog down system
performance? Does cMonitoredEvents.NextEvent block until there's a
change?

Also, what other tables are there? There's
MSNDIS_StatusMediaConnect... are there any other tables?

Pegasus [MVP]
September 26th 09, 11:03 PM
"yawnmoth" > wrote in message
...
On Sep 26, 1:43 pm, "Pegasus [MVP]" > wrote:
> "yawnmoth" > wrote in message
>
> ...
>
> > Events can be scheduled to run at a certain time via the Windows
> > Scheduler. What if you wanted to trigger an event to run during a
> > certain "event". ie. make it so one event triggers another?
>
> Here you go:
> @echo off
> set BatchName=d:\temp\test.bat
> set Scr=c:\TempVBS.vbs
> set VB=echo^>^>%Scr%
> cd 1>nul 2>%Scr%
> %VB% Set oWshShell = CreateObject("WScript.Shell")
> %VB% Set oWMIService = GetObject("winmgmts:\\.\root\wmi")
> %VB% Set cMonitoredEvents = oWMIService.ExecNotificationQuery _
> %VB% ("Select * from MSNDIS_StatusMediaConnect")
> %VB% Do While True
> %VB% Set sLatestEvent = cMonitoredEvents.NextEvent
> %VB% oWshShell.Run "cmd.exe /c %BatchName% " _
> %VB% ^& sLatestEvent.InstanceName
> %VB% Loop
> start /b cscript //nologo %Scr%
> ping localhost -n 10 > nul
> del %Scr%

Interesting. So I guess the vbscript just runs constantly, in the
background? Are there situations where it could bog down system
performance? Does cMonitoredEvents.NextEvent block until there's a
change?

Also, what other tables are there? There's
MSNDIS_StatusMediaConnect... are there any other tables?
=========
It is cscript.exe that runs constantly in the background (and that can be
killed with taskkill.exe if you so desire). I do not think that the
MSNdis_StatusMediaConnect event class causes any significant CPU loading but
then you can easily work this out for yourself by keeping the script running
for an hour before checking how much CPU time the cscript.exe process has
clocked up.

About other event classes - other than MSNDIS-StatusMediaDisconnect I do not
know. If you have a lot of patience then you can google for these two
classes. You will get some Microsoft TechNet pages which are likely to
contain a list of available classes.

Pegasus [MVP]
September 27th 09, 09:09 AM
"yawnmoth" > wrote in message
...
On Sep 26, 1:43 pm, "Pegasus [MVP]" > wrote:
> "yawnmoth" > wrote in message
>
> ...
>
> > Events can be scheduled to run at a certain time via the Windows
> > Scheduler. What if you wanted to trigger an event to run during a
> > certain "event". ie. make it so one event triggers another?
>
> Here you go:
> @echo off
> set BatchName=d:\temp\test.bat
> set Scr=c:\TempVBS.vbs
> set VB=echo^>^>%Scr%
> cd 1>nul 2>%Scr%
> %VB% Set oWshShell = CreateObject("WScript.Shell")
> %VB% Set oWMIService = GetObject("winmgmts:\\.\root\wmi")
> %VB% Set cMonitoredEvents = oWMIService.ExecNotificationQuery _
> %VB% ("Select * from MSNDIS_StatusMediaConnect")
> %VB% Do While True
> %VB% Set sLatestEvent = cMonitoredEvents.NextEvent
> %VB% oWshShell.Run "cmd.exe /c %BatchName% " _
> %VB% ^& sLatestEvent.InstanceName
> %VB% Loop
> start /b cscript //nologo %Scr%
> ping localhost -n 10 > nul
> del %Scr%

Interesting. So I guess the vbscript just runs constantly, in the
background? Are there situations where it could bog down system
performance? Does cMonitoredEvents.NextEvent block until there's a
change?

Also, what other tables are there? There's
MSNDIS_StatusMediaConnect... are there any other tables?

==============

Correction: It's probably both cscript.exe and wmiprvse.exe that you need to
monitor when checking processor loading.

Google