Solved

VB Script and getting domain username

Posted on 2014-04-03
7
169 Views
Last Modified: 2014-05-16
I have an ini file that I'm using. In it are 2 lines, a SMB link to an application and a hostname identifier.

This is what the app.ini file looks like:

FilePath=\\server\application.exe
Hostname = Computer

I want to use a VB script to take the username of the person who’s logged in and replace ‘computer’ with that user’s domain username. This way I don't have to edit every single app.ini

This is my script, when I double click it, it runs fine but it doesn’t replace the hostname with my username

' --- Global Declarations ---
Dim oWshShell
Dim ofso
Dim file
Dim sStartupCommand
Dim oWshNetwork
dim sLoginName
Dim sUserIniLocation
Dim sUserIniFile
Dim sBaseIniFile
Dim sHostname
Dim oParmFile
dim strParmFileText
dim sReplaceText
'
Set ofso = CreateObject("Scripting.FileSystemObject")
Set oWshShell = CreateObject("Wscript.Shell")
Set oWshNetwork = CreateObject("Wscript.Network")

SHostname = Environ("Username")
sUserIniLocation = "C:\localfolder\txt\"
sUserIniFile = sUserIniLocation & "app.ini"
sReplaceText = "Hostname=computer"

set oParmFile = oFSO.openTextFile(sUserIniFile)
strParmFileText = oParmFile.readall
oParmFile.close
strParmFileText = replace(strParmFileText, sReplaceText, SHostname)

set oParmFile = oFSO.openTextFile(sUserIniFile,2)
oParmFile.write(strParmFileText)
oParmFile.close
0
Comment
Question by:iamuser
  • 4
  • 2
7 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 39976827
Try replacing
SHostname = Environ("Username")

Open in new window

with
Set objSysInfo = CreateObject("ADSystemInfo")
sHostname = objSysInfo.UserName

Open in new window

0
 

Author Comment

by:iamuser
ID: 39976833
I will give that a try
0
 

Author Comment

by:iamuser
ID: 39976840
it worked but it replaced the entire "hostname=computer" line with a "CN, DC" line

I like to replace only the 'computer' portion of that line
0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 67

Accepted Solution

by:
sirbounty earned 350 total points
ID: 39976891
replace this
SHostname = Environ("Username")
sUserIniLocation = "C:\localfolder\txt\"
sUserIniFile = sUserIniLocation & "app.ini"
sReplaceText = "Hostname=computer"

Open in new window

with this

Set objSysInfo = CreateObject("ADSystemInfo")
sHostName = Split(Split(objSysInfo.UserName,",")(0),"=")(1)
sUserIniLocation = "C:\localfolder\txt\"
sUserIniFile = sUserIniLocation & "app.ini"

Open in new window


then change this line:

strParmFileText = replace(strParmFileText, sReplaceText, "Hostname=" & SHostname)

Open in new window

0
 

Author Comment

by:iamuser
ID: 39976914
Let me try that, thanks for the help
0
 
LVL 12

Assisted Solution

by:jkaios
jkaios earned 150 total points
ID: 39977132
Another method:

Dim oShell, oEvar
Set oShell = WScript.CreateObject("WScript.Shell")
set oEvar = oShell.Environment("Process")
sHostname = oEvar("UserName")

Open in new window

0
 

Author Comment

by:iamuser
ID: 40071110
Will re-post again since this is so old
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question