none
Powershell - Check users in each group and add a count RRS feed

  • Question

  • I am tasked to create a script that can perform the following task. Example: We have a Domain ABC.

    There are 4 security groups, named A, B, C and D.

    Peter is member of A and B group.
    David is member of B and C group.
    John is member of A group.
    B group is also a member A group.

    We want to find the total count of user in A, B groups. Those user that already in the group will be exclude in the count which mean exclude duplicate users count. Based on above, the script will show a total count of A & B = 3 users instead 5 users. I tried to work on a script that will count the user in A and B groups and also any group exist in A and B group. If John reside in A and B, the script will just add 1 count. Also If David is in B group but not in A group, it will add 1 count.

    Code that I tried but it didn't work.

    Foreach ($company In $companies) { $companyouname = $company |

    %{$_.Name} $users = Get-ADUser -Filter * -SearchBase

    ("OU=Users,OU=" + $companyouname + "," + $cntry_DN)

    -Searchscope SubTree Foreach ($user In $users) { $username = $user

    | %{$_.SAMAccountName} If (($GroupMember_A_Group -contains $username)

    -OR ($GroupMember_B_Group -contains $username)) {$User += 1} }

    $User = 0 $cntry + User + "," | Out-File $log -append



    • Edited by Cedric2 Tuesday, September 19, 2017 9:44 AM
    • Moved by Bill_Stewart Friday, March 9, 2018 7:32 PM This is not "teach me basics step-by-step" forum
    Tuesday, September 19, 2017 9:43 AM

All replies

  • The code you posted is complete nonsense. It appears you copied this incorrectly or made changes to a script that you do not understand.

    Please format it correctly and post back with a correctly written script.  Syntactically this can never run and it makes no sense so we cannot help you fix it.

    I recommend starting here to learn basic PowerShell:  https://mva.microsoft.com/en-us/training-courses/getting-started-with-microsoft-powershell-8276


    \_(ツ)_/

    Tuesday, September 19, 2017 9:50 AM
  • Hint. Use help to understand how to use CmdLets.

    Hint: 
    $group = Get-AdGroupMember <groupname>
    $group.Count


    \_(ツ)_/

    • Proposed as answer by I.T Delinquent Tuesday, September 19, 2017 2:50 PM
    Tuesday, September 19, 2017 9:54 AM
  • I have rewrite the code but still getting an error.

    Where-Object : Cannot bind parameter 'FilterScript'. Cannot convert the "objectclass" value of type "System.String"
     to type "System.Management.Automation.ScriptBlock".
    At C:\Untitled2.ps1:28 char:17
    +            where <<<<  objectclass -eq 'user' |
        + CategoryInfo          : InvalidArgument: (:) [Where-Object], ParameterBindingException
        + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.WhereObjectCommand
     
    Where-Object : Cannot bind parameter 'FilterScript'. Cannot convert the "objectclass" value of type "System.String"
     to type "System.Management.Automation.ScriptBlock".
    At C:\Untitled2.ps1:28 char:17
    +            where <<<<  objectclass -eq 'user' |
        + CategoryInfo          : InvalidArgument: (:) [Where-Object], ParameterBindingException
        + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.WhereObjectCommand
     
    =
    Cannot index into a null array.
    At C:\Desktop\Untitled2.ps1:34 char:8
    + $total[ <<<< 0].Count + $total[1].Count
        + CategoryInfo          : InvalidOperation: (0:Int32) [], RuntimeException
        + FullyQualifiedErrorId : NullArray

    $groups = $A_Group, $B_Group $gm = @() foreach ($group in $groups) { $gm += Get-ADGroupMember $group -Recursive | where objectclass -eq 'user' |

    }
    ($gm.samaccountname | Select -Unique).Count
    
    Write-Output $total = ($gm.samaccountname | Select -Unique).count
    Write-Output "Total: $($total[0].Count + $total[1].Count)"
    Please help.

    Wednesday, September 20, 2017 6:53 AM
  • Two most important things:

    #1 -  Learn PowerShell before attempting complex programming tasks.

    #2 - Learn how to format and post code correctly.

    What you just posted is a complete jumble.  YOU are just throwing together a bunch of bad guesses which make it nearly impossible to underwstand what you are trying to do.


    \_(ツ)_/

    Wednesday, September 20, 2017 7:00 AM
  • Sorry I am new to powershell and just hope some one can guide along.

    This is the actual code.

     $groups = $A_group, $B_group
         $gm = @()
         foreach ($group in $groups) {
         $gm += Get-ADGroupMember $group -Recursive | where objectclass -eq user| select SamAccountName
    }
    ($gm.samaccountname | Select -Unique).Count
    
    Write-Output $total = ($gm.SamAccountname | Select -Unique).count

    Wednesday, September 20, 2017 7:24 AM
  • You need to stop and take time to learn PowerShell.  You are just pasting things together with no idea what they do.

    Here is what you have posted without all of the errors.

    $groups = 'A_group', 'B_group'
    $results = $groups| Get-ADGroupMember -Recursive | 
    	Where-Object{$_.objectclass -eq 'user'}
    Write-Output $results.Count

    Before you post any more pleas do the following:

    Learn PowerShell: <a href="https://mva.microsoft.com/en-us/training-courses/getting-started-with-microsoft-powershell-8276</o:p">https://mva.microsoft.com/en-us/training-courses/getting-started-with-microsoft-powershell-8276<o:p></o:p>

    We cannot incrementally teach you PowerShell.


    \_(ツ)_/


    • Edited by jrv Wednesday, September 20, 2017 7:32 AM
    Wednesday, September 20, 2017 7:32 AM