Announcement

Collapse
No announcement yet.

Can't figure out the syntax for a script

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Can't figure out the syntax for a script

    I'm trying to run a script that adds users to a group if they are in a file but also removes them if they are no longer in that file so for example the forst part works fine

    ForEach ($User in $UserList)

    Then do what I need

    What I can't figure out is how to say for each user not in list then run this

  • #2
    Turn your logic around for the second part--for every username in the group whose name isn't on the list, delete that user.
    *-Query the group for the current member list.
    *-For each name, do a 'findstr' (find string) for that user's details in the source file $UserList. If not found, delete the user from the group.
    *RicklesP*
    MSCA (2003/XP), Security+, CCNA

    ** Remember: credit where credit is due, and reputation points as appropriate **

    Comment


    • #3
      I can't find the powershell commands for "if not found then"

      Comment


      • #4
        SO basically I have this ForEach ($User in $UserList) I need something like ForEach ($User NOT in $UserList) But I can't find an equivalent of the NOT part

        Comment


        • #5
          You can't do it that way. The ForEach only sets up a loop for execution of other steps, the statement itself doesn't perform any conditional test on the values you give it. That work is performed INSIDE the loop. What you're doing is loading the entire list of names into an array ($UserList), and then you have to step through that list and evaluate each single entry on it's own ($User). Based on your original description of what you wanted (add list names to group if not there, then remove names from group if not named on list), you have to do 2 different loops, one for each test. The first test is $User vs group membership, then second is group membership vs $UserList.

          First loop:
          Read the list of group names
          Start your loop to add missing names to the group ('ForEach' statement)
          Is the first list name in the group? (Test for first condition)
          Yes-do nothing (Test drops to bottom of loop)
          No-Add name to the group (Action when test fails)
          Next (Execution takes itself back to top of loop for next $User in the list)

          Second Loop:
          Start loop to remove unlisted names from group ('ForEach' statement)
          Is the first group member name on the $UserList? (Test for 2nd condition)
          Yes-do nothing (Test drops to bottom of loop)
          No-Remove name from group (Action when test fails)
          Next

          I've not written actual code statements here because of how the web service scans and interprets text as possible injection threats. But the logic is straightforward.
          *RicklesP*
          MSCA (2003/XP), Security+, CCNA

          ** Remember: credit where credit is due, and reputation points as appropriate **

          Comment


          • #6
            Let me take a look at this when I get back to the office next week, thanks for your help

            Comment

            Working...
            X