Solved

VB Script and getting domain username

Posted on 2014-04-03
7
171 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

623 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