Jump to content

Welcome to eMastercam

Register now to participate in the forums, access the download area, buy Mastercam training materials, post processors and more. This message will be removed once you have signed in.

Use your display name or email address to sign in:

Get Username in VB?


Recommended Posts

quote:

Is it possible to get the logged on user's name with VBS?


Yes, consider something like this:

 

code:

' Kick off the script

Call Main()

 

 

' Purpose: the main subroutine

Sub Main()

Dim objNetwork ' Network object

Dim strUserName ' current user name

 

 

' Retrieve the username of the person currently logged into the

' computer. Greet the person with their username.

Set objNetwork = WScript.CreateObject("WScript.Network")

strUserName = objNetwork.UserName

 

ShowString "Hi there! You're logged in as " & strUserName

 

 

' Cleanup

Set objNetwork = Nothing

End Sub

It uses the Network functions available to the Windows Scripting Host. You could use WMI instead, but people not running WinXP would have to install WMI in order for it to work. Using the Network WSH stuff is almost guaranteed to work on all Windows versions.

 

HTH

Link to comment
Share on other sites

Glad to hear that it works. Good thing you didn't have to resort to WMI. It's powerful, but ugly. Here's what a WMI version of the same script might look like:

 

code:

' Kick off the script

Call Main()

 

' Purpose: the main subroutine

Sub Main()

Dim strComputer ' computer name to query

Dim ojbWMI ' WMI object

Dim colResult ' results from the query

Dim strUserName ' current user name

 

 

' This computer.

strComputer = "."

 

' Retrieve the username of the person currently logged into the

' computer. Greet the person with their username.

Set objWMI = GetObject("winmgmts:" _

& "{impersonationLevel=impersonate}!" _

& strComputer & "rootcimv2")

 

Set colResult = objWMI.ExecQuery("Select * from Win32_ComputerSystem")

 

' There _should_ only be one.

For Each objComputer in colResult

strUserName = objComputer.UserName

Next

 

ShowString "Hi there! You're logged in as " & strUserName & "."

 

' Cleanup

Set objWMI = Nothing

Set colResult = Nothing

End Sub

Nasty, eh? The only benefit is that it'll display the workgroup/domain associated with the username.

 

cheers.gif

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.

Join us!

eMastercam - your online source for all things Mastercam.

Together, we are the strongest Mastercam community on the web with over 56,000 members, and our online store offers a wide selection of training materials for all applications and skill levels.

Follow us

×
×
  • Create New...