VB scripting, grabbing username and passing them as variables.

Discussion in 'OT Technology' started by 127.0.0.1, Jun 5, 2007.

  1. 127.0.0.1

    127.0.0.1 New Member

    Joined:
    Sep 7, 2003
    Messages:
    9,310
    Likes Received:
    0
    Location:
    Fort Worth, Texas
    I am attempting to do a few things with vb scripts. I have written a group policy that will distribute my script to all the users on my network that it applies to.

    I need a way to pull the current logged on username, and enter that in as a variable for my script. All of my users are roaming accounts too.

    I know that you can do something along the lines of.

    dim wsh
    set wsh = createobject("WScript.Shell")
    login_name = wsh.RegRead("HKEY_CURRENT_USER\Software\Microsoft\ Windows\CurrentVersion\Explorer\Logon User Name")

    Currently, I need to be able to grab the name, then pass it as a variable at the beginning of my script. The way the script is set up now,

    call updatecopyini("hard coded username")
    wscript.quit
    sub updatecopyini(iusername)

    I do not know how to get from one place to the next.

    thanks.
     
  2. 5Gen_Prelude

    5Gen_Prelude There might not be an "I" in the word "Team", but

    Joined:
    Mar 14, 2000
    Messages:
    14,519
    Likes Received:
    1
    Location:
    Vancouver, BC, CANADA
    Didn't I already show you how to do this without vb?
     
  3. peerk

    peerk New Member

    Joined:
    Mar 14, 2005
    Messages:
    984
    Likes Received:
    0
    I usually grab the username from the environment variable %USERNAME%

    Code:
    Set oShell = CreateObject( "WScript.Shell" )
    username = oShell.ExpandEnvironmentStrings("%UserName%")
    
    Your way works too, but this is good to know because there are other useful environment variables.

    I'm not sure I understand your question. But I think it is about variable scope.

    If you Dim a variable within a sub or pass it to a sub its scope is within the sub. If you don't it is a script level variable.

    Code:
    Option Explicit
    
    Dim a, b
    
    a = 5
    b = 10
    
    call showA()
    call showB(55)
    
    
    sub showA()
    	' displays 5
    	wscript.echo a
    end sub 
    
    sub showB(b)
    	' displays 55
    	wscript.echo b
    end sub
    
     
  4. 127.0.0.1

    127.0.0.1 New Member

    Joined:
    Sep 7, 2003
    Messages:
    9,310
    Likes Received:
    0
    Location:
    Fort Worth, Texas
    it didn't really stick...
     
  5. peerk

    peerk New Member

    Joined:
    Mar 14, 2005
    Messages:
    984
    Likes Received:
    0
    You could do something like this.

    Code:
    dim wsh
    set wsh = createobject("WScript.Shell")
    login_name = wsh.RegRead("HKEY_CURRENT_USER\Software\Microsoft\ Windows\CurrentVersion\Explorer\Logon User Name")
    
    call updatecopyini(login_name)
    wscript.quit
    
    
    sub updatecopyini(iusername)
    end sub
    
    Inside updatecopyini you can use the variable iusername or login_name. You can use login_name because it has script level scope. It has script level scope because it isn't created within a sub or function. The variable iusername has procedure level scope because it is created within a sub. It can only be used within updatecopyini.

    Since you can use login_name anywhere, you can even do something like this.

    Code:
    dim wsh
    set wsh = createobject("WScript.Shell")
    login_name = wsh.RegRead("HKEY_CURRENT_USER\Software\Microsoft\ Windows\CurrentVersion\Explorer\Logon User Name")
    
    call updatecopyini()
    wscript.quit
    
    
    sub updatecopyini()
    end sub
    
     
  6. 127.0.0.1

    127.0.0.1 New Member

    Joined:
    Sep 7, 2003
    Messages:
    9,310
    Likes Received:
    0
    Location:
    Fort Worth, Texas
    Here is the current setup, i think I have something wrong though..
    Code:
    dim wsh
    set wsh = createobject("WScript.Shell")
    login_name = wsh.RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Logon User Name")
    call updatecopyini(login_name)
     
    wscript.quit
    sub updatecopyini(iusername)
    end sub
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    strFileName = "C:\folder\test.ini"
    If objFSO.FileExists("C:\FSO\ScriptLog.txt") Then
        exit sub
    Else
    End If
     
    Set objFile = objFSO.OpenTextFile(strFileName , 1)
    dim strArray(5000), i, j
    i = 0
    Do Until objFile.AtEndOfStream 
        i = i + 1
        strLine = objFile.ReadLine
            if mid(strline, 1, 14) = "FileDirectory=" then
           strline = "FileDirectory=\\server\folder\" & iusername
        end if
        if mid(strline, 1, 7) = "Notify=" then
           strline = "Notify=1" 
        end if
        if mid(strline, 1, 15) = "UseManualInbox=" then
           strline = "UseManualInbox=1" 
        end if
        if mid(strline, 1, 13) = "PreviousUser=" then
           strline = "PreviousUser=" & iusername
        end if
        strArray(i) = strLine
    Loop
    objFile.Close
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile(strFileName, 2)
    for j = 1 to i
       objfile.writeline strArray(j)
    Next
    objFile.Close
    end sub
    it doesn't have errors, but it doesn't update the ini file
     
  7. 127.0.0.1

    127.0.0.1 New Member

    Joined:
    Sep 7, 2003
    Messages:
    9,310
    Likes Received:
    0
    Location:
    Fort Worth, Texas
    nevermind, i got the username thing working.

    thanks so much.
     

Share This Page