<?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>Projects Log &#187; vbscript</title>
	<atom:link href="http://dev.pulsed.net/wp/?cat=25&#038;feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://dev.pulsed.net/wp</link>
	<description>projects, diy</description>
	<lastBuildDate>Mon, 25 Jul 2011 11:22:33 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Vbscript CGI (No&#8230; not ASP)</title>
		<link>http://dev.pulsed.net/wp/?p=32</link>
		<comments>http://dev.pulsed.net/wp/?p=32#comments</comments>
		<pubDate>Thu, 15 Nov 2007 19:37:55 +0000</pubDate>
		<dc:creator>Kevin DEHLINGER</dc:creator>
				<category><![CDATA[Windows]]></category>
		<category><![CDATA[awesome]]></category>
		<category><![CDATA[cgi]]></category>
		<category><![CDATA[vbscript]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[win32]]></category>

		<guid isPermaLink="false">http://dev.pulsed.net/wp/?p=32</guid>
		<description><![CDATA[Yesterday, I don&#039;t know how, but while doing nothing I suddenly got the best idea ever: Using vbscript scripts with apache&#8230; and guess what&#8230; after some coding/debugging hours, it actually works like a charm!

so how did I manage to get this working?
Each computer with at least &#034;windows 2000&#034; should have cscript.exe somewhere: it&#039;s the vbscript/jscript [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday, I don&#039;t know how, but while doing nothing I suddenly got the best idea ever: Using vbscript scripts with apache&#8230; and guess what&#8230; after some coding/debugging hours, it actually works like a charm!<br />
<span id="more-32"></span><br />
so how did I manage to get this working?<br />
Each computer with at least &#034;windows 2000&#034; should have cscript.exe somewhere: it&#039;s the vbscript/jscript console interpreter</p>
<p>so I started to write my first script:</p>
<p><em><u>test.vbs</u></em><br />
<code>#!c:\WINDOWS\system32\cscript.exe<br />
wscript.echo "Content-type: text/html" &amp; vbcrlf<br />
wscript.echo "Hello World"</code></p>
<p>but it didn&#039;t work (500 Internal Server Error) so I checked apache error log and found:<br />
<code>C:\\wamp\\cgi-bin\\test.vbs(1, 1) Erreur de compilation Microsoft VBScript: Instruction attendue\r</code></p>
<p>Error in the first line at the first character&#8230; oh wait that&#039;s the shebang<br />
How to ignore the first line in my vbs code?</p>
<p>No idea? well that&#039;s the second best idea ever: you simply need to call another vbs script from the shebang that somehow ignores the first line and include the remaining part and call it for example &#034;vbscript_cgi.vbs&#034;:</p>
<p><em><u>vbscript_cgi.vbs</u></em><br />
<code>Set arguments = WScript.Arguments<br />
If arguments.Count &gt;=1 Then<br />
Set FSO = CreateObject("Scripting.FileSystemObject")<br />
	If FSO.FileExists(arguments(0)) Then<br />
		Set cgi = FSO.OpenTextFile(arguments(0))<br />
		cgi.ReadLine<br />
		ExecuteGlobal cgi.ReadAll<br />
		cgi.Close<br />
	End If<br />
	Set FSO = Nothing<br />
End If<br />
Set arguments = Nothing</code></p>
<p><em><u>test.vbs</u></em><br />
<code>#!c:\WINDOWS\system32\cscript.exe "C:\wamp\cgi-bin\vbscript_cgi.vbs"<br />
wscript.echo "Content-type: text/html" &amp; vbcrlf<br />
wscript.echo "Hello World"</code></p>
<p>Nah bad luck, still error 500&#8230; apache log says <code>malformed header from script. Bad header=Microsoft (R) Windows Script H: test.vbs</code> &#8230; wtf?!?</p>
<p>Oooh right&#8230; cscript always displays a banner when you launch a script:<br />
<code>C:\wamp\cgi-bin&gt;cscript test.vbs<br />
Microsoft (R) Windows Script Host Version 5.6<br />
Copyright (C) Microsoft Corporation 1996-2001. Tous droits rÃ©servÃ©s.</code></p>
<p>Lets add the &#034;/nologo&#034; switch in the shebang to avoid that:<br />
<em><u>test.vbs</u></em><br />
<code>#!c:\WINDOWS\system32\cscript.exe /nologo "C:\wamp\cgi-bin\vbscript_cgi.vbs"<br />
wscript.echo "Content-type: text/html" &amp; vbcrlf<br />
wscript.echo "Hello World"</code></p>
<p>yayyyy it wooooooorks :D<br />
<img src="http://dev.pulsed.net/misc/vbscriptcgi1.png" alt="hello world" /></p>
<p>after that I added some features such as the &#034;print&#034; alias, GET/POST variables support, and now that&#039;s a quick example what it can do:<br />
<img src="http://dev.pulsed.net/misc/vbscriptcgi2.png" alt="advanced example" /></p>
<div style="border: 1px dashed #708090; background-color: #add8e6">
	<code>'''''''''''''''''''''''''''</code><br />
	<code>'' vbscript_cgi.vbs      ''</code><br />
	<code>'' Kevin DEHLINGER       ''</code><br />
	<code>'' 2007 / dev.pulsed.net ''</code><br />
	<code>'''''''''''''''''''''''''''</code><br />
	<code></code><br />
	<code>Set arguments = WScript.Arguments</code><br />
	<code>If arguments.Count &gt;=1 Then</code><br />
	<code>Set FSO = CreateObject(&quot;Scripting.FileSystemObject&quot;)</code><br />
	<code>If FSO.FileExists(arguments(0)) Then</code><br />
	<code>Set cgi = FSO.OpenTextFile(arguments(0))</code><br />
	<code>cgi.ReadLine</code><br />
	<code>ExecuteGlobal cgi.ReadAll</code><br />
	<code>cgi.Close</code><br />
	<code>End If</code><br />
	<code>Set FSO = Nothing</code><br />
	<code>End If</code><br />
	<code>Set arguments = Nothing</code><br />
	<code></code><br />
	<code></code><br />
	<code>sub print(v)</code><br />
	<code>wscript.echo v</code><br />
	<code>end sub</code><br />
	<code></code><br />
	<code>sub println(v)</code><br />
	<code>wscript.echo v &amp; vbcrlf</code><br />
	<code>end sub</code><br />
	<code></code><br />
	<code></code><br />
	<code>Function GetCgiVars</code><br />
	<code></code><br />
	<code>Set Shell = CreateObject( &quot;WScript.Shell&quot; )</code><br />
	<code>reqmethod=Ucase( Shell.ExpandEnvironmentStrings(&quot;%REQUEST_METHOD%&quot;))</code><br />
	<code></code><br />
	<code></code><br />
	<code>Select Case reqmethod</code><br />
	<code></code><br />
	<code>Case &quot;GET&quot;,&quot;HEAD&quot;</code><br />
	<code>cgiinput=Shell.ExpandEnvironmentStrings(&quot;%QUERY_STRING%&quot;)&amp; &quot;&quot;</code><br />
	<code></code><br />
	<code>Case &quot;POST&quot;</code><br />
	<code></code><br />
	<code>content_length=Shell.ExpandEnvironmentStrings(&quot;%CONTENT_LENGTH%&quot;)</code><br />
	<code></code><br />
	<code>If not IsNumeric(content_length) then</code><br />
	<code>print &quot;Content-Type: text/plain&quot; &amp; vbcrlf</code><br />
	<code>print &quot;No Content-Length was sent with the POST request&quot; &amp; vbcrlf</code><br />
	<code>Exit Function</code><br />
	<code>End If</code><br />
	<code></code><br />
	<code>cgiinput= WScript.StdIn.ReadAll</code><br />
	<code></code><br />
	<code></code><br />
	<code>Case Else</code><br />
	<code>print &quot;Content-Type: text/plain&quot; &amp; vbcrlf</code><br />
	<code>print &quot;Unsupported REQUEST_METHOD (&quot; &amp; reqmethod &amp; &quot;)&quot; &amp; vbcrlf</code><br />
	<code>Exit Function</code><br />
	<code></code><br />
	<code>End Select</code><br />
	<code></code><br />
	<code>cgiinput=Replace(cgiinput,&quot;+&quot;,&quot; &quot;)</code><br />
	<code>cgiinput=Replace(cgiinput,&quot;&amp;&quot;,&quot;;&quot;)</code><br />
	<code>cgiinput=Replace(cgiinput,&quot;=&quot;,&quot;=;&quot;)</code><br />
	<code></code><br />
	<code>InputArray=Split(cgiinput,&quot;;&quot;)</code><br />
	<code>Set CgiVars = CreateObject(&quot;Scripting.Dictionary&quot;)</code><br />
	<code></code><br />
	<code></code><br />
	<code>status=0</code><br />
	<code>Hash_Key=&quot;&quot;</code><br />
	<code></code><br />
	<code>For Each value in InputArray</code><br />
	<code>If(Mid(value,Len(value),1)=&quot;=&quot;) Then</code><br />
	<code>Hash_Key=Mid(value,1,Len(value)-1)</code><br />
	<code>Else</code><br />
	<code>CgiVars.Add Unescape_Url(Hash_Key),Unescape_Url(value)</code><br />
	<code>End If</code><br />
	<code>Next</code><br />
	<code></code><br />
	<code>Set GetCgiVars=CgiVars</code><br />
	<code>End Function</code><br />
	<code></code><br />
	<code></code><br />
	<code>Function Unescape_Url (url)</code><br />
	<code>Set RegExp1 = New RegExp</code><br />
	<code>RegExp1.IgnoreCase = True</code><br />
	<code>RegExp1.Global = True</code><br />
	<code>RegExp1.Pattern = &quot;%([0-9A-F]{2})&quot;</code><br />
	<code></code><br />
	<code></code><br />
	<code>Set Matches = RegExp1.Execute(url)</code><br />
	<code>For Each Match in Matches</code><br />
	<code>url = Replace(url,&quot;%&quot; &amp; Match,Hex2Char(Match))</code><br />
	<code>Next</code><br />
	<code>Unescape_Url=url</code><br />
	<code>End Function</code><br />
	<code></code><br />
	<code>Function Hex2Char (hexx)</code><br />
	<code>Hex2Char = Chr(1*CLng(&quot;&amp;H&quot; &amp; hexx))</code><br />
	<code>End Function</code>
</div>
<p>	<strong>Download this code:</strong> <a href="http://dev.pulsed.net/wp/code/vbscript_cgi.vbs">vbscript_cgi.vbs</a>
</ol>
<div style="border: 1px dashed #708090; background-color: #add8e6">
	<code>#!c:\WINDOWS\system32\cscript.exe /nologo &quot;C:\wamp\cgi-bin\vbscript_cgi.vbs&quot;</code><br />
	<code></code><br />
	<code>''''''''''''''''''''''''''''''</code><br />
	<code>'' testscript.vbs           ''</code><br />
	<code>'' Kevin DEHLINGER          ''</code><br />
	<code>'' 2007 / dev.pulsed.net    ''</code><br />
	<code>'' query some data with     ''</code><br />
	<code>'' wmi/wsh (some of thecode ''</code><br />
	<code>'' comes from scriptomatic) ''</code><br />
	<code>''''''''''''''''''''''''''''''</code><br />
	<code></code><br />
	<code></code><br />
	<code>print &quot;Content-type: text/html&quot; &amp; vbcrlf</code><br />
	<code></code><br />
	<code></code><br />
	<code>print &quot;&lt;!DOCTYPE html PUBLIC &quot;&quot;-//W3C//DTD HTML 4.01//EN&quot;&quot; \&quot;&quot;http://www.w3.org/TR/html4/strict.dtd&quot;&quot;&gt;&quot; &amp; vbcrlf &amp; _</code><br />
	<code>&quot;&lt;html&gt;&quot; &amp; _</code><br />
	<code>&quot;&lt;head&gt;&quot; &amp; _</code><br />
	<code>&quot;  &lt;meta content=&quot;&quot;text/html; charset=ISO-8859-1&quot;&quot;&quot;  &amp; _</code><br />
	<code>&quot; http-equiv=&quot;&quot;content-type&quot;&quot;&gt;&quot;  &amp; _</code><br />
	<code>&quot;  &lt;title&gt;&lt;/title&gt;&quot; &amp; _</code><br />
	<code>&quot;&lt;/head&gt;&quot; &amp; _</code><br />
	<code>&quot;&lt;body&gt;&quot;</code><br />
	<code></code><br />
	<code></code><br />
	<code>Set mycgivars=GetCgiVars</code><br />
	<code></code><br />
	<code>if mycgivars.exists(&quot;computer&quot;) then</code><br />
	<code>computer=mycgivars.item(&quot;computer&quot;)</code><br />
	<code>else</code><br />
	<code>computer = &quot;.&quot;</code><br />
	<code>end if</code><br />
	<code></code><br />
	<code>print &quot;&lt;form method=&quot;&quot;post&quot;&quot; action=&quot;&quot;testscript.vbs&quot;&quot; name=&quot;&quot;test&quot;&quot;&gt;Computer: &lt;input&quot; &amp; _</code><br />
	<code>&quot; name=&quot;&quot;computer&quot;&quot; value=&quot;&quot;&quot; &amp; computer &amp; &quot;&quot;&quot;&gt;&lt;input&quot; &amp; _</code><br />
	<code>&quot; type=&quot;&quot;submit&quot;&quot;&gt;&lt;/label&gt;&lt;/form&gt;&quot;</code><br />
	<code></code><br />
	<code></code><br />
	<code>On Error Resume Next</code><br />
	<code>Const wbemFlagReturnImmediately = &amp;h10</code><br />
	<code>Const wbemFlagForwardOnly = &amp;h20</code><br />
	<code></code><br />
	<code>println &quot;Computer: &quot; &amp; computer &amp; &quot;&lt;br \&gt;&lt;br \&gt;&quot;</code><br />
	<code></code><br />
	<code></code><br />
	<code>Set objWMIService = GetObject(&quot;winmgmts:\\&quot; &amp; computer &amp; &quot;\root\CIMV2&quot;)</code><br />
	<code>Set colItems = objWMIService.ExecQuery(&quot;SELECT * FROM Win32_LogicalDisk&quot;, &quot;WQL&quot;, _</code><br />
	<code>wbemFlagReturnImmediately + wbemFlagForwardOnly)</code><br />
	<code></code><br />
	<code>For Each objItem In colItems</code><br />
	<code>println &quot;Drive: &quot; &amp; objItem.Caption &amp; &quot;&lt;br \&gt;&quot;</code><br />
	<code>println &quot;Description: &quot; &amp; objItem.Description &amp; &quot;&lt;br \&gt;&quot;</code><br />
	<code>println &quot;VolumeName: &quot; &amp; objItem.VolumeName &amp; &quot;&lt;br \&gt;&quot;</code><br />
	<code>println   &quot;&lt;br \&gt;&quot;</code><br />
	<code>Next</code><br />
	<code></code><br />
	<code></code><br />
	<code></code><br />
	<code>print &quot;&lt;/body&gt;&lt;/html&gt;&quot;</code><br />
	<code></code><br />
	<code>Function WMIDateStringToDate(dtmDate)</code><br />
	<code>print dtm:</code><br />
	<code>WMIDateStringToDate = CDate(Mid(dtmDate, 5, 2) &amp; &quot;/&quot; &amp; _</code><br />
	<code>Mid(dtmDate, 7, 2) &amp; &quot;/&quot; &amp; Left(dtmDate, 4) _</code><br />
	<code>&amp; &quot; &quot; &amp; Mid (dtmDate, 9, 2) &amp; &quot;:&quot; &amp; Mid(dtmDate, 11, 2) &amp; &quot;:&quot; &amp; Mid(dtmDate,13, 2))</code><br />
	<code>End Function</code>
</div>
<p>	<strong>Download this code:</strong> <a href="http://dev.pulsed.net/wp/code/testscript.vbs">testscript.vbs</a>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://dev.pulsed.net/wp/?feed=rss2&amp;p=32</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
