RegEx.IgnoreCase
(1)
Matches.count
(1)
RegExpPos
(1)
RegEx.Pattern
(1)
RegEx.Execute
(1)
RegEx.Global
(1)
FirstIndex
(1)
SUserName
(1)

Generate usernames from Hostnames.

Asked By sudhi_shrivatsa
13-Nov-09 08:36 AM
Hi ,

I want to generate username from the hostname.
For example if the hostname is "test2345" then username should be "user2345".

where "user" is common for all the servers.

If the hostname is "test0123" it should omit 0 and only take the later part.
It will be something like this then "user234".

Can i achive this from a VBSCript. If yes please let me know how.

Thanks in advance.
Sudhi....

You need to nail down your requirements in greater detail.

Pegasus [MVP] replied to sudhi_shrivatsa
13-Nov-09 08:53 AM
You need to nail down your requirements in greater detail. According to your
post the following interpretations are possible:
a) Remove the first four characters from the host name and replace it with
the word "user".
b) Remove all leading non-numeric characters and replace them with the word
c) Take the last four characters and put the word "user" in front.
d) Take the trailing digits and put the word "user" in front.
e) In all cases suppress the first number if it is a 0.
f) In all cases, suppress all leading 0s except the last one.
g) In all cases, suppress all leading 0s.
I also wonder about the wisdom of the scheme. If you have two users using
one machine then you would get identical user names . . . Time to put on the
thinking cap!

Pefasus your understanding is correct. I cant help you on wisdom of thescheme.

sudhi_shrivatsa replied to Pegasus [MVP]
13-Nov-09 10:07 AM
Pefasus your understanding is correct. I cant help you on wisdom of the
scheme. Its is already set here.  The admin account on these are set like
this. 200 Machines one common admin password but admin username is derived
from the hostname. with following criteria.
eg
Hostname = file456
Admin user = user456
password = common123
------------------------------------------------------------------
Hostname = file1456
Admin user = user1456
password = common123
------------------------------------------------------------------
Hostname = file0456
Admin user = user456
password = common123
-------------------------------------------------------------------
a) Remove all leading non-numeric characters and replace them with the word
b)In all cases, suppress all leading 0s.

All are local admins on the servers. Hope this information adds some more
light.

Now I have to run a script from one of these machine which will fetch the
server names from one text file and logon remotely and run a specific command
add the output to one text file. Rest all is done but i am stuck at
generating username from the server name.

Let me know if you need any more info on this.

Thanks
Sudhi...

kedrdmandryourithwordngn theIf you are just looking for the string

Tom Lavedas replied to sudhi_shrivatsa
13-Nov-09 05:41 PM
ke
d
rd
mand
r
your
ith
word
ng
n the

If you are just looking for the string manipulation part of the
problem, a regular expression can be used, something like this (in
VBS) ...

sHostName =3D "file0123"
' Locate first occurrence of a digit other than zero
nPos =3D RegExpPos("[1-9]", sHostName)
if nPos > 0 and nPos <=3D Len(sHostName) then
sUserName =3D "user" & Mid(sHostName, nPos)
wsh.echo sHostName, "=3D>", sUserName
else
wsh.echo "Unable to parse Hostname", sHostName
end if

Function RegExpPos(patrn, strng)
Tom,You are close.It should only remove the first zero from the numeric part.
sudhi_shrivatsa replied to Tom Lavedas
13-Nov-09 11:23 AM
Tom,

You are close.
It should only remove the first zero from the numeric part.

Eg. file0123
username = user123
----------------------------------------------------
file00123
username = user123.
-------------------------------------------------
file1023
username = user1023.
-----------------------------------------------------------

Hope this is good now.
Sudhi...
OK, so multiple zeros ARE a problem.
Tom Lavedas replied to sudhi_shrivatsa
13-Nov-09 05:41 PM
OK, so multiple zeros ARE a problem.  It turns out the solution was a
lot easier than I first imagined or I might have addressed before.

Try this, instead ...

sHostName =3D "file00123"
nPos =3D RegExpPos("\d", sHostName)
if nPos > 0 and nPos <=3D Len(sHostName) then
sUserName =3D "user" & Mid(sHostName, nPos)
wsh.echo sHostName, "=3D>", sUserName
else
wsh.echo "Unable to parse Hostname", sHostName
end if

Function RegExpPos(patrn, strng)
Dim regEx, Match, Matches, RetPos ' Declare variables.
Set regEx =3D New RegExp   ' Create regular expression.
regEx.Pattern =3D patrn    ' Set pattern.
regEx.IgnoreCase =3D True  ' Set case insensitivity.
regEx.Global =3D false     ' Set non-global applicability.
Set Matches =3D regEx.Execute(strng)   ' Execute search.
if Matches.count > 0 then
RetPos =3D Matches(0).FirstIndex + 1
if Matches(0).Value =3D "0" then RetPos =3D RetPos + 1
else
RetPos =3D 0
end if
RegExpPos =3D RetPos
End Function

BTW, the \d pattern is shorthand for [0-9] - match all digits.
_____________________
Tom Lavedas
Post Question To EggHeadCafe