Avatar of big_grasshopper
big_grasshopper

asked on 

VB Scripting help using varible %username%

I am in the process of setting up a test domain Win Svr 2003 and ran across this vbs script I really like from ( http://lazynetworkadmin.com/content/view/7/6/ ). I have the script mapping drives with no problems, but it will not work with the variable %username% in the csv file. I am just beginning my adventure in scripting so this is beyond my ability. Any help would be greatly appreciated.

Also how do you most admin's map drives and printer, I'm interested to see how everyone else does this task.
----------COPY EVERYTHING BELOW THIS LINE----------
On Error Resume Next
 
Dim GroupList
Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("WScript.Shell")
Set WshNetwork = WScript.CreateObject("WScript.Network")
 
GetGroupInfo()
 
LogonPath = fso.GetParentFolderName(WScript.ScriptFullName)
'**************************************Group Mappings Based on Grouplist.csv*********************************
If fso.FileExists(logonpath&"\Grouplist.csv") Then
   Set grplist = Fso.OpenTextFile(logonpath&"\Grouplist.csv")
   ' make File into an Array
   aGroup = Split(grplist.Readall,vbcrlf)
   For I = 0 to UBound(GroupList) ' Check Every Group Membership the user is in (populated into Grouplist)
      grpname = Grouplist(i)
      For x = 0 to UBound(aGroup) ' Read the entire CSV to make sure all drives are mapped for each Group
         mapline = agroup(x)
         If InStr(LCase(mapline),LCase(grpname)) Then ' If you're in the group
            mapline = Mid(mapline,InStr(mapline,",")+1) ' Remove the GroupName from the line
            Drive = Left(mapline,InStr(mapline,",")-1) ' Extract Drive Letter
            Path = Mid(mapline,InStr(mapline,",")+1) ' Extract the path
 
            If (fso.DriveExists(drive) <> True) and (Drive<>"!!") Then ' If The Drive is not already mapped
               WshNetwork.MapNetworkDrive drive,path,true ' Map The Drive
               wscript.sleep 1000
            End If
 
        If Drive = "!!" then
               WSHNetwork.AddWindowsPrinterConnection Path
               wscript.sleep 1000
            end if
 
         End If
      Next
   Next
End If
 
 
Sub GetGroupInfo
Set UserObj = GetObject("WinNT://" & wshNetwork.UserDomain & "/" & WshNetwork.UserName)
Set Groups = UserObj.groups
 
For Each Group In Groups
GroupCount = GroupCount + 1
Next
 
ReDim GroupList(GroupCount -1)
i = 0
For Each Group In Groups
GroupList(i) = Group.Name
i = i + 1
Next
End Sub 
----------COPY EVERYTHING ABOVE THIS LINE----------
 
 Then make yourself a GroupList.csv file and follow this format:
 
----------CSV FILE CONTENTS----------
Domain Users,F:,\\optimus-prime\data\users\%username%
----------CSV FILE CONTENTS----------

Open in new window

VB ScriptWindows BatchWindows Server 2003

Avatar of undefined
Last Comment
B.J. Davis
Avatar of AmazingTech
AmazingTech

The easiest would be to change this line:

Path = Mid(mapline,InStr(mapline,",")+1) ' Extract the path

To enumerate %username%.

Path = Replace(Mid(mapline,InStr(mapline,",")+1),"%username%",environ("UserName),,,vbTextCompare) ' Extract the path
ASKER CERTIFIED SOLUTION
Avatar of B.J. Davis
B.J. Davis
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of trythisone
trythisone
Flag of United States of America image

If you need to use %username% Define the variable first:

username = shell.ExpandEnvironmentStrings("%USERNAME%")
Avatar of big_grasshopper

ASKER

AmazingTech I tried what you posted and I got this error:
Script:
logon.vbs
Line:24
Char:122
Error: Unterminated string constant
Code: 800A0409
Source: MS VBScript compilation error

bjd it worked

trythisone I wasn't able to get that to work

Thank You guys I appreciate it. What do you guys use to map your network drives?
On Error Resume Next
 
Dim GroupList
Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("WScript.Shell")
Set WshNetwork = WScript.CreateObject("WScript.Network")
 
GetGroupInfo()
 
LogonPath = fso.GetParentFolderName(WScript.ScriptFullName)
'**************************************Group Mappings Based on Grouplist.csv*********************************
If fso.FileExists(logonpath&"\Grouplist.csv") Then
   Set grplist = Fso.OpenTextFile(logonpath&"\Grouplist.csv")
   ' make File into an Array
   aGroup = Split(grplist.Readall,vbcrlf)
   For I = 0 to UBound(GroupList) ' Check Every Group Membership the user is in (populated into Grouplist)
      grpname = Grouplist(i)
      For x = 0 to UBound(aGroup) ' Read the entire CSV to make sure all drives are mapped for each Group
         mapline = agroup(x)
         If InStr(LCase(mapline),LCase(grpname)) Then ' If you're in the group
            mapline = Mid(mapline,InStr(mapline,",")+1) ' Remove the GroupName from the line
            Drive = Left(mapline,InStr(mapline,",")-1) ' Extract Drive Letter
	    Path = Replace(Mid(mapline,InStr(mapline,",")+1), "%username%", WshNetwork.UserName) ' Extract the path
 
            If (fso.DriveExists(drive) <> True) and (Drive<>"!!") Then ' If The Drive is not already mapped
               WshNetwork.MapNetworkDrive drive,path,true ' Map The Drive
               wscript.sleep 1000
            End If
 
        If Drive = "!!" then
               WSHNetwork.AddWindowsPrinterConnection Path
               wscript.sleep 1000
            end if
 
         End If
      Next
   Next
End If
 
 
Sub GetGroupInfo
Set UserObj = GetObject("WinNT://" & wshNetwork.UserDomain & "/" & WshNetwork.UserName)
Set Groups = UserObj.groups
 
For Each Group In Groups
GroupCount = GroupCount + 1
Next
 
ReDim GroupList(GroupCount -1)
i = 0
For Each Group In Groups
GroupList(i) = Group.Name
i = i + 1
Next
End Sub 

Open in new window

Avatar of AmazingTech
AmazingTech

Sorry it was missing the ending quote from environ("UserName").

Path = Replace(Mid(mapline,InStr(mapline,",")+1),"%username%",environ("UserName"),,,vbTextCompare)
Avatar of AmazingTech
AmazingTech

Our organization uses KIX.

Logon Script runs KIX32 with an INI for input.

http://www.kixtart.org/
Avatar of B.J. Davis
B.J. Davis
Flag of United States of America image

i use a combination of KixTart and DialogScript

thank you ...
Windows Server 2003
Windows Server 2003

Windows Server 2003 was based on Windows XP and was released in four editions: Web, Standard, Enterprise and Datacenter. It also had derivative versions for clusters, storage and Microsoft’s Small Business Server. Important upgrades included integrating Internet Information Services (IIS), improvements to Active Directory (AD) and Group Policy (GP), and the migration to Automated System Recovery (ASR).

129K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo