Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VB Script and getting domain username

Posted on 2014-04-03
7
Medium Priority
?
173 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 67

Accepted Solution

by:
sirbounty earned 1400 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 600 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

Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

Question has a verified solution.

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

This article will show, step by step, how to integrate R code into a R Sweave document
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

715 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