Using PHP instead of VBS for Windows System Administration

Managing many computers is not easy. No matter if they are Windows, Linux or Mac, it is extremity time consuming if you have to physically visit each one to make a change or get information. I personally am an ‘administration through automation’ type of person. I never walked around to a bunch of machines to repeatedly make changes or gather information, I always scripted it. In the past I have written some interesting expect scripts for Linux and some quite baffling windows batch files.

With windows batch scripting there is a point that it would be nice to understand a script later and be able to share it and support it. The way batch scripting on windows is limited this just isn’t practical. That is where Visual Basic Script becomes the natural progression with your Windows administration. The Official Scripting Guys Forum is a great place for finding such scripts. VBS has it’s drawbacks too. It isn’t a language that finds much use anywhere but administration. It would be nice to be able to use a more flexible language to do the same administrative capabilities and open up that functionality to things like web applications.

You may not think first of PHP as being a language that can wrap its fingers around Windows hooks and make the OS dance like a marionette. But it can with a very useful extension called COM. Some of you may be familiar with using .NET libraries in PHP. But some functionality is available directly to PHP without the overhead of another framework. I will no doubt have a few posts on this subject but I would like to give you a brief introduction to this functionality.

A useful example would be to list out the local users for a given computer. The VBS version of the script from here looks like this (I simplified the script, we all know how to read a file):
On Error Resume Next
Set objComputer = GetObject("WinNT://localhost")
objComputer.Filter = Array("User")
For Each objUser in objComputer
Wscript.Echo objUser.Name & "(" & objUser.FullName & ")"
Next

Now with PHP the exact same thing can be done. I will simplify things a bit at first and we will experiment a bit with our script.
$objComputer = new COM("WinNT://localhost");
$objComputer->Filter = array("User");
$objShell = new COM("Wscript.Shell");

foreach ($objComputer as $objUser)
{
echo $objUser->Name . "\n";
}

Notice on the first line direct translation of ‘GetObject’  to ‘new COM.  The only other differences are simply language specific.  Notice the switch from period (‘.’) to the arrow (‘->’) object delimiter (I don’t remember what this is called off hand).  After adding the semi-colons (‘;’) it starts to really look like PHP. Then changing the ‘foreach’ loop should also be straight forward.  You will also need to change the echo function to the PHP version.  As you can see this process could be automated.  If I get the time I may just do that.

Run this from the command line or from your browser and you will get a list of all the local User group accounts.  Now just to add a little spice to the script, lets pop up a native windows dialog with the computer name in it.  Note the $objShell var below:
$objComputer = new COM("WinNT://localhost");
$objComputer->Filter = array("User");
$objShell = new COM("Wscript.Shell");

foreach ($objComputer as $objUser)
{
$objShell->Popup("User:" . $objUser->Name);
}

Save that out and make sure not to run that one through your webserver as you will no doubt get errors. Run from the command prompt or double clicked and you will get a window with an OK button for each user. As you can see it is easy to access native Windows assemblies for native scripting. It is also easy to translate a Visual Basic Script into PHP. With all the added benefit that PHP brings, it is easy to see the potential. If you come up with any PHP scripts let me know!

Advertisements
Using PHP instead of VBS for Windows System Administration

4 thoughts on “Using PHP instead of VBS for Windows System Administration

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s