micolous.id.au

The result of a blogging accident

Dynamically changing Windows’ wallpaper based on hostname

I have a situation where I’ve got 14 machines booting Windows XP over iSCSI, and they’ve all got the same computer name set, and they each need to have a different wallpaper set on each machine. This runs the “RetroLAN” machines at StreetGeek in the new setup, to make it much easier to roll out updates to machines. They’re all using the same base image, and this will get restored if things get broken. I’m using gPXE to boot the machines in the first place, and I’ve disabled NetBIOS over TCP/IP (to avoid the problem where Windows complains if two computers have the same name), and manually set the DNS search order (because when you boot Windows over iSCSI, Windows’ own DHCP client doesn’t get activated).

So I hacked together this VBScript that does a reverse DNS lookup on the IP, and uses that to get a specific wallpaper for the machine using a bit of copy-paste from the ‘net. The script requires you use JSWare’s JSSys addin for VBScript, so make sure you have that DLL installed and registered before trying to use my script. There’s a way to do it without JSWare using the registry, but it only applies after logging out and logging in again.

In the code it will try to grab the wallpaper from \\logres\wallpapers. You’ll need to change this for your setup. To activate this, I added it to the Startup folder, so about 5 seconds after login, the wallpaper is automatically changed to be for the correct machine.

One minor caveat: Apparently my network card was network card #2, and there was no #1. You can check what ID your card is in HKLM\Software\Microsoft\Windows NT\CurrentVersion\NetworkCards\, and change the line in the script appropriately.

' Automatically setup wallpaper (autowallpaper.vbs)Function GetFQDN(ipaddress)
  set sh = createobject("wscript.shell")
  set fso = createobject("scripting.filesystemobject")
  Set Env = sh.Environment("PROCESS")  workfile = fso.gettempname
  sh.run "%comspec% /c nslookup " & ipaddress & "  > " & workfile,0,true
  set sh = nothing
  set ts = fso.opentextfile(workfile)
  data = split(ts.readall,vbcr)
  ts.close  set ts = nothing
  fso.deletefile workfile
  set fso = nothing
  for n = 0 to ubound(data)
    if instr(data(n),"Name") then
      parts = split(data(n),":")
      hostname= trim(cstr(parts(1)))
      Exit For
    end if
    hostname = "could not resolve IP address"
  next

  GetFQDN = hostname
End FunctionConst HKCU = &H80000001
Const HKLM = &H80000002Set objReg = GetObject("winmgmts:\\.\root\default:StdRegProv")' Get NIC service name
objReg.GetStringValue HKLM, "Software\Microsoft\Windows NT\CurrentVersion\NetworkCards\2", "ServiceName", nicService' Get IP address
objReg.GetMultiStringValue HKLM, "System\CurrentControlSet\Services\TCPIP\Parameters\Interfaces\" & nicService, "IPAddress", addrs' Resolve name
fullname = GetFQDN(addrs(0))
shortname = split(fullname, ".")(0)Dim objOps
Set objOps = CreateObject("JSSys3.Ops")r = objOps.SetWallpaper("\\logres\wallpapers\" & shortname & ".bmp", "center")