none
loop going wrong or?? vb script RRS feed

  • Question

  • I have created a script to push message out to users.

    but it dosnt behave like i wish?

    the first line and last line is the same

    the second line and the second line is the same

    and the third first line and third line is the same??

    Something like this??

    1
    2
    3
    4
    3
    2
    1

    I want it to run once only from top of the csv file and to the button of it.

    1
    2
    3
    4

    .

    somehow the message come out like below.

    the code:

    Set grplist = ("c:\CSV\Script_message.csv") 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) row = split(mapline,",") SecGroup = row(0) If InStr(LCase(SecGroup),LCase(grpname)) Then ' If you're in the group 'wscript.echo mapline row = split(mapline,",") SecGroup = row(0) Title = row(1) Symbol = row(2) Sec = row(3) Message = Row(4)

    Set objShell = Wscript.CreateObject("Wscript.Shell") objShell.Popup Message,Sec,Title,Sym ' http://msdn.microsoft.com/en-us/library/x83z1d9f%28v=vs.84%29.aspx Call WriteLogFileLine(strFile,"Besked: " & Message & " # Tid: " & Sec & "sek # BoxTitle: " & Title & " # Symbol: " & Symbol) 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

    the csv file:

    MSG_CompService,ServiceDesk,EXCL,0,Kontakt ServiceDesk
    MSG_CompService1,Information,INFO,5,Information Mark
    MSG_CompService2,???,QUES,5,Question Mark
    MSG_CompService3,STOP,STOP,5,Stop Mark


    Helpdesk Supporter






    • Edited by LongQuo Tuesday, September 2, 2014 4:27 PM
    Tuesday, September 2, 2014 3:25 PM

Answers

  • I looked at this http://community.spiceworks.com/scripts/show/630-map-drives-based-on-group-membership

    and created this. and it works now.

    modified code:

    http://pastebin.com/n2wx1W0p

    create a folder in your c drive named "cfg"

    put above file in the folder as script.vbs

    create in the same folder a csv file with this content:

    Domain Users,ServiceDesk,EXCL,0,ServiceDesk
    Domain Users,Information,INFO,5,Information Mark
    Domain Users,???,QUES,5,Question Mark
    Domain Users,STOP,STOP,5,Stop Mark

    run the script and you should get 4 message because you member of the for groups.

    only work if you mashine is a member of domain.

     


    Helpdesk Supporter

    • Marked as answer by LongQuo Tuesday, September 2, 2014 9:07 PM
    Tuesday, September 2, 2014 9:04 PM

All replies

  • It's a lot to ask for others to read through all of that code, figure out what's wrong (debug it), and then fix it for you. Start small. Write a small sample script that contains only the minimum amount of code needed to reproduce the problem. When you do this, it is likely you will discover the flaw in your script all on your own. This is also an excellent learning exercise.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, September 2, 2014 3:47 PM
    Moderator
  • Much of the code is just bad guesswork by someone.

    This makes no sense:

     Set grplist = ("c:\CSV\Script_message.csv")
     aGroup
    = Split(grplist.Readall,vbcrlf)

    YOU cannot set a value this way.  Start by reviewing how to use VBScript.


    ¯\_(ツ)_/¯

    Tuesday, September 2, 2014 3:54 PM
  • I have created a script to push message out to users.

    Further to Bills excellent advice: It would help you a lot if you gave your code a clear structure. Right now it looks like so:

    1. Header block
    2. Declarations
    3. A subroutine
    4. A lot of code
    5. More subroutines

    A structure like the one below would be much easier to maintain and debug for these reasons:

    • It is highly modular. Specific tasks are performed in specific subroutines.
    • You can easily turn individual subroutines on and off. With much of your code currently in the main section it is a struggle to do this (which is why you find it difficult to locate the cause of your problem).

    In general you should have only a small amount of code in your main routine. The subroutines should be below the main routine.

    Header
    Global declarations

    GetGroupInfo()

    GroupMappings
    Include()

    sub GroupMappings
    ..
    end sub

    Sub Include (strFile)
    ...
    End Sub

    Sub GetGroupInfo
    ...
    End Sub

    Tuesday, September 2, 2014 4:05 PM
  • The script read the csv file map the first value to a Active Directory Group.

    If the user is a member of that group it continues with the next value and so on.

    I have made 4 group and put a user "TestUser" in it.

    When the script runs it trigger them all.

    in this order

    1
    2
    3
    4
    3
    2
    1

    where it only show do this

    1
    2
    3
    4

    i have shortet the script down to the to routines.


    Helpdesk Supporter


    • Edited by LongQuo Tuesday, September 2, 2014 4:33 PM
    Tuesday, September 2, 2014 4:32 PM
  • I think it this subroutine i dont understand.

    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

    the script i have worked further with is this.

    http://www.lazynetworkadmin.com/knowledgebase-mainmenu-6/2-windows/62-mapunmap-network-drive-based-on-group-membership


    Helpdesk Supporter


    • Edited by LongQuo Tuesday, September 2, 2014 4:39 PM
    Tuesday, September 2, 2014 4:34 PM
  • This is impossible:

     Set grplist = ("c:\CSV\Script_message.csv")
      
       aGroup
    = Split(grplist.Readall,vbcrlf)

    YOU have posted only part of the code and what you have posted does not match your question.


    ¯\_(ツ)_/¯

    Tuesday, September 2, 2014 4:37 PM
  • A longer look and most of the code makes no sense.  It  is code that has been edited by someone who does not know VBScript.

    You need to start over as this is not fixable in a few lines.  You should contact a consultant to help you if this is important.  We cannot rewrite this for you.


    ¯\_(ツ)_/¯

    Tuesday, September 2, 2014 4:40 PM
  • i will start from scratch with the base script and figure out what goes wrong.

    the Base code look like this

    ----------COPY EVERYTHING BELOW THIS LINE for the Script----------
    'This script removes mapped drives that are not in the grouplist.csv
    'Created By Chey Harden 1.17.07
    
    On Error Resume Next
    Const ForReading = 1
    
    Dim objFSO, objDrive, objTextFile, intDrive, intNetLetter, strSearch
    Dim objFile, strContents, WSHNetwork, strDrive, objNetwork
    
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objNetwork = CreateObject("WScript.Network") 
    Set objDrive = objNetwork.EnumNetworkDrives 
    Set objTextFile = objFSO.OpenTextFile("\\fileserver\netlogon\grouplist.csv", ForReading)
    Set WSHNetwork = WScript.CreateObject("WScript.Network")
    
    
    ' Enumerated Mapped Drives
    For intDrive = 0 to objDrive.Count -1 Step 2
    intNetLetter = IntNetLetter +1
    'WScript.Echo  objDrive.Item(intDrive) & "," & objDrive.Item(intDrive +1)
    strSearch = objDrive.Item(intDrive) & "," & objDrive.Item(intDrive +1)
    'Wscript.Echo strSearch
    
     
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    Set objFile = objFSO.OpenTextFile("\\fileserver\netlogon\grouplist.csv", ForReading)
    strContents = objFile.ReadAll
    objFile.Close
    
    If InStr(strContents, strSearch) Then
     'Wscript.Echo "Found"
     
     Else
        'Wscript.Echo strSearch
     strDrive = Left(strSearch,2) 
     'Wscript.Echo strDrive
     strDrive = "" & strDrive & ""
     'Wscript.Echo strDrive
     WSHNetwork.RemoveNetworkDrive strDrive
    End If
    
    
    '************************ 
    Next
    
    'The next part of the script maps the correct 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 = 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 ' 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 
    'msgbox "The Logon script has been run"
    
     
    
    ----------COPY EVERYTHING ABOVE THIS LINE for the Script----------


    Helpdesk Supporter

    Tuesday, September 2, 2014 5:22 PM
  • If the purpose of this script is to map drives based on group memberships, then I recommend that you use Group Policy preferences instead. (Then you don't need a script.)

    -- Bill Stewart [Bill_Stewart]

    Tuesday, September 2, 2014 5:29 PM
    Moderator
  • I know but I use this to push popd message out to user. that are member of specifik group.

    It simpler to manage from the csv file for supporter.


    Helpdesk Supporter

    Tuesday, September 2, 2014 7:56 PM
  • I know but I use this to push popd message out to user. that are member of specifik group.

    It simpler to manage from the csv file for supporter.


    Helpdesk Supporter

    So what is the exact problem. the original post has nothing to do with mapping drives or sending messages.


    ¯\_(ツ)_/¯

    Tuesday, September 2, 2014 8:05 PM
  • We would definitely recommend using group policy for this purpose, particularly because it seems you're not an experienced script programmer. If you want to pop up a message to the user that tells them what groups they are in, you can write a script that does only that. You don't need a script to map the drives.

    In any case, it's a bit much to ask others to rewrite a script for you, particularly when there are better ways to accomplish your goal (group policy). It is no longer necessary to map drives using a logon script.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, September 2, 2014 8:06 PM
    Moderator
  • I use the group membership lookup from this drive mapping script to push the specifik popup message.

    instead of mapping drive i push message out instead. 


    Helpdesk Supporter

    Tuesday, September 2, 2014 8:16 PM
  • I use the group membership lookup from this drive mapping script to push the specifik popup message.

    instead of mapping drive i push message out instead. 


    Helpdesk Supporter

    So you are now saying this is not a logon script to map drive????

    I think you need to rethink the whole question.  If you cannot sort out what you are asking I suggest hiring a consultant to help you sort it out.


    ¯\_(ツ)_/¯

    Tuesday, September 2, 2014 8:24 PM
  • So what is your question? Please be specific.

    -- Bill Stewart [Bill_Stewart]

    Tuesday, September 2, 2014 8:24 PM
    Moderator
  • http://community.spiceworks.com/scripts/show/630-map-drives-based-on-group-membership

    okay

    I have a user that member of member of 

    3 groups

    grp_mess1

    grp_mess2

    grp_mess3

    the CSV file look like this

    grp_mess1,icon:,message 1
    grp_mess2,icon:,message 2
    grp_mess3,icon:,message 3

    i have modified the mapping drive path to push message

    mapping drive path

     If (fso.DriveExists(drive) <> True) and (Drive<>"!!") Then ' If The Drive is not already mapped
                   WshNetwork.MapNetworkDrive drive,path ' Map The Drive
                   wscript.sleep 1000
     End If

    above to something.

    Set objShell = Wscript.CreateObject("Wscript.Shell") objShell.Popup Message,Sec,Title,Sym

    User x also get the message but somehow

    he get is in this way

    message 1

    message 2

    message 3

    message 2

    message 1

    and he should only get it like this

    message 1

    message 2

    message 3


    Helpdesk Supporter

    Tuesday, September 2, 2014 8:32 PM
  • Sorry but you cannot do that. Scripts do not work that way.

    You need to spend some time leaning about Windows and how scripts work.

    Start by defining exactly what you need to do in plain language then start collecting the pieces needed.

    For scripting knowledge start here: http://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx

    You cannot send a popup message to a remote user. YOu can use netmsg utimilies like "MSG"

    Type MSG /?  and follow the instructions.


    ¯\_(ツ)_/¯

    Tuesday, September 2, 2014 8:40 PM
  • the script is a path of of the loginscript that are executed when the person logs on.

    so beside mapping drives. he also get these messages.


    Helpdesk Supporter

    Tuesday, September 2, 2014 8:46 PM
  • I looked at this http://community.spiceworks.com/scripts/show/630-map-drives-based-on-group-membership

    and created this. and it works now.

    modified code:

    http://pastebin.com/n2wx1W0p

    create a folder in your c drive named "cfg"

    put above file in the folder as script.vbs

    create in the same folder a csv file with this content:

    Domain Users,ServiceDesk,EXCL,0,ServiceDesk
    Domain Users,Information,INFO,5,Information Mark
    Domain Users,???,QUES,5,Question Mark
    Domain Users,STOP,STOP,5,Stop Mark

    run the script and you should get 4 message because you member of the for groups.

    only work if you mashine is a member of domain.

     


    Helpdesk Supporter

    • Marked as answer by LongQuo Tuesday, September 2, 2014 9:07 PM
    Tuesday, September 2, 2014 9:04 PM
  • I don't understand your question(s), sorry. In addition, I don't think you're going to be able to talk anyone into fixing all of your code for you since there is already another supported way to map drives based on group memberships (group policy preferences).

    I recommend using group policy preferences instead. In this way you do not need a script. (Why should we rewrite your script for you when you can use group policy preferences and not need a script at all?)

    If you insist on using a script, you will probably need to hire a consultant to rewrite/redesign your script to do what you want for it to do.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, September 2, 2014 9:08 PM
    Moderator
  • Suit yourself. I recommend not using a script at all. This is much simpler to manage using policy.

    -- Bill Stewart [Bill_Stewart]

    Tuesday, September 2, 2014 9:09 PM
    Moderator
  • it not about mapping "drives".

    It about pushing "service" message out based on group membership.

    When people login.

    Try above code.



    Helpdesk Supporter

    Tuesday, September 2, 2014 9:10 PM
  • but thanks for the input... must keep it simple and clean...easier to debug then.

    Helpdesk Supporter

    Tuesday, September 2, 2014 9:12 PM
  • I have made copy of this thread.  It is a perfect example of how not to ask a question.

    I still am not sure of what the original post has to do with the end story.

    I suspect we have been flim-flamed by a fool.


    ¯\_(ツ)_/¯

    Tuesday, September 2, 2014 9:18 PM
  • hmm i only seeked help to understand the strange behavior of my first script.

    maybe i did not specify my problem good enought. but have found the error in my script and it work now.


    Helpdesk Supporter

    Tuesday, September 2, 2014 9:29 PM
  • You have asked at least three distinct and apparently unrelated and fairly vague questions.

    Here is the answer to one.  This is howto correctly enumerate and extract group names.

    groups = GetGroupList()
    For Each group In groups
        WScript.Echo group
    Next
    
    
    Function GetGroupList()
        
        Set wshNetwork = CreateObject("WScript.Network")
        Set user = GetObject("WinNT://" & wshNetwork.UserDomain & "/" & WshNetwork.UserName)
    
    i = 0 For Each group In user.Groups ReDim Preserve groupList(i) groupList(i) = group.Name i = i + 1 Next
    GetGroupList = groupList
    End Function

    You would help your self by actually trying to learn how to write VBScript instead of just pasting things together and then calling for help.


    ¯\_(ツ)_/¯


    • Edited by jrv Wednesday, September 3, 2014 2:30 AM
    Wednesday, September 3, 2014 2:28 AM