shmitchell22602
asked on
How to get vbs script to return current username
I need to write a script that will copy a file from a network share to the "C:\Documents and Settings\" & name "\Application Data\Microsoft\Templates\" folder. (name being the current user logged onto the machine). I have been unable to succefully pull the username. Any ideas?
You could just use the following command and the USERNAME environment variable:
copy "\\servername\share\dir\" "C:\Documents and Settings\%username%\Applic ation Data\Microsoft\Templates\"
copy "\\servername\share\dir\" "C:\Documents and Settings\%username%\Applic
Hi, you can also use the AppData environment variable from VBScript, shown in DOS by typing
echo %appdata%.
Set objShell = CreateObject("WScript.Shel l")
strFolder = objShell.ExpandEnvironment Strings("% APPDATA%") & "\Microsoft\Templates\"
MsgBox strFolder
Regards,
Rob.
echo %appdata%.
Set objShell = CreateObject("WScript.Shel
strFolder = objShell.ExpandEnvironment
MsgBox strFolder
Regards,
Rob.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
FYI: the code sited above that uses the "MyText" variable didn't work for me.
I had a slightly different task in mind in looking at this post: I needed to create a VB script for a user so they can delete their Java cache on one of our terminal servers. Through the power of Google, I found this bit of code that can be used to define the UserName variable:
Set objWshShell = WScript.CreateObject("WScr ipt.Shell" )
UserName = objWshShell.Environment("P ROCESS")(" UserName")
Then, I added those 2 lines & the UserName variable to my script to delete all subfolders of the Java Cache folder located at: "C:\Documents and Settings\%UserName%\Applic ation Data\Sun\Java\Deployment\c ache":
************************** ********** ********** ********** *****
Set objWshShell = WScript.CreateObject("WScr ipt.Shell" )
UserName = objWshShell.Environment("P ROCESS")(" UserName")
Profile = "c:\Documents and Settings\"
Set fs = CreateObject("Scripting.Fi leSystemOb ject")
set RootFld = fs.GetFolder(Profile & UserName & "\Application Data\Sun\Java\Deployment\c ache\")
Set oSub = RootFld.SubFolders
For Each cSubFld In oSub
Set oSub2 = fs.GetFolder(Profile & UserName & "\Application Data\Sun\Java\Deployment\c ache\" & cSubFld.Name)
oSub2.Delete
Next
For Each cFile In RootFld.Files
cFile.Delete
Next
wscript.echo "Your Java cache folder has been cleared."
************************** ********** ********** ********** *****
What I came up worked perfectly for me & I will put a copy out there for users who are having issues with Java-based software.
I had a slightly different task in mind in looking at this post: I needed to create a VB script for a user so they can delete their Java cache on one of our terminal servers. Through the power of Google, I found this bit of code that can be used to define the UserName variable:
Set objWshShell = WScript.CreateObject("WScr
UserName = objWshShell.Environment("P
Then, I added those 2 lines & the UserName variable to my script to delete all subfolders of the Java Cache folder located at: "C:\Documents and Settings\%UserName%\Applic
**************************
Set objWshShell = WScript.CreateObject("WScr
UserName = objWshShell.Environment("P
Profile = "c:\Documents and Settings\"
Set fs = CreateObject("Scripting.Fi
set RootFld = fs.GetFolder(Profile & UserName & "\Application Data\Sun\Java\Deployment\c
Set oSub = RootFld.SubFolders
For Each cSubFld In oSub
Set oSub2 = fs.GetFolder(Profile & UserName & "\Application Data\Sun\Java\Deployment\c
oSub2.Delete
Next
For Each cFile In RootFld.Files
cFile.Delete
Next
wscript.echo "Your Java cache folder has been cleared."
**************************
What I came up worked perfectly for me & I will put a copy out there for users who are having issues with Java-based software.
From command line you can access the WMI -
wmic /node:NODENAME computersystem get username
wmic /node:NODENAME computersystem get username
The solution that worked for me was in:
https://www.experts-exchange.com/questions/23766279/How-to-get-the-currently-logged-in-users-Active-Directory-Attribute.html
Set objSysInfo = CreateObject("ADSystemInfo ")
Set objUser = GetObject("LDAP://" & objSysInfo.UserName)
WScript.Echo objUser.sAMAccountName
To get a listing of all the attributes, use a commandline tool called adfind
http://www.joeware.net/freetools/
https://www.experts-exchange.com/questions/23766279/How-to-get-the-currently-logged-in-users-Active-Directory-Attribute.html
Set objSysInfo = CreateObject("ADSystemInfo
Set objUser = GetObject("LDAP://" & objSysInfo.UserName)
WScript.Echo objUser.sAMAccountName
To get a listing of all the attributes, use a commandline tool called adfind
http://www.joeware.net/freetools/
Option Explicit
Dim Text, Title
Dim WshNetwork ' Object variable
Text = "Networking information" & vbCrLf & vbCrLf
Title = "WSH sample - by G. Born"
' Create a new WshNetwork object to access network properties.
Set WshNetwork = WScript.CreateObject("WScr
Text = Text & "Computer name : " & WshNetwork.ComputerName & vbCrLf
Text = Text & "Domain : " & WshNetwork.UserDomain & vbCrLf
Text = Text & "User name : " & WshNetwork.UserName & vbCrLf
MsgBox Text, vbOKOnly + vbInformation, Title