Page 1 of 2 12>
Topic Options
#92709 - 2003-08-30 05:42 PM kform: Rename computers
Les Offline
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 $domainoptional $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
      $nametrim(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(10del $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
#92710 - 2003-08-30 07:00 PM Re: kform: Rename computers
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Doh! Forgot to mention who I borrowed the original form from. It has a striking resemblance to Remote Shutdown written by frak.

Thanks frak.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#92711 - 2003-08-30 08:39 PM Re: kform: Rename computers
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
OK, got to do a bit of testing...
My function wmiWhoIs() is not very truthful. It seems to report fine on most machines but I have a bunch of machines on the shop floor that AutoLogon at boot with TweakUI and this WMI function won't report who is logged on. The NETUSERS CL utility does report who is logged on.

Any ideas? All machines are Win2k.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#92712 - 2003-08-30 10:16 PM Re: kform: Rename computers
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Looks like I'm talking to myself. [Frown]

Gave up on the WMI stuff and went with good old NETUSERS from www.systemtools.com

[ 01. September 2003, 06:10: 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
#92713 - 2003-08-30 10:35 PM Re: kform: Rename computers
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Dang! missed a var.

$shellcmd = $netdom + $target + ' ' + $newnamesw + $newname + ' ' + $UserDsw + $UserOsw + $rebootsw
_________________________
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 Offline
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)
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#92715 - 2003-08-30 11:09 PM Re: kform: Rename computers
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
your piping to a temp file can be handled nicely with:

my version of ReadFile(): http://www.kixtart.org/board/ultimatebb.php?ubb=get_topic;f=12;t=000321

and collecting the data from it with: FilterArray(): http://www.kixtart.org/board/ultimatebb.php?ubb=get_topic;f=12;t=000453
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#92716 - 2003-08-30 11:12 PM Re: kform: Rename computers
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Does WMIQuery() not pull from the same place as my wmiWhoIs() UDF? I found my UDF to be unreliable but the NETUSERS CL utility works just fine for what I need.

BTW, my form has passed the theory stage and has actually renamed computers. [Big Grin]

Next week it goes through the real test when my co-workers run it through their paces. [Roll Eyes]
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#92717 - 2003-08-30 11:38 PM Re: kform: Rename computers
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
there is, am sure of it, easier way to determine remote logged on user.

once I get this another system running next to me, will be able to check without VPN...
_________________________
!

download KiXnet

Top
#92718 - 2003-08-30 11:41 PM Re: kform: Rename computers
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
heh, got some beer in it...
can't boot it before it dries up [Big Grin]
_________________________
!

download KiXnet

Top
#92719 - 2003-09-01 01:29 AM Re: kform: Rename computers
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
OK, cleaned it up a bit.
Posted version 1.1

[ 01. September 2003, 06:12: 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
#92720 - 2003-09-01 02:49 AM Re: kform: Rename computers
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
nicey.
_________________________
!

download KiXnet

Top
#92721 - 2003-09-01 06:16 AM Re: kform: Rename computers
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Was not happy with it so went back to the drawing board.

Posted version 2. It now has a history log, removes renamed computernames from the listbox, and allows for the admin account/password to be typed rather than hard-coded.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#92722 - 2003-09-01 09:20 AM Re: kform: Rename computers
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11628
Loc: CA
Does this work?

http://www.kixtart.org/cgi-bin/ultimatebb.cgi?ubb=get_topic;f=12;t=000262

Top
#92723 - 2003-09-01 10:19 AM Re: kform: Rename computers
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
heh, it might...
like said, the solution can't be difficult.
we just need to discover it [Wink]
_________________________
!

download KiXnet

Top
#92724 - 2003-09-01 02:35 PM Re: kform: Rename computers
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
DOC,
I have a lot of computers that autologon and may not get rebooted for a year. I tried the WMI method on them but it was not reliable. NETUSERS works every time.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#92725 - 2003-09-01 03:41 PM Re: kform: Rename computers
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Dang! Typo in the rewrite. [Embarrassed]


      $UserOsw = '/usero:' + $domain + '\' + $account +' /passwordo:' + $password + ' '

_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#92726 - 2003-09-15 11:25 PM Re: kform: Rename computers
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
There's also fnWMILoggedIn() - Retrieves users/computers connected to a computer for both local/remote user lists.
_________________________
There are two types of vessels, submarines and targets.

Top
#92727 - 2005-07-14 12:37 AM Re: kform: Rename computers
NTDOC Administrator Offline
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 Offline
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
Page 1 of 2 12>


Moderator:  Shawn, ShaneEP, Ruud van Velsen, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 1003 anonymous users online.
Newest Members
StuTheCoder, M_Moore, BeeEm, min_seow, Audio
17884 Registered Users

Generated in 0.074 seconds in which 0.027 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org