jechilt
(Starting to like KiXtart)
2005-02-15 10:24 AM
Renaming workstation, need 5 digit placeholder and a case switch?

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!


Radimus
(KiX Supporter)
2005-02-15 10:45 AM
Re: Renaming workstation, need 5 digit placeholder and a case switch?

Code:

for $numName = 0 to 16

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

? $strName
next



Radimus
(KiX Supporter)
2005-02-15 11:09 AM
Re: Renaming workstation, need 5 digit placeholder and a case switch?

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



Richard H.Administrator
(KiX Supporter)
2005-02-15 11:13 AM
Re: Renaming workstation, need 5 digit placeholder and a case switch?

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



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

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.


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

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???


Les
(KiX Master)
2005-02-15 04:01 PM
Re: Renaming workstation, need 5 digit placeholder and a case switch?

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.


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

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...


Richard H.Administrator
(KiX Supporter)
2005-02-15 04:16 PM
Re: Renaming workstation, need 5 digit placeholder and a case switch?

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]


Sealeopard
(KiX Master)
2005-02-16 12:37 AM
Re: Renaming workstation, need 5 digit placeholder and a case switch?

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.


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

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!


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

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.