与If语句作斗争

时间:2019-03-21 13:31:26

标签: powershell if-statement logical-operators

我在理解PowerShell中变量的值时遇到问题,并使用if语句检查它们。

$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
    }
}

我要检查该组是否存在,然后检查该组中是否存在用户。我尝试了很多语句,但是似乎都没有。 即使在控制台中没有写下任何内容,它也不为null或为空。

这是我使用不存在的组时得到的:

Screenshot

有人可以给我解决方案吗?

1 个答案:

答案 0 :(得分:0)

您正在运行什么版本的PowerShell?为什么不为此使用内置的AD组cmdlet,或者为什么不使用ADDS而是其他一些LDAP服务?

或者您可能在OSX / Linux上并且正在使用PSCore,而ADDS / RSAT cmdlet还不存在?

为了……的目标

  

我要检查组是否存在,然后检查用户是否存在于   这个小组。

…在Windows上,使用PowerShell 3x或更高版本,实际上只是这个...

2019-03-22 02:35:08.854 12651-12651/com.example.dominik.smarthome2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.dominik.smarthome2, PID: 12651
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dominik.smarthome2/com.example.dominik.smarthome2.MainActivity}: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.example.dominik.smarthome2/.MyService }: app is in background uid UidRecord{ee07604 u0a297 TPSL idle procs:1 proclist:12651, seq(0,0,0)}
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3108)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3251)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1948)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7045)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)
 Caused by: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.example.dominik.smarthome2/.MyService }: app is in background uid UidRecord{ee07604 u0a297 TPSL idle procs:1 proclist:12651, seq(0,0,0)}
    at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1666)
    at android.app.ContextImpl.startService(ContextImpl.java:1611)
    at android.content.ContextWrapper.startService(ContextWrapper.java:677)
    at com.example.dominik.smarthome2.MainActivity.onCreate(MainActivity.java:122)
    at android.app.Activity.performCreate(Activity.java:7327)
    at android.app.Activity.performCreate(Activity.java:7318)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3088)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3251) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1948) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:214) 
    at android.app.ActivityThread.main(ActivityThread.java:7045) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)   

不过,请使用您的LDAP方法...怎么样...

# 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."

    }
}