VBS to get username then create shortcut on user desktop

Hello,
I need a VBS code which will get user name of user who is actually loged into PC and create a shortcut on the user desktop. Path for the shortcut is \\servername\folder\shortusername

short username is just first character from the frst name and two characters from the last name.

so here is example:
John Smith -> JSM
jarmo666Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

luconstaCommented:
Try this:

Function GetCurrentUser()

    Dim objNetwork
    Dim sUser, sDomain
    Set objNetwork = CreateObject("Wscript.Network")
    
    'strComputer = objNetwork.ComputerName
    sUser = objNetwork.UserName
    sDomain = objNetwork.UserDomain
    
    GetCurrentUser = sDomain & "\" & sUser

    'Cleanup
    Set objNetwork = Nothing
End Function

Open in new window

0
luconstaCommented:
Sorry the above answer is incomplete - it will return only the user name.
0
luconstaCommented:
But combined with the code of bushmobile from VB - The simplest way to Create Shortcut you cand have a fully functional code:
 

Sub TestShortcut()
    Create_ShortCut "\\servername\folder\" & GetCurrentUser, "Desktop", GetCurrentUser & " Folder", "", , WshNormalFocus
End Sub

Private Sub Create_ShortCut(ByVal sTargetPath As String, ByVal sShortCutPath As String, ByVal sShortCutName As String, _
                            Optional ByVal sArguments As String, Optional ByVal sWorkPath As String, _
                            Optional ByVal eWinStyle As WshWindowStyle = vbNormalFocus, Optional ByVal iIconNum As Integer)
    ' Requires reference to Windows Script Host Object Model
    Dim oShell As IWshRuntimeLibrary.WshShell
    Dim oShortCut As IWshRuntimeLibrary.WshShortcut
    
    Set oShell = New IWshRuntimeLibrary.WshShell
    Set oShortCut = oShell.CreateShortcut(oShell.SpecialFolders(sShortCutPath) & _
                                          "\" & sShortCutName & ".lnk")
    With oShortCut
        .TargetPath = sTargetPath
        .Arguments = sArguments
        .WorkingDirectory = sWorkPath
        .WindowStyle = eWinStyle
        .IconLocation = sTargetPath & "," & iIconNum
        .Save
    End With
    
    Set oShortCut = Nothing: Set oShell = Nothing
End Sub

Function GetCurrentUser()

    Dim objNetwork
    Dim sUser, sDomain
    Set objNetwork = CreateObject("Wscript.Network")
    
    sUser = objNetwork.UserName
    sDomain = objNetwork.UserDomain
    
    GetCurrentUser = sUser

    'Cleanup
    Set objNetwork = Nothing
End Function

Open in new window

0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

merowingerCommented:
This should to it.
It's important that the username contains two words
set WshShell = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("Wscript.Network") 
strUser = objNetwork.UserName

strShortName = Ucase(Left(Split(strUser," ")(0),1) &Left(Split(strUser," ")(1),2))

set oShellLink = WshShell.CreateShortcut(WshShell.ExpandEnvironmentStrings("%USERPROFILE%") &"\Desktop\" &strUser &".lnk")
oShellLink.TargetPath = "\\servername\folder\" &strShortName
oShellLink.WindowStyle = 1
oShellLink.Description = "Homedrive"

oShellLink.Save

Open in new window

0
jarmo666Author Commented:
To:merowinger

I have this error message when i run the VBS code. See attached printscreen
errorVBS.JPG
0
merowingerCommented:
can you insert the following on line 4
Wscript.echo objNetwork.UserName
What' the result?
As i said if the username is just one word like "john" the script will fail. It's written to build the shortname out of two names like "John Smith"
If you want also one word tt work you have to say how it shell be
0
jarmo666Author Commented:
well seems that i get only singe name back. The problem is that we have long name in AD but windows return just the basic username not full name. I need sometime to find out which key i will use for establish the short name, then i'll come back
0
merowingerCommented:
what delivers the following command?

start->run->cmd->whoami
0
luconstaCommented:
You have long names in AD as LOGON name or as USER Name - First Name/Last Name?...
Type the following command at a command prompt:

SET USERNAME

you will see the variable named USERNAME with the user's logon name after the "=" sign.
0
jarmo666Author Commented:
I get just domainname\cermakja so no long name getting back, i don't see posibility how to dedicate short name from tihis oneword logon name because if user has more than 8 charaters lastname then it will not work anyway.
0
merowingerCommented:
could you please post the result of this script.
It will query AD for the fullusername of the local user name.

If i have the name convention of the result i can customize the script
Set WshShell = CreateObject("WScript.Shell") 
Set objNetwork = CreateObject("Wscript.Network")  
strUser = objNetwork.UserName
strDomain=objNetwork.UserDomain

Set objUser = GetObject("WinNT://" & strDomain)
objUser.Filter = Array("user")

For Each strUser In objUser
	If Instr(ucase(strUser.Name),ucase(strUser)) Then
		strFullName = strUser.FullName
	End If
Next 

WScript.Echo strFullName

Open in new window

0
jarmo666Author Commented:
just get attached error screen
vbserror.JPG
0
merowingerCommented:
oh ok try this. heop this will work
Set WshShell = CreateObject("WScript.Shell")  
Set objNetwork = CreateObject("Wscript.Network")   
strUser = objNetwork.UserName 
strDomain=objNetwork.UserDomain 
 
Set objUsers = GetObject("WinNT://" & strDomain) 
objUsers.Filter = Array("user") 
 
For Each objUser In objUsers 
        If Instr(ucase(objUser.Name),ucase(strUser)) Then 
                strFullName = objUser.FullName 
        End If 
Next  
 
WScript.Echo strFullName

Open in new window

0
luconstaCommented:
Well jarmo666 why you can't just use the logon name (that "only word") the name is UNIQUE this is guarateed by the AD and shouldn't be any problem using it...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
merowingerCommented:
@luconsta: It seemes that the homedrives are already created with shortnames
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.