# setMyComputer.pl changes desktop 'My Computer' icon label to computer name # and current user name via the registry, per tip in Windows 2000 Magazine # Security UPDATE, May 24, 2000 (from http://www.windows2000faq.com). # # Written 25-may-2000 by Frank Brown. # Mod 07-jun-2000, fgb: run OS-specific code for NT 4.0 or W2K, based on # Windows version on target machine (determined by custom module rmtVer). # # usage: setMyComputer [machinelist.fil] [/v] [/deb] # # Notes: # ----- # 1. Original version works on NT 4.0 machines only! # 2. Changes registry entry type from REG_SZ to REG_EXPAND_SZ. # # Nothin' but perl, baby! require "ctime.pl"; use Net::Ping; use Win32::Registry; use rmtVer; # evil globals: $bDebug = 0; $sAppName = "setMyComputer"; $sDate = &ctime(time); chop $sDate; $hk; $sLocalMachine = Win32::NodeName; $sMachine = ""; $sMachineList = ""; $sNow = time(); $bSingleMachine = 0; $nTotalMachines = 0; $bVerbose = 0; $sVersion = "1.0"; MAIN: ParseCmdArgs(); print "$sAppName $sVersion starting at $sDate...\n"; if ($sMachineList eq "") { if (! $bSingleMachine) { $sMachine = $sLocalMachine; print "No list supplied; modifying local machine.\n"; # arf } if (-1 != &chRegVal()) { ++$nTotalMachines; } } else { open(FPCLIST,$sMachineList) || die "Can't open file $sMachineList: $!\n"; while () { if (!/^[\!\#;]/) { # skip comments chop $_; s/\s(.*)//; # remove whitespace s/^\\\\//; # remove leading '\\' $sMachine = $_; if (&IsVisibleOnNet()) { if (-1 != &chRegVal()) { ++$nTotalMachines; } } } } close(FPCLIST); } print "\n$sAppName: modified icon on $nTotalMachines machine"; if ($nTotalMachines != 1) { print "s."; } exit; # subroutines: sub doNT40 { local $sValName = ""; local $sNewVal = '%computername% / %username%'; if ($bDebug) { print "dbg: SetValueEx($sValName,0,REG_EXPAND_SZ,$sNewVal)\n"; } else { if (!$hk->SetValueEx($sValName,0,REG_EXPAND_SZ,$sNewVal)) { if ($bVerbose) { print "error setting value for $sValName\n"; } } else { print "set\n"; } } } sub doW2K { local $sValName = 'LocalizedString'; local $sNewVal = ""; local $sEnvStr = '%computername% / %username%'; if ($bDebug) { print "dbg: QueryValue($sValName,$sNewVal)\n"; } if (!$hk->QueryValueEx($sValName,REG_SZ,$sNewVal)) { if ($bVerbose) { print "error getting value of $sValName\n"; } return -1; } $sNewVal =~ s/My Computer/$sEnvStr/; if ($bDebug) { print "dbg: new val = $sNewVal\n"; } if (!$hk->SetValueEx($sValName,0,REG_EXPAND_SZ,$sNewVal)) { if ($bVerbose) { print "error setting value for $sValName\n"; } } else { print "set\n"; } } sub chRegVal { ### NT 4.0 only! local $sKey = "SOFTWARE\\Classes\\CLSID\\{20D04FE0-3AEA-1069-A2D8-08002B30309D}"; local $hkr; local $sVer = rmtVer::getRemoteInfo($sMachine); $sServer = "\\\\" . $sMachine; # used by Connect() print "Updating $sMachine..."; $HKR = $main::HKEY_LOCAL_MACHINE; unless ($HKR->Connect($sServer,$hkr)) { print "Can't connect to $sServer; Win95?\n"; return -1; } if (!$hkr->Open($sKey,$hk)) { print "error opening key $sKey\n"; return; } if ($sVer eq 'NT 4.0') { &doNT40(); } else { if ($sVer eq 'Win2000') { &doW2K(); } else { print "Can't run on PC running $sVer\n"; } } $hk->Close(); $hkr->Close(); } sub Help { printf("%s version %s\n",$sAppName,$sVersion); printf("\nusage: %s [machinelist.fil] [/v[erbose]]\n\n",$sAppName); print "machinelist.fil should be an ascii file with 1 machine per line.\n"; print "If no machinelist is specified, current machine is modified.\n"; } sub IsVisibleOnNet { $p = Net::Ping->new("icmp"); return ($p->ping($sMachine,1)); } sub ParseCmdArgs { while (@ARGV) { $_ = shift @ARGV; if ($bDebug) { print "dbg: parsing $_\n"; } if (/^[\/\-]/) { # is arg a switch? $arg = substr($_,1,3); # parse 1st 3 chars: $arg =~ s/[A-Z]/[a-z]/; if (($arg eq "hel") || (substr($arg,0,1) eq "?")) { &Help(); exit; } if (substr($arg,0,1) eq "v") { $bVerbose = 1; } if (($arg eq "deb")) { $bDebug = 1; } } else { # not a switch if (/^[\\]/) { # single machine (UNC) s/\s(.*)//; # remove whitespace s/^\\\\//; # remove leading \\ $sMachine = $_; $bSingleMachine = 1; } else { $sMachineList = $_; } } } }