Can’t figure out the syntax for a script

Home Forums Scripting PowerShell Can’t figure out the syntax for a script

This topic contains 5 replies, has 3 voices, and was last updated by Avatar weirdo 2 years, 11 months ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • Avatar
    jason0923
    Participant
    #166668

    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

    Avatar
    Anonymous
    #371970

    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.

    Avatar
    weirdo
    Member
    #390566

    I can’t find the powershell commands for “if not found then”

    Avatar
    weirdo
    Member
    #390567

    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

    Avatar
    Anonymous
    #371971

    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.

    Avatar
    weirdo
    Member
    #390568

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

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.