Thursday, May 28, 2009

Shortcuts to Shortcuts

This is aimed at a fairly narrow niche, but here goes.

Let’s just say you have an intranet web site and you want to put a shortcut on each user’s desktop to the portal.  Easy enough.  Piece of cake.  No-Bake cake actually.  But let’s say you want to capture some information from each user hitting the web site.  Easy as well.  Piece of another cake.  Typically you would turn on integrated authentication and capture the HTTP_USER or REMOTE_USER session variables with IIS, or some equivalent with WebSphere or Apache or whatever you use.

But what if you wan to also collect the name of their computer?  The MAC address?  Whether they have Admin rights or not? 

You can do this with a lot of different scripting tools, but KiXtart provides a few more ready-made widgets to solve this task in less time.

The Ingredients:

  • A Windows-based network with Active Directory
  • Login script (KiXtart, in this case)
  • Liquefied Caffeine (hot or cold, doesn’t matter)

The Code:

Break ON
$scname = "Help Desk"
$url = "http://myintranet.company.com/helpdesk/?uid="+@userid+"&cn="+@wksta+
"&p="+@priv+"&m="+@address+"&d="+@date+"&t="+@time

$objShell = CreateObject("Wscript.Shell")
$path = ExpandEnvironmentVars("%USERPROFILE%")+"\Desktop\$scname.url"

? "creating/updating shortcut..."
$sc = $objShell.CreateShortcut($path)
$sc.FullName = $scname
$sc.TargetPath = $url
$sc.Save()
? "shortcut has been updated"


The time-saver here is the library of built-in "macros" available in KiXtart. These are basically global variables with special names, which expose information without having to paste in a bunch of additional code.



This is evident in the example above, by using the macros @userid (username), @wksta (computer name), @priv (“ADMIN” or ”USER”), @address (MAC), @date (current date in YYYY/MM/DD format), and @time (current time).



The code above will both create and replace the shortcut (if it already exists) each time the user logs on.  This automatically captures their login date and time in the URL of the shortcut.  Arguably a silly effort, but it’s just to show you CAN do this.  Rather than having to instruct users (and God help you!) on how to query their computer name, or installing another script or widget to do that with another mouse click, this takes care of that in one step.  As soon as the user hits the URL, your web page has the computer name, MAC, username, local permissions, date and time, and whatever.  You can also include @producttype (operating system name) @csd (version), @onwow64 (32 or 64 bit) and so on.



As I said, you can do this with VBScript, PowerShell, Perl, Python, and probably even with two sticks and bucket of mud and a case of beer.  But sometimes you need to pick the tool that does the job fastest and easiest.  That doesn’t mean it will be the best tool for all other needs.

No comments: