Struggling with If StatementsStyling multi-line conditions in 'if' statements?Python's equivalent of && (logical-and) in an if-statementPutting a simple if-then-else statement on one lineElse clause on Python while statementHow do I compare two string variables in an 'if' statement in Bash?How to write inline if statement for print?if else statement in AngularJS templatesif, elif, else statement issues in BashToo many 'if' statements?Receiving an error “Get-ADGroupMember : Cannot validate argument on parameter 'Identity'. The argument is null or empty. ”

Is this relativistic mass?

Was there ever an axiom rendered a theorem?

What's the difference between repeating elections every few years and repeating a referendum after a few years?

What does 'script /dev/null' do?

Why do we use polarized capacitors?

Is there a familial term for apples and pears?

What do you call something that goes against the spirit of the law, but is legal when interpreting the law to the letter?

Can a planet have a different gravitational pull depending on its location in orbit around its sun?

Crop image to path created in TikZ?

Why did the Germans forbid the possession of pet pigeons in Rostov-on-Don in 1941?

Is it legal to have the "// (c) 2019 John Smith" header in all files when there are hundreds of contributors?

Unbreakable Formation vs. Cry of the Carnarium

Is it wise to focus on putting odd beats on left when playing double bass drums?

Can the Produce Flame cantrip be used to grapple, or as an unarmed strike, in the right circumstances?

Why do UK politicians seemingly ignore opinion polls on Brexit?

What is GPS' 19 year rollover and does it present a cybersecurity issue?

Where to refill my bottle in India?

Can I legally use front facing blue light in the UK?

extract characters between two commas?

Does the average primeness of natural numbers tend to zero?

Calculate Levenshtein distance between two strings in Python

How can I add custom success page

Check if two datetimes are between two others

Extreme, but not acceptable situation and I can't start the work tomorrow morning



Struggling with If Statements


Styling multi-line conditions in 'if' statements?Python's equivalent of && (logical-and) in an if-statementPutting a simple if-then-else statement on one lineElse clause on Python while statementHow do I compare two string variables in an 'if' statement in Bash?How to write inline if statement for print?if else statement in AngularJS templatesif, elif, else statement issues in BashToo many 'if' statements?Receiving an error “Get-ADGroupMember : Cannot validate argument on parameter 'Identity'. The argument is null or empty. ”






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I have problems of understanding values of variables in PowerShell and I check them with if statements.



$LDAPDirectoryService = '10.10.XXX.XXX:389'
$DomainDN = 'o=Enterprise'
#$LDAPFilter = '(&(objectCategory=Person)(memberOf=cn=alc-01-Planung-rw,ou=KT,o=enterprise))'

$LDAPFilter = '(&(cn=alc-01-Planung-rw))'

$null = [System.Reflection.Assembly]::LoadWithPartialName('System.Net')

$LDAPServer = New-Object System.DirectoryServices.Protocols.LdapConnection $LDAPDirectoryService
$LDAPServer.AuthType = [System.DirectoryServices.Protocols.AuthType]::Anonymous

$Scope = [System.DirectoryServices.Protocols.SearchScope]::Subtree
$AttributeList = @('*')

$SearchRequest = New-Object System.DirectoryServices.Protocols.SearchRequest -ArgumentList $DomainDN,$LDAPFilter,$Scope,$AttributeList

$groups = $LDAPServer.SendRequest($SearchRequest)
$groups

if ($groups -eq $null) "No Group found"
if ($groups -eq " ") "No Group found"

foreach ($group in $groups.Entries)
$users = $group.attributes['member'].GetValues('string')
foreach ($user in $users)
Write-Host $user




I want to check if the group exists and then if users are existing in this group. I tried many statements but none of them seem to work.
It's not null or blank, even when nothing is written down in the console.



This is what I got when I use group which doesn't exist:



Screenshot



Can anybody show me a solution?










share|improve this question



















  • 1





    Try if ($groups.matcheddn -eq $null) 'no group found'

    – Mark Wragg
    Mar 21 at 13:42











  • Unfortunately it remains the same

    – Tobias Huprich
    Mar 21 at 13:50











  • I think $groups is an array, so you could test the Count property with if ($groups.Count -eq 0) or if (!($groups.Count)). Cannot test this myself right now though..

    – Theo
    Mar 21 at 13:53











  • Its not an array "$Groups.Entries" remains 1 under all circumstances

    – Tobias Huprich
    Mar 21 at 14:10











  • what do you get back when a group DOES exist?

    – Lee_Dailey
    Mar 21 at 15:59

















0















I have problems of understanding values of variables in PowerShell and I check them with if statements.



$LDAPDirectoryService = '10.10.XXX.XXX:389'
$DomainDN = 'o=Enterprise'
#$LDAPFilter = '(&(objectCategory=Person)(memberOf=cn=alc-01-Planung-rw,ou=KT,o=enterprise))'

$LDAPFilter = '(&(cn=alc-01-Planung-rw))'

$null = [System.Reflection.Assembly]::LoadWithPartialName('System.Net')

$LDAPServer = New-Object System.DirectoryServices.Protocols.LdapConnection $LDAPDirectoryService
$LDAPServer.AuthType = [System.DirectoryServices.Protocols.AuthType]::Anonymous

$Scope = [System.DirectoryServices.Protocols.SearchScope]::Subtree
$AttributeList = @('*')

$SearchRequest = New-Object System.DirectoryServices.Protocols.SearchRequest -ArgumentList $DomainDN,$LDAPFilter,$Scope,$AttributeList

$groups = $LDAPServer.SendRequest($SearchRequest)
$groups

if ($groups -eq $null) "No Group found"
if ($groups -eq " ") "No Group found"

foreach ($group in $groups.Entries)
$users = $group.attributes['member'].GetValues('string')
foreach ($user in $users)
Write-Host $user




I want to check if the group exists and then if users are existing in this group. I tried many statements but none of them seem to work.
It's not null or blank, even when nothing is written down in the console.



This is what I got when I use group which doesn't exist:



Screenshot



Can anybody show me a solution?










share|improve this question



















  • 1





    Try if ($groups.matcheddn -eq $null) 'no group found'

    – Mark Wragg
    Mar 21 at 13:42











  • Unfortunately it remains the same

    – Tobias Huprich
    Mar 21 at 13:50











  • I think $groups is an array, so you could test the Count property with if ($groups.Count -eq 0) or if (!($groups.Count)). Cannot test this myself right now though..

    – Theo
    Mar 21 at 13:53











  • Its not an array "$Groups.Entries" remains 1 under all circumstances

    – Tobias Huprich
    Mar 21 at 14:10











  • what do you get back when a group DOES exist?

    – Lee_Dailey
    Mar 21 at 15:59













0












0








0








I have problems of understanding values of variables in PowerShell and I check them with if statements.



$LDAPDirectoryService = '10.10.XXX.XXX:389'
$DomainDN = 'o=Enterprise'
#$LDAPFilter = '(&(objectCategory=Person)(memberOf=cn=alc-01-Planung-rw,ou=KT,o=enterprise))'

$LDAPFilter = '(&(cn=alc-01-Planung-rw))'

$null = [System.Reflection.Assembly]::LoadWithPartialName('System.Net')

$LDAPServer = New-Object System.DirectoryServices.Protocols.LdapConnection $LDAPDirectoryService
$LDAPServer.AuthType = [System.DirectoryServices.Protocols.AuthType]::Anonymous

$Scope = [System.DirectoryServices.Protocols.SearchScope]::Subtree
$AttributeList = @('*')

$SearchRequest = New-Object System.DirectoryServices.Protocols.SearchRequest -ArgumentList $DomainDN,$LDAPFilter,$Scope,$AttributeList

$groups = $LDAPServer.SendRequest($SearchRequest)
$groups

if ($groups -eq $null) "No Group found"
if ($groups -eq " ") "No Group found"

foreach ($group in $groups.Entries)
$users = $group.attributes['member'].GetValues('string')
foreach ($user in $users)
Write-Host $user




I want to check if the group exists and then if users are existing in this group. I tried many statements but none of them seem to work.
It's not null or blank, even when nothing is written down in the console.



This is what I got when I use group which doesn't exist:



Screenshot



Can anybody show me a solution?










share|improve this question
















I have problems of understanding values of variables in PowerShell and I check them with if statements.



$LDAPDirectoryService = '10.10.XXX.XXX:389'
$DomainDN = 'o=Enterprise'
#$LDAPFilter = '(&(objectCategory=Person)(memberOf=cn=alc-01-Planung-rw,ou=KT,o=enterprise))'

$LDAPFilter = '(&(cn=alc-01-Planung-rw))'

$null = [System.Reflection.Assembly]::LoadWithPartialName('System.Net')

$LDAPServer = New-Object System.DirectoryServices.Protocols.LdapConnection $LDAPDirectoryService
$LDAPServer.AuthType = [System.DirectoryServices.Protocols.AuthType]::Anonymous

$Scope = [System.DirectoryServices.Protocols.SearchScope]::Subtree
$AttributeList = @('*')

$SearchRequest = New-Object System.DirectoryServices.Protocols.SearchRequest -ArgumentList $DomainDN,$LDAPFilter,$Scope,$AttributeList

$groups = $LDAPServer.SendRequest($SearchRequest)
$groups

if ($groups -eq $null) "No Group found"
if ($groups -eq " ") "No Group found"

foreach ($group in $groups.Entries)
$users = $group.attributes['member'].GetValues('string')
foreach ($user in $users)
Write-Host $user




I want to check if the group exists and then if users are existing in this group. I tried many statements but none of them seem to work.
It's not null or blank, even when nothing is written down in the console.



This is what I got when I use group which doesn't exist:



Screenshot



Can anybody show me a solution?







powershell if-statement logical-operators






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 21 at 13:48









Ansgar Wiechers

146k13134192




146k13134192










asked Mar 21 at 13:31









Tobias HuprichTobias Huprich

83




83







  • 1





    Try if ($groups.matcheddn -eq $null) 'no group found'

    – Mark Wragg
    Mar 21 at 13:42











  • Unfortunately it remains the same

    – Tobias Huprich
    Mar 21 at 13:50











  • I think $groups is an array, so you could test the Count property with if ($groups.Count -eq 0) or if (!($groups.Count)). Cannot test this myself right now though..

    – Theo
    Mar 21 at 13:53











  • Its not an array "$Groups.Entries" remains 1 under all circumstances

    – Tobias Huprich
    Mar 21 at 14:10











  • what do you get back when a group DOES exist?

    – Lee_Dailey
    Mar 21 at 15:59












  • 1





    Try if ($groups.matcheddn -eq $null) 'no group found'

    – Mark Wragg
    Mar 21 at 13:42











  • Unfortunately it remains the same

    – Tobias Huprich
    Mar 21 at 13:50











  • I think $groups is an array, so you could test the Count property with if ($groups.Count -eq 0) or if (!($groups.Count)). Cannot test this myself right now though..

    – Theo
    Mar 21 at 13:53











  • Its not an array "$Groups.Entries" remains 1 under all circumstances

    – Tobias Huprich
    Mar 21 at 14:10











  • what do you get back when a group DOES exist?

    – Lee_Dailey
    Mar 21 at 15:59







1




1





Try if ($groups.matcheddn -eq $null) 'no group found'

– Mark Wragg
Mar 21 at 13:42





Try if ($groups.matcheddn -eq $null) 'no group found'

– Mark Wragg
Mar 21 at 13:42













Unfortunately it remains the same

– Tobias Huprich
Mar 21 at 13:50





Unfortunately it remains the same

– Tobias Huprich
Mar 21 at 13:50













I think $groups is an array, so you could test the Count property with if ($groups.Count -eq 0) or if (!($groups.Count)). Cannot test this myself right now though..

– Theo
Mar 21 at 13:53





I think $groups is an array, so you could test the Count property with if ($groups.Count -eq 0) or if (!($groups.Count)). Cannot test this myself right now though..

– Theo
Mar 21 at 13:53













Its not an array "$Groups.Entries" remains 1 under all circumstances

– Tobias Huprich
Mar 21 at 14:10





Its not an array "$Groups.Entries" remains 1 under all circumstances

– Tobias Huprich
Mar 21 at 14:10













what do you get back when a group DOES exist?

– Lee_Dailey
Mar 21 at 15:59





what do you get back when a group DOES exist?

– Lee_Dailey
Mar 21 at 15:59












1 Answer
1






active

oldest

votes


















0














What version of PowerShell are you running? Why are you not using the built-in AD group cmdlets for this or are you not using ADDS but some other LDAP service?



Or you may be on OSX/Linux and are using PSCore, which the ADDS/RSAT cmdlets are not there, well, not yet?



For your goals of …




I want to check if the group exists and then if users are existing in
this group.




… On Windows, with PowerShell 3x or higher, it's really only this...



# Get all AD groups and all members of each group
Clear-Host
(Get-ADGroup -Filter '*').Name |
%
"`n*** The members of $PSItem are as follows: ***`n"
If((Get-ADGroupMember -Identity $PSItem).Count -ge 1)

(Get-ADGroupMember -Identity $PSItem).SamAccountName

Else

Write-Warning -Message "$PSItem does not exist or has no members."




# Filtered
Clear-Host
((Get-ADGroup -Filter '*').Name -match 'Domain Admins|Domain Users' ) |
%
"`n*** The members of $PSItem are as follows: ***`n"
If((Get-ADGroupMember -Identity $PSItem).Count -ge 1)

(Get-ADGroupMember -Identity $PSItem).SamAccountName

Else

Write-Warning -Message "$PSItem does not exist or has no members."





Using your LDAP approach though... How about this...



'Administrators','Distributed COM Users' | 
ForEach
# Define LDAP search root, the Global catalog of the domain
$sLDAPSearchRoot = "LDAP://$((Get-ADDomainController).IPv4Address):389"

# The Groupname to looking for
($sGroupName = "$_")

# The LDAP query - query string
$sSearchStr = "(&(objectCategory=group)(name="+$sGroupName+"))"

# Get the search object
$oSearch = New-Object directoryservices.DirectorySearcher($oADRoot,$sSearchStr)

# Looking for the group
$oFindResult = $oSearch.FindAll()

# On success, get a DirectoryEntry object for the group
$oGroup = New-Object System.DirectoryServices.DirectoryEntry($oFindResult.Path)


# And list all members
If (($oGroup.Member).Count -ge 1)

%($oMembers = New-Object System.DirectoryServices.DirectoryEntry($sLDAPSearchRoot+"/"+$_))

Else
Write-Warning -Message "$($oGroup.Member) does not exist or has no members"






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55281588%2fstruggling-with-if-statements%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    What version of PowerShell are you running? Why are you not using the built-in AD group cmdlets for this or are you not using ADDS but some other LDAP service?



    Or you may be on OSX/Linux and are using PSCore, which the ADDS/RSAT cmdlets are not there, well, not yet?



    For your goals of …




    I want to check if the group exists and then if users are existing in
    this group.




    … On Windows, with PowerShell 3x or higher, it's really only this...



    # Get all AD groups and all members of each group
    Clear-Host
    (Get-ADGroup -Filter '*').Name |
    %
    "`n*** The members of $PSItem are as follows: ***`n"
    If((Get-ADGroupMember -Identity $PSItem).Count -ge 1)

    (Get-ADGroupMember -Identity $PSItem).SamAccountName

    Else

    Write-Warning -Message "$PSItem does not exist or has no members."




    # Filtered
    Clear-Host
    ((Get-ADGroup -Filter '*').Name -match 'Domain Admins|Domain Users' ) |
    %
    "`n*** The members of $PSItem are as follows: ***`n"
    If((Get-ADGroupMember -Identity $PSItem).Count -ge 1)

    (Get-ADGroupMember -Identity $PSItem).SamAccountName

    Else

    Write-Warning -Message "$PSItem does not exist or has no members."





    Using your LDAP approach though... How about this...



    'Administrators','Distributed COM Users' | 
    ForEach
    # Define LDAP search root, the Global catalog of the domain
    $sLDAPSearchRoot = "LDAP://$((Get-ADDomainController).IPv4Address):389"

    # The Groupname to looking for
    ($sGroupName = "$_")

    # The LDAP query - query string
    $sSearchStr = "(&(objectCategory=group)(name="+$sGroupName+"))"

    # Get the search object
    $oSearch = New-Object directoryservices.DirectorySearcher($oADRoot,$sSearchStr)

    # Looking for the group
    $oFindResult = $oSearch.FindAll()

    # On success, get a DirectoryEntry object for the group
    $oGroup = New-Object System.DirectoryServices.DirectoryEntry($oFindResult.Path)


    # And list all members
    If (($oGroup.Member).Count -ge 1)

    %($oMembers = New-Object System.DirectoryServices.DirectoryEntry($sLDAPSearchRoot+"/"+$_))

    Else
    Write-Warning -Message "$($oGroup.Member) does not exist or has no members"






    share|improve this answer





























      0














      What version of PowerShell are you running? Why are you not using the built-in AD group cmdlets for this or are you not using ADDS but some other LDAP service?



      Or you may be on OSX/Linux and are using PSCore, which the ADDS/RSAT cmdlets are not there, well, not yet?



      For your goals of …




      I want to check if the group exists and then if users are existing in
      this group.




      … On Windows, with PowerShell 3x or higher, it's really only this...



      # Get all AD groups and all members of each group
      Clear-Host
      (Get-ADGroup -Filter '*').Name |
      %
      "`n*** The members of $PSItem are as follows: ***`n"
      If((Get-ADGroupMember -Identity $PSItem).Count -ge 1)

      (Get-ADGroupMember -Identity $PSItem).SamAccountName

      Else

      Write-Warning -Message "$PSItem does not exist or has no members."




      # Filtered
      Clear-Host
      ((Get-ADGroup -Filter '*').Name -match 'Domain Admins|Domain Users' ) |
      %
      "`n*** The members of $PSItem are as follows: ***`n"
      If((Get-ADGroupMember -Identity $PSItem).Count -ge 1)

      (Get-ADGroupMember -Identity $PSItem).SamAccountName

      Else

      Write-Warning -Message "$PSItem does not exist or has no members."





      Using your LDAP approach though... How about this...



      'Administrators','Distributed COM Users' | 
      ForEach
      # Define LDAP search root, the Global catalog of the domain
      $sLDAPSearchRoot = "LDAP://$((Get-ADDomainController).IPv4Address):389"

      # The Groupname to looking for
      ($sGroupName = "$_")

      # The LDAP query - query string
      $sSearchStr = "(&(objectCategory=group)(name="+$sGroupName+"))"

      # Get the search object
      $oSearch = New-Object directoryservices.DirectorySearcher($oADRoot,$sSearchStr)

      # Looking for the group
      $oFindResult = $oSearch.FindAll()

      # On success, get a DirectoryEntry object for the group
      $oGroup = New-Object System.DirectoryServices.DirectoryEntry($oFindResult.Path)


      # And list all members
      If (($oGroup.Member).Count -ge 1)

      %($oMembers = New-Object System.DirectoryServices.DirectoryEntry($sLDAPSearchRoot+"/"+$_))

      Else
      Write-Warning -Message "$($oGroup.Member) does not exist or has no members"






      share|improve this answer



























        0












        0








        0







        What version of PowerShell are you running? Why are you not using the built-in AD group cmdlets for this or are you not using ADDS but some other LDAP service?



        Or you may be on OSX/Linux and are using PSCore, which the ADDS/RSAT cmdlets are not there, well, not yet?



        For your goals of …




        I want to check if the group exists and then if users are existing in
        this group.




        … On Windows, with PowerShell 3x or higher, it's really only this...



        # Get all AD groups and all members of each group
        Clear-Host
        (Get-ADGroup -Filter '*').Name |
        %
        "`n*** The members of $PSItem are as follows: ***`n"
        If((Get-ADGroupMember -Identity $PSItem).Count -ge 1)

        (Get-ADGroupMember -Identity $PSItem).SamAccountName

        Else

        Write-Warning -Message "$PSItem does not exist or has no members."




        # Filtered
        Clear-Host
        ((Get-ADGroup -Filter '*').Name -match 'Domain Admins|Domain Users' ) |
        %
        "`n*** The members of $PSItem are as follows: ***`n"
        If((Get-ADGroupMember -Identity $PSItem).Count -ge 1)

        (Get-ADGroupMember -Identity $PSItem).SamAccountName

        Else

        Write-Warning -Message "$PSItem does not exist or has no members."





        Using your LDAP approach though... How about this...



        'Administrators','Distributed COM Users' | 
        ForEach
        # Define LDAP search root, the Global catalog of the domain
        $sLDAPSearchRoot = "LDAP://$((Get-ADDomainController).IPv4Address):389"

        # The Groupname to looking for
        ($sGroupName = "$_")

        # The LDAP query - query string
        $sSearchStr = "(&(objectCategory=group)(name="+$sGroupName+"))"

        # Get the search object
        $oSearch = New-Object directoryservices.DirectorySearcher($oADRoot,$sSearchStr)

        # Looking for the group
        $oFindResult = $oSearch.FindAll()

        # On success, get a DirectoryEntry object for the group
        $oGroup = New-Object System.DirectoryServices.DirectoryEntry($oFindResult.Path)


        # And list all members
        If (($oGroup.Member).Count -ge 1)

        %($oMembers = New-Object System.DirectoryServices.DirectoryEntry($sLDAPSearchRoot+"/"+$_))

        Else
        Write-Warning -Message "$($oGroup.Member) does not exist or has no members"






        share|improve this answer















        What version of PowerShell are you running? Why are you not using the built-in AD group cmdlets for this or are you not using ADDS but some other LDAP service?



        Or you may be on OSX/Linux and are using PSCore, which the ADDS/RSAT cmdlets are not there, well, not yet?



        For your goals of …




        I want to check if the group exists and then if users are existing in
        this group.




        … On Windows, with PowerShell 3x or higher, it's really only this...



        # Get all AD groups and all members of each group
        Clear-Host
        (Get-ADGroup -Filter '*').Name |
        %
        "`n*** The members of $PSItem are as follows: ***`n"
        If((Get-ADGroupMember -Identity $PSItem).Count -ge 1)

        (Get-ADGroupMember -Identity $PSItem).SamAccountName

        Else

        Write-Warning -Message "$PSItem does not exist or has no members."




        # Filtered
        Clear-Host
        ((Get-ADGroup -Filter '*').Name -match 'Domain Admins|Domain Users' ) |
        %
        "`n*** The members of $PSItem are as follows: ***`n"
        If((Get-ADGroupMember -Identity $PSItem).Count -ge 1)

        (Get-ADGroupMember -Identity $PSItem).SamAccountName

        Else

        Write-Warning -Message "$PSItem does not exist or has no members."





        Using your LDAP approach though... How about this...



        'Administrators','Distributed COM Users' | 
        ForEach
        # Define LDAP search root, the Global catalog of the domain
        $sLDAPSearchRoot = "LDAP://$((Get-ADDomainController).IPv4Address):389"

        # The Groupname to looking for
        ($sGroupName = "$_")

        # The LDAP query - query string
        $sSearchStr = "(&(objectCategory=group)(name="+$sGroupName+"))"

        # Get the search object
        $oSearch = New-Object directoryservices.DirectorySearcher($oADRoot,$sSearchStr)

        # Looking for the group
        $oFindResult = $oSearch.FindAll()

        # On success, get a DirectoryEntry object for the group
        $oGroup = New-Object System.DirectoryServices.DirectoryEntry($oFindResult.Path)


        # And list all members
        If (($oGroup.Member).Count -ge 1)

        %($oMembers = New-Object System.DirectoryServices.DirectoryEntry($sLDAPSearchRoot+"/"+$_))

        Else
        Write-Warning -Message "$($oGroup.Member) does not exist or has no members"







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 22 at 2:29

























        answered Mar 22 at 1:48









        postanotepostanote

        4,0922411




        4,0922411





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55281588%2fstruggling-with-if-statements%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript