01-29-2021 01:53 AM
Hello everybody.
I am trying to obtain info about the user running my script so about a logged-in user. I wasn't able to find a way how to do it directly in DIAdem script but I found neat code for Windows VBS I guess (I am still kind of new in scripting so I'm not sure what typ of code it is):
' *************************************************************************** ' UserName_SurName.vbs ' Gets the current user's first name and last name ' *************************************************************************** Set objSysInfo = CreateObject("ADSystemInfo") Set objCurrentUser = GetObject("LDAP://" & objSysInfo.UserName) WScript.Echo objCurrentUser.givenName WScript.Echo objcurrentuser.lastname
WScript.Echo objcurrentuser.department
But I wasn't able to obtain my desired info even though I tried to substitute WScript.Echo with MsgBox and whatnot.
I then tried to run the code externally from DIAdem like this:
dim myShell
set myShell = CreateObject("Wscript.Shell")
dim shellExec
set shellExec = myShell.Run("C:\Users\MyUserName\Desktop\UserName_SurName.vbs")
do while shellExec.Status = 0 '0: task is still running
call pause(0.1)
loop
if shellExec.Status=1 then '1: task has finished
msgbox shellExec.stdout.ReadAll
end if
where UserName_SurName.vbs is the first code saved as .vbs on my desktop. This time again without a success.
Could you please help me to obtain the user name, surname and department either directly from DIAdem script or from the script I posted? What do I do wrong.
Thank you in advance.
Marek
Solved! Go to Solution.
01-29-2021 10:49 AM
There are two ways to figure out a user using the LDAP or just read the environment variable.
If the environment variable fulfills your needs I would pick this one because it will always be fast.
Option Explicit
dim username, email
call GetUserNameAndMail(username, email)
MsgBox username & VBCRLF & email
MsgBox createObject("WScript.Shell").Environment("PROCESS").Item("USERNAME")
public sub GetUserNameAndMail(ByRef userName, byref userEMail)
userName = vbEmpty
userEMail = vbEmpty
dim objUser : Set objUser = GetObject("LDAP://" & CreateObject("ADSystemInfo").UserName)
call objUser.GetInfoEx(Array("Mail", "samAccountName"), 0) ' cache two attributes to speed up
userName = objUser.samAccountName
userEMail = objUser.mail
end sub
02-01-2021 03:19 AM
Hello AndreasK
Thank you very much for your post! It solved my troubles. I had to do slight changes to get what I needed nevertheless I couldn't do it without you.
dim myFirstName, mySurName, myDepartment
call GetUserNameAndDept(myFirstName, mySurName, myDepartment)
public sub GetUserNameAndDept(ByRef myFirstName, byref mySurName, byref myDepartment)
myFirstName = vbEmpty
mySurName = vbEmpty
myDepartment = vbEmpty
dim objUser : Set objUser = GetObject("LDAP://" & CreateObject("ADSystemInfo").UserName)
call objUser.GetInfoEx(Array("GivenName", "LastName", "Department"), 0) ' cache three attributes to speed up
myFirstName = objUser.GivenName
mySurName = objUser.LastName
myDepartment = objUser.Department
end sub
Marek