[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 563
  • Last Modified:

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
0
jarmo666
Asked:
jarmo666
  • 6
  • 5
  • 4
1 Solution
 
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
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
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
 
merowingerCommented:
@luconsta: It seemes that the homedrives are already created with shortnames
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now