Page 1 of 1 1
Topic Options
#133800 - 2005-02-15 10:24 AM Renaming workstation, need 5 digit placeholder and a case switch?
jechilt Offline
Starting to like KiXtart

Registered: 2000-12-01
Posts: 102
Loc: Denver Colorado
Our logon script is providing a local machine and selected services password change and then moves on to look at the computer workstation name. The name is checked against a MS Access Database. If the name is an old name, it gets changed to the new name using a shell command with NetDom.
No problems at the moment as it is working fine...

We found out that we have to alter our naming convention for a selected group of computers. This brings 2 problems. First is the prefix and secondly is the number. We have to use a 5 digit number. In the code below, hopefuly you will be able to see that we parse the machine name, change the number string to an integer, do a counter loop to increment the workstation number then piece it back together with the prefix for a total name:
Code:


; set connection string
$objConn = DBConnOpen('DRIVER={Microsoft Access Driver (*.mdb)}; UID=; PWD=; DBQ=$DBpath')
$recordset = DBGetRecordset($objConn,"SELECT ws_oldName FROM ws WHERE ws_oldName = '@wksta'")
$recordset1 = DBGetRecordset($objConn,"SELECT ws_newName FROM ws ORDER BY ws_newName")
$recordset2 = DBGetRecordset($objConn,"SELECT ws_newName FROM ws WHERE ws_oldName = '@WKSTA'")
If $recordset[0] = @WKSTA
? 'Record for @WKSTA is listed as an old name! Proceed to change machine name'
$ws_rename = $recordset2[0]
Else
? 'No Record Found for @WKSTA! Proceed to Add New Record'
; got to get the last machine name number to increment
$highNum = 0
For Each $record1 In $recordset1
;$recordsetfields=Split($record,Chr(10))
$parseName = Left('$record1',6)
; ? '$parseName' ;for debuging
If $parseName = "NSJNPW"
$parseNum = Right('$record1',5)
; ? '$highNum' ;for debuging
If '$parseNum' >= '$highNum'
$highNum = '$parseNum'
EndIf
EndIf
Next
$wsNewName = CInt($highNum)
$wsNewName = $wsNewName + 1
If $wsNewName < '9999'
? 'Verifying process with naming convention: $parseName @CRLF'
$parseName = '$parseName'+'0'
$wsNewName = '$parseName$wsNewName'
Else
$wsNewName = '$parseName$wsNewName'
EndIf
? 'Process complete. New machine name number is: $wsNewName @CRLF'
? 'The new machine name will be: $wsNewName @CRLF



We now need to add NSSFNW to the naming convetion. I am thinking a switch will do the job for that. However, when we go to NSSFNW, the workstation count needs to start at 00001 and increment from there. I am not sure how to get the number to hold 5 places.
I did cheat when I did the NSNCPW. We arleady have over 1000 workstations so I added a 0 to the name and then just used the counter to add one.
Can anyone provide some suggestions?

Thanks!
_________________________
John
LM Contractor
One of the 2 dads

Top
#133801 - 2005-02-15 10:45 AM Re: Renaming workstation, need 5 digit placeholder and a case switch?
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
Code:

for $numName = 0 to 16

$strName=""+$numName
while len($strName)<5
$strName="0"+$strName
loop

? $strName
next

_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#133802 - 2005-02-15 11:09 AM Re: Renaming workstation, need 5 digit placeholder and a case switch?
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
Code:

select
case $numName < 10 $strName="0000"+$numName
case $numName < 100 $strName="000" +$numName
case $numName < 1000 $strName="00" +$numName
case $numName < 10000 $strName="0" +$numName
endselect

_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#133803 - 2005-02-15 11:13 AM Re: Renaming workstation, need 5 digit placeholder and a case switch?
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
No need to make it that complicated:
Code:
$sZeroFilled=Right("00000"+$iNumber,5)


Top
#133804 - 2005-02-15 11:43 AM Re: Renaming workstation, need 5 digit placeholder and a case switch?
jechilt Offline
Starting to like KiXtart

Registered: 2000-12-01
Posts: 102
Loc: Denver Colorado
Quote:

No need to make it that complicated:
Code:
$sZeroFilled=Right("00000"+$iNumber,5)






simple and effective. i wish i would have known more about the right().

Do you think a case is the best way to go with the prefix for seperation? I have a hunch we are going to change several machine names prefix, depending on the command (military) that owns them.
_________________________
John
LM Contractor
One of the 2 dads

Top
#133805 - 2005-02-15 03:53 PM Re: Renaming workstation, need 5 digit placeholder and a case switch?
jechilt Offline
Starting to like KiXtart

Registered: 2000-12-01
Posts: 102
Loc: Denver Colorado
well, i must not be smart enough to figure this out on my own...yet.

The system is working with the add 1 on the number but here is the new challenge and I can't figure it out.
Let's say that the number goes from 00001, 00002, 00003, 00005, 00006, etc.
The way the script is running through the record set, it is telling us that 00006 is the highest number and the new assigned number will be 00007.
We want to catch 00004 and assign the machine to that number. This way we won't have huge gaps in the numbering sequence.
I looked at using an array instead of an if statement but cant figure out how to get the 00004 value.
Could you shed some light on this???
_________________________
John
LM Contractor
One of the 2 dads

Top
#133806 - 2005-02-15 04:01 PM Re: Renaming workstation, need 5 digit placeholder and a case switch?
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Well... maybe you need to explain yourself better cuz I am not sure I understand.

The value of 00004 was known to you before you converted it to a string by padding with zeros so you should already know its value. Otherwwise, you can use Val() to get it again.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#133807 - 2005-02-15 04:15 PM Re: Renaming workstation, need 5 digit placeholder and a case switch?
jechilt Offline
Starting to like KiXtart

Registered: 2000-12-01
Posts: 102
Loc: Denver Colorado
Quote:

Well... maybe you need to explain yourself better cuz I am not sure I understand.

The value of 00004 was known to you before you converted it to a string by padding with zeros so you should already know its value. Otherwwise, you can use Val() to get it again.




I am terrible at explaining so let me give it another go

Looking at the first post, the code runs through the recordset. In the process it parses the record and we then look at the numeric piece
Code:
 Right($record1,5) 


Next we use the if statement to find the high number when the records are being run. I don't really know what the record information is except a bunch of numbers.
When we built the database, it was populated with all of our workstations old names and they were assigned new names. The script is looking at this database to get the new name. The problem I found was the new name of approximately 200 "unknown old name" computer need a different naming convention and have to start at 00001. So, what I am attempting to do is to avoid creating a new record for the existing workstation entry. First, we identify the workstation and then we give it the proper prefix and then we have to give it the numeric value. We know that some computer names have been assigned with a number like 00005. Using the script as it shows in the first post, the next assigned number will be 00006. While this is a true statement, we want it to be assigned to the first free number (hypothetical) of 00004. So, I have been trying to figure out how to capture the low free number and high free number. If the low number exists or matches the high number, use it.
I hope this helps a little...
_________________________
John
LM Contractor
One of the 2 dads

Top
#133808 - 2005-02-15 04:16 PM Re: Renaming workstation, need 5 digit placeholder and a case switch?
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Well, you already sort the records by the computername, so just compare each record to the previous one. If they differ by more than one then there is a gap.

Here is one way of doing it:
Code:
$highNum = 0

For Each $record1 In $recordset1
If Not $highNum
$parseNum=CInt(Right($record1,5))
If $iNextNum AND $iNextNum<>$parseNum
$highNum=$iNextNum
"Gap found - using sequence number "+$highNum ?
Else
$iNextNum=$parseNum+1
EndIf
EndIf
Next
If Not $highNum $highNum=$iNextNum+1 EndIf



[edit]
Replaced the "EndIf" lost on the last line of the code during cut'n'paste.
[/edit]


Edited by Richard H. (2005-02-16 09:22 AM)

Top
#133809 - 2005-02-16 12:37 AM Re: Renaming workstation, need 5 digit placeholder and a case switch?
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11164
Loc: Boston, MA, USA
You could use a SQL statement to identify missing sequence numbers by basically performing a self-join against itself and using the sequence number as paramerter, e.g.
Code:

select a.seq
from table_a as a
full join table_a as b
on a.seq=b.seq-1
where b.seq is null



But why is this done in a login script and not via central admin script? Renaming workstations requires admin privs and a reboot anyway.
_________________________
There are two types of vessels, submarines and targets.

Top
#133810 - 2005-02-16 08:48 AM Re: Renaming workstation, need 5 digit placeholder and a case switch?
jechilt Offline
Starting to like KiXtart

Registered: 2000-12-01
Posts: 102
Loc: Denver Colorado
Quote:

Well, you already sort the records by the computername, so just compare each record to the previous one. If they differ by more than one then there is a gap.

Here is one way of doing it:
Code:
$highNum = 0
For Each $record1 In $recordset1
If Not $highNum
$parseNum=CInt(Right($record1,5))
If $iNextNum AND $iNextNum<>$parseNum
$highNum=$iNextNum
"Gap found - using sequence number "+$highNum ?
Else
$iNextNum=$parseNum+1
EndIf
EndIf
Next
If Not $highNum $highNum=$iNextNum+1






Hi Richard,
Thanks for this example. It works perfectly!
_________________________
John
LM Contractor
One of the 2 dads

Top
#133811 - 2005-02-16 08:51 AM Re: Renaming workstation, need 5 digit placeholder and a case switch?
jechilt Offline
Starting to like KiXtart

Registered: 2000-12-01
Posts: 102
Loc: Denver Colorado
Quote:


But why is this done in a login script and not via central admin script? Renaming workstations requires admin privs and a reboot anyway.




This process is being done with the login script for our technician group only. Putting it in the login script, our techs log in and the script executes (matter of convenience and time savings). There are other requirements involved in our process that requires the tech to log on to every workstation on the installation.
_________________________
John
LM Contractor
One of the 2 dads

Top
Page 1 of 1 1


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

Who's Online
2 registered (morganw, mole) and 414 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.061 seconds in which 0.022 seconds were spent on a total of 12 queries. Zlib compression enabled.

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