Windows registry scripting with PHP (compiled to EXE!)

I do a lot of windows scripting for all sorts of different reasons, and I use different languages depending on what I want to do. For example, I built an intranet website which catalogs all of our Windows PCs, the specifications, their software etc. But it was messy because I used KixTart for the main script which gathers data about the machine with WMI, and sent that info back to the server which parsed all the data with PHP. I also had other scripts written to pull back other info, using Autohotkey and other languages, and all going back to the same PHP script for parsing. Anyway, I think I should be able to do all of this with PHP, although I didn’t realise I could.

There are a lot of PHP compilers out there but for simple stuff like file parsing, directory operations etc. I always just use Bambalam PHP EXE Compiler/Embedder. It’s a great piece of software, very easy to use, creates decent size EXE files for Windows by simply packing the PHP parser and your code together into a nice little EXE package. I should point out that I’ve only ever used it for command-line stuff but I’m pretty sure you can do some GUI stuff with Winbinder. The only real drawback is that it uses PHP 4.4 and hasn’t been updated since 2006 I think. However, it’s still a very useful piece of kit for those who like PHP and also windows scripting. I only found out lately about PHP’s COM scripting and that I could access the registry with this without the need for third party tools like REG.EXE etc. - You can even use the COM scripting to check EXE file versions etc.

So I’ve included some basic code below to allow you to check a value from the registry (in this case it’s the Lotus Notes path, and then it checks the notes.exe file version from that path. I’m sure you can modify this to do all sorts of useful Windows stuff. COM is extremely useful and there’s an entire MSDN codebase there for you to play with.

So anyway here’s the code:


<?php 
error_reporting(0); // turns off exceptions for missing paths etc 

$reg = new COM("WScript.Shell") or die("Requires Windows Scripting Host"); 
$stringtoread="HKEY_LOCAL_MACHINE\\SOFTWARE\\Lotus\\Notes\\Path"; //works with values, not sure how to read entire keys 
$LotusPath = $reg->RegRead($stringtoread); 
if ($LotusPath){ 
        $fso = new COM("Scripting.FileSystemObject") or die("Could not create Scripting.FileSystemObject"); 
        $version = $fso->GetFileVersion("$LotusPath\\notes.exe"); 
} 

if ($version)echo "Lotus Notes Version = $version"; 
?>

With bamcompile I was able to compile the PHP above into a simple EXE of only 514KB! if Javascript is more your thing, I will be writing a similar article soon about compiling jscript to EXE files.

Blog comments powered by Disqus