Run file if user isn't a member of a group

Author Message
spectrum1

  • Total Posts : 7
  • Scores: 0
  • Reward points : 0
  • Joined: 12/5/2011
  • Status: offline
Run file if user isn't a member of a group Monday, December 05, 2011 9:49 AM (permalink)
0
I'm having problems with this vb script. All works how we want apart from the "else" statement at the end. We want users who aren't a member of MANAGERS group to run the registry file remove.reg. The update works for users who are members of MANAGERS group and have the application on their PC C:\Program Files\Hewlett-Packard\HP\network.exe. Hope someone can help, I'm new to scripting.
 
Set WSHNetwork = CreateObject("WScript.Network")
Set WshShell = WScript.CreateObject("Wscript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Wscript.Shell")
strfolderpath = objShell.ExpandEnvironmentStrings("%appdata%\Hewlett-Packard\")
Set UserObj = GetObject("WinNT://" & WSHNetwork.UserDomain & "/" & WSHNetwork.UserName)
For Each GroupObj In UserObj.Groups
    Select Case UCase(GroupObj.Name)
          Case "MANAGERS"
            set filesys=CreateObject("Scripting.FileSystemObject")
     If filesys.FileExists("C:\Program Files\Hewlett-Packard\HP\network.exe") Then
        filesys.CopyFile "V:\HP\Configuration\*", strfolderpath
            WshShell.run "regedit /S ""C:\update.reg""",0,True
   Else
     WshShell.run "regedit /S ""C:\remove.reg""",0,True
    
End If
      
End Select
Next
<message edited by spectrum1 on Monday, December 05, 2011 9:52 AM>
 
#1
    59cobalt

    • Total Posts : 979
    • Scores: 91
    • Reward points : 0
    • Joined: 7/17/2011
    • Status: offline
    Re:Run file if user isn't a member of a group Monday, December 05, 2011 10:20 AM (permalink)
    5
    First, using a Select statement for a binary decision is pointless. Second, the If-Then-Else is inside the Case "MANAGERS" branch, so remove.reg will only be merged with the Registry when a user is a member of "Managers" and network.exe does not exist. Third, even if you turn the Else into a Case Else, things will not work as you seem to expect, because then remove.reg will be merged for each group other than "Managers" that the user is a member of.

    If you want your script to do something when some collection does not contain a particular item, you need something like this:
    isManager = False
    For Each GroupObj In UserObj.Groups
     If UCase(GroupObj.Name) = "MANAGERS" Then isManager = True
    Next
    If isManager Then
     If filesys.FileExists("C:\Program Files\Hewlett-Packard\HP\network.exe") Then
     filesys.CopyFile "V:\HP\Configuration\*", strfolderpath
     WshShell.Run "regedit /S C:\update.reg", 0, True
     End If
    Else
     WshShell.Run "regedit /S C:\remove.reg", 0, True
    End If

     
    #2
      spectrum1

      • Total Posts : 7
      • Scores: 0
      • Reward points : 0
      • Joined: 12/5/2011
      • Status: offline
      Re:Run file if user isn't a member of a group Monday, December 05, 2011 11:08 AM (permalink)
      0
      Many thanks this has really helped. 
      
      <message edited by spectrum1 on Monday, December 05, 2011 11:14 AM>
       
      #3
        59cobalt

        • Total Posts : 979
        • Scores: 91
        • Reward points : 0
        • Joined: 7/17/2011
        • Status: offline
        Re:Run file if user isn't a member of a group Tuesday, December 06, 2011 12:14 AM (permalink)
        0
        You're welcome.
         
        #4

          Online Bookmarks Sharing: Share/Bookmark

          Jump to:

          Current active users

          There are 0 members and 1 guests.

          Icon Legend and Permission

          • New Messages
          • No New Messages
          • Hot Topic w/ New Messages
          • Hot Topic w/o New Messages
          • Locked w/ New Messages
          • Locked w/o New Messages
          • Read Message
          • Post New Thread
          • Reply to message
          • Post New Poll
          • Submit Vote
          • Post reward post
          • Delete my own posts
          • Delete my own threads
          • Rate post

          2000-2012 ASPPlayground.NET Forum Version 3.9