Avatar of PeterSinger
PeterSinger
Flag for Australia asked on

VB Script How do I add the ability to use %username% in the .cvs file

Hi,

How do I add the ability in the attached code to use %username% and %logonserver% in the .cvs file

e.g.
example Grouplist.csv file below
Users,H:,%logonserver%\%username%$
or
Users,H:,\\servername\%username%$

Regards
Peter

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)) Then ' If the drive mapping is stale,remove it
	       WshNetwork.RemoveNetworkDrive drive,true,true
               wscript.sleep 1500
	End If        
 
	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

VB ScriptVisual Basic Classic

Avatar of undefined
Last Comment
PeterSinger

8/22/2022 - Mon
sirbounty

Use
strUserName = WshShell.ExpandEnvironmentStrings("%UserName%")
strLogonServer=WshShell.ExpandEnvironmentStrings("%LogonServer%")

PeterSinger

ASKER
So how would I intgragrate it inot the code.
I want to be able to use ("%UserName%") or ("%LogonServer%") instead of the full name if I want to and use the full name if I want to.
Would you send back a copy of my script with your changes put into it?
Peter
sirbounty

I don't understand your question.
The above statements will get the environment variables you're looking for, however you reference to WshNetwork.UserName will retrieve the same thing.

Where are you wanting to use this?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
PeterSinger

ASKER
HI,
 
Read the code.
I take the commands from a file called "logonpath&"\Grouplist.csv"
In that file the format is;
group,H:,\\servername\sharename
I want to be able to use Users,H:,%logonserver%\%username%$

(a veriable) instead of just using the full name of the share.
I put your lines in my code and it does not map the dirve to the share whenrun.
See the code below. Please help asap.
Peter
 

On Error Resume Next
 
Dim GroupList
Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("WScript.Shell") 
Set WshNetwork = WScript.CreateObject("WScript.Network") 
 
strUserName = WshShell.ExpandEnvironmentStrings("%UserName%")
strLogonServer=WshShell.ExpandEnvironmentStrings("%LogonServer%")
 
 
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)) Then ' If the drive mapping is stale,remove it
	       WshNetwork.RemoveNetworkDrive drive,true,true
               wscript.sleep 1500
	End If        
 
	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

sirbounty

Still don't quite understand, but take those two new variables and use them where you need them...try this:
On Error Resume Next
 
Dim GroupList
Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("WScript.Shell") 
Set WshNetwork = WScript.CreateObject("WScript.Network") 
 
strUserName = WshShell.ExpandEnvironmentStrings("%UserName%")
strLogonServer=WshShell.ExpandEnvironmentStrings("%LogonServer%")
 
 
GetGroupInfo()
 
'LogonPath = fso.GetParentFolderName(WScript.ScriptFullName)
'**************************************Group Mappings Based on Grouplist.csv*********************************
If fso.FileExists(strLogonServer &"\Grouplist.csv") Then
   Set grplist = Fso.OpenTextFile(strLogonServer &"\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)) Then ' If the drive mapping is stale,remove it
               WshNetwork.RemoveNetworkDrive drive,true,true
               wscript.sleep 1500
        End If        
 
        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

ASKER CERTIFIED SOLUTION
purplepomegranite

THIS SOLUTION 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
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
purplepomegranite

Thanks, but IMO the points should be split, as SirBounty provided the original code amendment - I simply added this into your routine.  If you use Request Attention a moderator can reopen the question to allow the points to be split.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
PeterSinger

ASKER
The issue is I  asked SirBounty twice to provice the information and he did not till I had to open a new issue and find someone else to fully answer the question.  I do not wish to re-open the issue. Thanks for your input.
sirbounty

Not sure what you mean by asking me twice - this is the only question I have been involved in of yours.
I may not have completely understood what you were after, but that doesn't mean I was neglecting the question...
PeterSinger

ASKER
I do not think you wrer neglecting the question.  I just needed it answered.
Regards
Peter
Your help has saved me hundreds of hours of internet surfing.
fblack61