<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>No Znx! &#187; vbs</title>
	<atom:link href="http://znx.no/tag/vbs/feed/" rel="self" type="application/rss+xml" />
	<link>http://znx.no</link>
	<description>the pigeons!!!!</description>
	<lastBuildDate>Tue, 20 Dec 2011 22:42:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>BigBrother Scripting</title>
		<link>http://znx.no/2009/03/bigbrother-scripting/</link>
		<comments>http://znx.no/2009/03/bigbrother-scripting/#comments</comments>
		<pubDate>Tue, 17 Mar 2009 22:40:13 +0000</pubDate>
		<dc:creator>znx</dc:creator>
				<category><![CDATA[Scripts]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[ms]]></category>
		<category><![CDATA[vbs]]></category>

		<guid isPermaLink="false">http://znx.no/?p=108</guid>
		<description><![CDATA[I was recently asked if it was possible to monitor the Event Log for a single event and ensure that it was occurring regularly. It is rare that I handle Windows scripting and when I do I normally find myself cursing it, haha! In this case we want to ensure that a print server is [...]]]></description>
			<content:encoded><![CDATA[<p>I was recently asked if it was possible to monitor the Event Log for a single event and ensure that it was occurring regularly. It is rare that I handle Windows scripting and when I do I normally find myself cursing it, haha! In this case we want to ensure that a print server is constantly printing through the day, we expect that at least 1 print job will occur every 15 minutes, if not then we&#8217;d like a warning. Obviously this check should only run during work hours.</p>
<p>So the first step is relatively simple, access the Event Log and look for a single event by its event code.</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #151B8D; font-weight: bold;">set</span> objWMIService = <span style="color: #E56717; font-weight: bold;">GetObject</span>(<span style="color: #800000;">&quot;winmgmts:\root\cimv2&quot;</span>)
<span style="color: #151B8D; font-weight: bold;">set</span> colEvents = objWMIService.ExecQuery _
   (<span style="color: #800000;">&quot;Select * from Win32_NTLogEvent Where Logfile = 'System' and EventCode = 10&quot;</span>)</pre></div></div>

<p>What we did here was grab all the events with event code of 10 (a print job!). So if we count the number of events within a range then we will have basically completed a huge part of the work.</p>
<p>So next step is to make an interval that will be 15 minutes back from whatever time is current.</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #151B8D; font-weight: bold;">set</span> dtmStartDate = <span style="color: #E56717; font-weight: bold;">CreateObject</span>(<span style="color: #800000;">&quot;WbemScripting.SWbemDateTime&quot;</span>)
dtmStartDate.SetVarDate DateAdd(<span style="color: #800000;">&quot;n&quot;</span>,-15,Now()),<span style="color: #00C2FF; font-weight: bold;">True</span></pre></div></div>

<p>And applying that into our statement:</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #151B8D; font-weight: bold;">set</span> colEvents = objWMIService.ExecQuery _
   (<span style="color: #800000;">&quot;Select * from Win32_NTLogEvent Where Logfile = 'System' and EventCode = 10&quot;</span> _
   &amp;amp; <span style="color: #800000;">&quot; and TimeWritten &amp;gt;= '&quot;</span> &amp;amp; dtmStartDate &amp;amp; <span style="color: #800000;">&quot;'&quot;</span>)</pre></div></div>

<p>This means that we are now collecting the events that only occurred within the last fifteen minutes. So what next, well we need to have a statement to pass to BigBrother to indicate success or failure. Fortunately enough I have another script which monitors the cluster (thanks to the awesome DeadCat repository) and it has some code to help place the file that BigBrother collects.</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;">const HKLM = &amp;amp;H80000002
strBBExtPathNew = <span style="color: #800000;">&quot;SOFTWARE\Quest Software\BigBrother\bbnt\ExternalPath&quot;</span>
strBBExtPathOld = <span style="color: #800000;">&quot;SOFTWARE\BigBrother\bbnt\ExternalPath&quot;</span>
<span style="color: #151B8D; font-weight: bold;">set</span> oReg = <span style="color: #E56717; font-weight: bold;">GetObject</span>(<span style="color: #800000;">&quot;winmgmts:\root\default:StdRegProv&quot;</span>)
&nbsp;
oReg.GetStringValue HKLM,strBBExtPathNew,,strExtPath
<span style="color: #8D38C9; font-weight: bold;">if</span> isNull(strExtPath) <span style="color: #8D38C9; font-weight: bold;">then</span>
  oReg.GetStringValue HKLM,strBBExtPathOld,,strExtPath
<span style="color: #8D38C9; font-weight: bold;">end</span> <span style="color: #8D38C9; font-weight: bold;">if</span>
<span style="color: #8D38C9; font-weight: bold;">if</span> isNull(strExtPath) <span style="color: #8D38C9; font-weight: bold;">then</span>
  WScript.Quit
<span style="color: #8D38C9; font-weight: bold;">end</span> <span style="color: #8D38C9; font-weight: bold;">if</span></pre></div></div>

<p><span id="more-108"></span>So now we have the path to put our file in, all we need to do now is write the file and add two simple checks to see that the time is within work ours (9-5).</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #151B8D; font-weight: bold;">On</span> <span style="color: #151B8D; font-weight: bold;">Error</span> <span style="color: #151B8D; font-weight: bold;">Resume</span> <span style="color: #8D38C9; font-weight: bold;">Next</span>
&nbsp;
strState = <span style="color: #800000;">&quot;green Printing was active within the last 15 minutes&quot;</span>
strBBFileName = <span style="color: #800000;">&quot;print&quot;</span>
intEventCode = 10
&nbsp;
<span style="color: #008000;">' Find out where to put the BB log files
</span>const HKLM = &amp;amp;H80000002
strBBExtPathNew = <span style="color: #800000;">&quot;SOFTWARE\Quest Software\BigBrother\bbnt\ExternalPath&quot;</span>
strBBExtPathOld = <span style="color: #800000;">&quot;SOFTWARE\BigBrother\bbnt\ExternalPath&quot;</span>
<span style="color: #151B8D; font-weight: bold;">set</span> oReg = <span style="color: #E56717; font-weight: bold;">GetObject</span>(<span style="color: #800000;">&quot;winmgmts:\root\default:StdRegProv&quot;</span>)
&nbsp;
<span style="color: #008000;">' Check for the BB External Path, new then old otherwise quit
</span>oReg.GetStringValue HKLM,strBBExtPathNew,,strExtPath
<span style="color: #8D38C9; font-weight: bold;">if</span> isNull(strExtPath) <span style="color: #8D38C9; font-weight: bold;">then</span>
  oReg.GetStringValue HKLM,strBBExtPathOld,,strExtPath
<span style="color: #8D38C9; font-weight: bold;">end</span> <span style="color: #8D38C9; font-weight: bold;">if</span>
<span style="color: #8D38C9; font-weight: bold;">if</span> isNull(strExtPath) <span style="color: #8D38C9; font-weight: bold;">then</span>
  WScript.Quit
<span style="color: #8D38C9; font-weight: bold;">end</span> <span style="color: #8D38C9; font-weight: bold;">if</span>
&nbsp;
<span style="color: #008000;">' Get Computer name
</span><span style="color: #151B8D; font-weight: bold;">set</span> WshNetwork = WScript.<span style="color: #E56717; font-weight: bold;">CreateObject</span>(<span style="color: #800000;">&quot;WScript.Network&quot;</span>)
strComputerName = WshNetwork
&nbsp;
<span style="color: #008000;">' Prepare file
</span><span style="color: #151B8D; font-weight: bold;">set</span> fso = <span style="color: #E56717; font-weight: bold;">CreateObject</span>(<span style="color: #800000;">&quot;Scripting.FileSystemObject&quot;</span>)
<span style="color: #151B8D; font-weight: bold;">set</span> f = fso.OpenTextFile(strExtPath &amp;amp; <span style="color: #800000;">&quot;\&quot;</span> &amp;amp; strBBFileName, 8, <span style="color: #00C2FF; font-weight: bold;">True</span>)
&nbsp;
<span style="color: #008000;">' Run between 9 - 5
</span><span style="color: #8D38C9; font-weight: bold;">if</span> Hour(Now()) &amp;lt; 9 <span style="color: #8D38C9; font-weight: bold;">then</span>
  strState = <span style="color: #800000;">&quot;green Not checking as we are outwith work hours&quot;</span>
  f.Write strState
  <span style="color: #151B8D; font-weight: bold;">set</span> fso = <span style="color: #00C2FF; font-weight: bold;">Nothing</span>
  <span style="color: #151B8D; font-weight: bold;">set</span> f = <span style="color: #00C2FF; font-weight: bold;">Nothing</span>
  WScript.Quit
<span style="color: #8D38C9; font-weight: bold;">end</span> <span style="color: #8D38C9; font-weight: bold;">if</span>
<span style="color: #8D38C9; font-weight: bold;">if</span> Hour(Now()) &amp;gt; 17 <span style="color: #8D38C9; font-weight: bold;">then</span>
  strState = <span style="color: #800000;">&quot;green Not checking as we are outwith work hours&quot;</span>
  f.Write strState
  <span style="color: #151B8D; font-weight: bold;">set</span> fso = <span style="color: #00C2FF; font-weight: bold;">Nothing</span>
  <span style="color: #151B8D; font-weight: bold;">set</span> f = <span style="color: #00C2FF; font-weight: bold;">Nothing</span>
  WScript.Quit
<span style="color: #8D38C9; font-weight: bold;">end</span> <span style="color: #8D38C9; font-weight: bold;">if</span>
&nbsp;
<span style="color: #008000;">' Get a date 15 minutes in the past.
</span><span style="color: #151B8D; font-weight: bold;">set</span> dtmStartDate = <span style="color: #E56717; font-weight: bold;">CreateObject</span>(<span style="color: #800000;">&quot;WbemScripting.SWbemDateTime&quot;</span>)
dtmStartDate.SetVarDate DateAdd(<span style="color: #800000;">&quot;n&quot;</span>,-15,Now()),<span style="color: #00C2FF; font-weight: bold;">True</span>
&nbsp;
<span style="color: #008000;">' Collect the events from eventlog within the last 15 minutes
</span><span style="color: #151B8D; font-weight: bold;">set</span> objWMIService = <span style="color: #E56717; font-weight: bold;">GetObject</span>(<span style="color: #800000;">&quot;winmgmts:\root\cimv2&quot;</span>)
<span style="color: #151B8D; font-weight: bold;">Set</span> colEvents = objWMIService.ExecQuery _
   (<span style="color: #800000;">&quot;Select * from Win32_NTLogEvent Where Logfile = 'System' and &quot;</span> _
   &amp;amp; <span style="color: #800000;">&quot;EventCode = '&quot;</span> &amp;amp; intEventCode _
   &amp;amp; <span style="color: #800000;">&quot;' and TimeWritten &amp;gt;= '&quot;</span> &amp;amp; dtmStartDate &amp;amp; <span style="color: #800000;">&quot;'&quot;</span>)
&nbsp;
<span style="color: #008000;">' If no events then alert.
</span><span style="color: #8D38C9; font-weight: bold;">if</span> colEvents.Count = 0 <span style="color: #8D38C9; font-weight: bold;">then</span>
  strState = <span style="color: #800000;">&quot;red No print job within the last 15 minutes&quot;</span>
<span style="color: #8D38C9; font-weight: bold;">end</span> <span style="color: #8D38C9; font-weight: bold;">if</span>
&nbsp;
<span style="color: #008000;">' Output
</span>f.Write strState
&nbsp;
<span style="color: #151B8D; font-weight: bold;">set</span> fso = <span style="color: #00C2FF; font-weight: bold;">Nothing</span>
<span style="color: #151B8D; font-weight: bold;">set</span> f = <span style="color: #00C2FF; font-weight: bold;">Nothing</span>
WScript.Quit</pre></div></div>

<p>Obviously you can adjust this script to monitor for any regular event and it works well on Windows 2003 and XP machines. It is at this point I thought that I was finished. Testing this script on a Windows XP machine, no issues it was doing exactly what it was meant to. However the server I was intending on running it under was a Windows 2000 server. So I was forced to make some more changes to the script to allow it to run. The changes I made unfortunately make the script slightly harder to adjust to make it work for any event.</p>
<p>The first change was to the statement to collect the print job.</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #151B8D; font-weight: bold;">Set</span> colEvents = objWMIService.ExecQuery _
  (<span style="color: #800000;">&quot;Select * From Win32_NTLogEvent Where Type = 'information' &quot;</span> _
  &amp; <span style="color: #800000;">&quot;and EventCode = &quot;</span> &amp; intEventCode _
  &amp; <span style="color: #800000;">&quot;TimeWritten &gt;= '&quot;</span> &amp; dtmStartDate &amp; <span style="color: #800000;">&quot;'&quot;</span>)</pre></div></div>

<p>The next change I had to make was very strange, the Count method didn&#8217;t seem to exist (strange!?) so I was forced to loop to count:</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #008000;">' UGLY
</span><span style="color: #151B8D; font-weight: bold;">set</span> intCount = 0
<span style="color: #8D38C9; font-weight: bold;">For</span> <span style="color: #8D38C9; font-weight: bold;">Each</span> objEvent <span style="color: #8D38C9; font-weight: bold;">in</span> colEvents
  intCount = intCount + 1
<span style="color: #8D38C9; font-weight: bold;">Next</span>
<span style="color: #8D38C9; font-weight: bold;">if</span> intCount = 0 <span style="color: #8D38C9; font-weight: bold;">then</span>
  strState = <span style="color: #800000;">&quot;red No print job within the last 15 minutes&quot;</span>
<span style="color: #8D38C9; font-weight: bold;">end</span> <span style="color: #8D38C9; font-weight: bold;">if</span></pre></div></div>

<p>Those two changes allowed me to successfully monitor a single event. Same script in Linux would have taken me seconds! Anyway at least its a working model, let&#8217;s hope not a lot more requests come through for Windows issues, especially W2K ones!</p>
]]></content:encoded>
			<wfw:commentRss>http://znx.no/2009/03/bigbrother-scripting/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

