#92709 - 2003-08-30 05:42 PM
kform: Rename computers
|
Les
KiX Master
   
Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
|
Well guys... here is my first stab at doing something in kforms... please don't laugh. I know it's not perfect cuz I am really lazy. It is still a work in progress but am eager to get this on the board. There is still stuff commented out and code to echo back debugging info.
There are lots of dependencies (NETDOM5, NETUSERS) and a bit of hard coding (FF domain).
It was written for my unique requirement. I have a whole bunch of computers named 999-51-xxx where xxx is unique. To migrate from NT4 to AD, I have to prefix them all with my OU (FF) so, 999-51-123 would become FF0123. I snuck in a leading zero so that when we get to 999, it will still sort well.
It uses Jens' NetView2() UDF to browse for machines that are likely to be turned on. First you ping with Jochen's Ping() UDF, then check to see if you can connect to the C$ share and finally see if someone is logged on with NETUSERS. If nobody is logged on, rename, else send them a message. If/when they call, have them logoff and then rename.
Here is the code (tested and revised)
; Script to rename remote machines ; Requires Kixtart 4.x, Kixforms 2.3.0.38 ; by Les Ligetfalvy break on dim $label,$frame1,$frame2,$label2,$cmd1,$cmd2,$cmd3,$cmd4,$cmd5,$ Global $form,$account,$password,$domain,$Text4,$Text5,$Text6,$form2,$list1,$list2,$label1 $ = setoption('explicit','on')
$Form = CreateObject('Kixtart.Form') $Form.Caption = 'Workstation Rename' $Form.Width = 420 $Form.Height = 500 $Form.center
$label = $Form.Label('Note that only machines that have been on for a while will show in the list.' + @CRLF + 'Renamed computers will not list by their new name right away, even with a rescan,' + @CRLF + 'and old computer names will reappear if the domain is rescanned.',10,10,400,40)
$Frame1 = $Form.Frame('Select Computer',10,55,240,337) $Frame2 = $Form.Frame('Action Steps',260,55,145,337)
$label1 = $Form.Label('Domain Name: - $domain',40,75,120,25) $list1 = $Form.Listbox(,20,100,220,281)
$cmd1 = $Form.CommandButton('&Check Status',280,190) $cmd1.onclick = 'onbutton1click() $$list1.setfocus'
$cmd2 = $Form.CommandButton('Send &Message',280,230) $cmd2.onclick = 'onbutton2click() $$list1.setfocus'
$label2 = $Form.Label('1: Select Domain' + @CRLF + @CRLF + '2: Check computer status' + @CRLF + @CRLF + '3: If a user is logged on,' + @CRLF + ' send them a message' + @CRLF + @CRLF + '4: Rename the computer',270,75,160,110)
$cmd3 = $Form.CommandButton('&Rename',280,270) $cmd3.onclick = 'onbutton3click() $$list1.setfocus'
$cmd4 = $Form.CommandButton('&Select Domain',280,310) $cmd4.onclick = 'onbutton4click() $$list1.setfocus'
;for each $computer in netview2($domain,1) ; $list1.additem($computer) ;next
$cmd5 = $Form.CommandButton('E&xit',280,350) $cmd5.onclick = 'onbutton5click() $$list1.setfocus'
$list2 = $Form.Listbox(,9,400,395,60)
$Form.Show $list1.setfocus
While $Form.Visible $=Execute($Form.DoEvents()) Loop
FUNCTION onbutton1click ;Check Status dim $reachable,$WhoIs if $list1.text = -1 $list2.additem(@time + ' - NO COMPUTER SELECTED, NOTHING TO STATUS CHECK!',0) exit 1 endif $form.mousepointer = 11 if ping(split($list1.text,',')[0],0,1) $reachable = '\\' + split($list1.text,',')[0] + '\C$$' if not exist($reachable) $list2.additem(@time + ' - ' + split($list1.text,',')[0] + ' - No Rights or Renamed!',0) beep $form.mousepointer = 1 exit 1 $list1.setfocus else $WhoIs = NetUsers(split($list1.text,',')[0]) if $WhoIs $list2.additem(@time + ' - ' + split($list1.text,',')[0] + ' - ' + $WhoIs + ' - logged on!',0) else $list2.additem(@time + ' - ' + split($list1.text,',')[0] + ' - user logged off!',0) endif endif else $list2.additem(@time + ' - ' + split($list1.text,',')[0] + ' - Unreachable!',0) endif $form.mousepointer = 1 ENDFUNCTION
FUNCTION onbutton2click ;Send Message dim $Recipient,$body1,$message if $list1.text = -1 $list2.additem(@time + ' - NO COMPUTER SELECTED, NOTHING TO SEND TO!',0) exit 1 endif $Recipient = split($list1.text,',')[0] $body1 = 'Enter Message to send to ' + $Recipient $message = 'Please call Les at x1846' $message = $Form.Inputbox($body1,'Send Message',$message) if $message if sendmessage($Recipient,$message) $list2.additem(@time + ' - ' + split($list1.text,',')[0] + ' - Send message failed!',0) else $list2.additem(@time + ' - ' + split($list1.text,',')[0] + ' - Message sent OK',0) endif else $list2.additem(@time + ' - ' + split($list1.text,',')[0] + ' - Send message cancelled!',0) endif ENDFUNCTION
FUNCTION onbutton3click ;Rename dim $target,$NameSuffix,$NewNamePrefix,$newname,$netdom,$newnamesw,$UserDsw,$UserOsw,$rebootsw,$shellcmd,$selection select case $list1.text = -1 $list2.additem(@time + ' - NO COMPUTER SELECTED, NOTHING TO RENAME!',0) exit 1 case split(Left($list1.text,4),',')[0] = '999-' $target = split($list1.text,',')[0] $NameSuffix = split($target,'-')[2] $NewNamePrefix = 'FF0' $newname = $NewNamePrefix + $NameSuffix $netdom = 'netdom renamecomputer ' $newnamesw = '/newname:' $UserDsw = '/userd:' + $domain + '\' + $account +' /passwordd:' + $password + ' ' $UserOsw = '/usero:' + $domain + '\' + $account +' /passwordo:' + $password + ' ' $rebootsw = '/reboot:60' $shellcmd = $netdom + $target + ' ' + $newnamesw + $newname + ' ' + $UserDsw + $UserOsw + $rebootsw ; $selection = $form.msgbox('Are you sure you want to rename this Machine','Warning',4) $selection = $form.msgbox($shellcmd,'Warning',4) If $selection = 6 ; shell $shellcmd $list2.additem(@time + ' - ' + split($list1.text,',')[0] + ' - Renamed to: ' + $NewName,0) $list1.removeitem($list1.listindex) else $list2.additem(@time + ' - ' + split($list1.text,',')[0] + ' - RENAME CANCELLED!',0) endif case 1 $list2.additem(@time + ' - COMPUTER NAME DOES NOT START WITH 999') exit 1 endselect endfunction
FUNCTION onbutton4click ;Select Domain dim $label3,$savebtn,$label4,$label5,$label6,$ $Form2 = CreateObject('Kixtart.Form') $Form2.Caption = 'Domain and Account Settings' $Form2.Width = 300 $Form2.Height = 200 $Form2.center
$label3 = $Form2.Label('Enter an account with admin rights on both the domain and the workstations',10,10,290,30) $savebtn = $Form2.CommandButton('Save and return',100,130) $savebtn.onclick = 'onbutton6click()'
$label4 = $Form2.Label("Account:",30,42,48,15) $label4.alignment = 1 $Text4 = $Form2.Textbox('Administrator',80,40,90,20)
$label5 = $Form2.Label("Password:",30,72,48,15) $label5.alignment = 1 $label5.passwordchar="?" $Text5 = $Form2.Textbox($password,80,70,90,20) $Text5.passwordchar="*"
$label6 = $Form2.Label("Domain:",30,102,48,20) $label6.alignment = 1 $Text6 = $Form2.Textbox('FF',80,100,90,20)
$Form2.Show
While $Form2.Visible $=Execute($Form2.DoEvents()) Loop endfunction
FUNCTION onbutton5click ;Exit quit 1 endfunction
FUNCTION onbutton6click ;Exit dim $computer $account = $text4.text $password = $text5.text $domain = ucase($text6.text) $form2.mousepointer = 11 $list1.clear $list2.clear $label1.text = 'Domain Name :- $domain' for each $computer in netview2($domain,1) $list1.additem($computer) next $list2.additem(@time + ' - ' + 'Logging started') $form2.mousepointer = 1 $form2.hide endfunction
function NetView2(optional $domain, optional $commentflag) ;http://81.17.37.55/cgi-bin/ultimatebb.cgi?ubb=get_topic;f=12;t=000202 dim $array[255] dim $redim, $i, $j, $tempfile dim $filehandle, $retcode dim $name, $comment, $line $redim = 255 $i = 0 $j = 0 $tempfile = '%temp%\netview.tmp' if $domain $domain = '/domain:'+trim($domain) endif if vartype($commentflag) $commentflag=val($commentflag) else $commentflag=0 endif if exist($tempfile) del $tempfile endif shell '%comspec% /c net view $domain >"'+$tempfile+'"' if @error = 0 $filehandle=1 do $retcode=open($filehandle,$tempfile) $filehandle=$filehandle+1 until $retcode=0 or $filehandle=11 select case $retcode=-3 exit 4 case $retcode=-2 exit 6 case $retcode=-1 exit 123 case $retcode=0 $filehandle=$filehandle-1 case $retcode>0 exit @ERROR case $filehandle=11 exit 4 endselect for $j = 1 to 5 $line = readline($filehandle) ; skip headings next while @error = 0 $name= trim(substr($line,3,instr($line,' ')-1)) $comment = trim(right($line,len($line)-instr($line,' '))) if $commentflag $array[$i]=$name+','+$comment else $array[$i]=$name endif $i=$i+1 if $i = $redim $redim=$redim*2 redim preserve $array[$redim] endif $line = readline($filehandle) loop $retcode=close($filehandle) del $tempfile if $i redim preserve $array[$i-4] $netview2 = $array exit @error endif endif $netview2 = 0 exit @error endfunction
function Ping($Computer,$GetIP,optional $LoopCount,optional $TimeOut) dim $ip,$ipfile,$c if $GetIP $ipfile = %temp% + '\~ip.tmp' shell '%Comspec% /q /e:1024 /c for /F "tokens=2 delims=[]" %%i IN ('+ chr(39) + '"ping $Computer -n 1 | find "]""' + chr(39) + ') do echo %%i >' + $ipfile $ = open(10,$ipfile,2) $ip = readline(10) $ = close(10) del $ipfile if $ip $Ping = $ip else $Ping = 'Bad IP address $Computer !' endif exit else if $TimeOut for $c = 0 to $LoopCount shell '%Comspec% /C ping $Computer -n 1 -w $TimeOut | find /C "TTL=" > nul' if @error = 0 $Ping = 1 exit endif next else for $c = 0 to $LoopCount shell '%Comspec% /C ping $Computer | find /C "TTL=" > nul' if @error = 0 $Ping = 1 exit endif next endif $Ping = 0 endif endfunction
function NetUsers($computer) dim $tmpfile,$user,$ if $computer $tmpfile = %temp% + '\~netusers.tmp' shell '%Comspec% /q /c netusers \\$Computer | find "\" >' + $tmpfile $ = open(10,$tmpfile,2) $ = readline(10) $user = readline(10) $ = close(10) del $tmpfile if $user $NetUsers = split($user,' ')[0] endif exit 0 else $NetUsers = 0 endif endfunction
[ 01. September 2003, 15:43: Message edited by: LLigetfa ]
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.
|
|
Top
|
|
|
|
#92714 - 2003-08-30 11:04 PM
Re: kform: Rename computers
|
Radimus
Moderator
   
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
what is it that you need? the name of any logged on users?
WMIQuery does that fairly easily: $remoteuser=wmiquery("UserName","Win32_ComputerSystem",$Computer)
|
|
Top
|
|
|
|
#92715 - 2003-08-30 11:09 PM
Re: kform: Rename computers
|
Radimus
Moderator
   
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
|
|
Top
|
|
|
|
#92722 - 2003-09-01 09:20 AM
Re: kform: Rename computers
|
NTDOC
Administrator
   
Registered: 2000-07-28
Posts: 11628
Loc: CA
|
|
|
Top
|
|
|
|
#92727 - 2005-07-14 12:37 AM
Re: kform: Rename computers
|
NTDOC
Administrator
   
Registered: 2000-07-28
Posts: 11628
Loc: CA
|
And here is an image of how the form looks
|
|
Top
|
|
|
|
#92728 - 2005-07-20 10:32 AM
Re: kform: Rename computers
|
dangerous
Lurker
Registered: 2005-07-20
Posts: 2
|
Les,
what is this app and where can i get it from? I have to rename a multitude of win2k computers and finding a script to do it is like a needle in a haystack!! I can rename the ad object but need to rename the physical machine via script preferably, this can be done before they join new domain or after...whatever is gonna be easier...
Les, can you tell me where you posted ver 2.0 of your kixtart script please..
Thanks
Kevin
|
|
Top
|
|
|
|
Moderator: Shawn, ShaneEP, Ruud van Velsen, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Mart
|
0 registered
and 987 anonymous users online.
|
|
|