Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VB Script and getting domain username

Posted on 2014-04-03
7
Medium Priority
?
175 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses

972 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