检查广告组是否为空

时间:2019-06-25 16:07:22

标签: powershell

我正在寻找一种方法来首先检查AD组是否为空,如果是,则将诸如“ $ GroupName +为空”之类的内容写入CSV文件。我想这将是IF-THEN构造,但请注意确保将其放置在下面的代码或语法中。谢谢您的帮助。

#Script will export group members from the imported list of groups
#Import the AD Module necessary for the script to run
Import-Module ActiveDirectory
#specify the file that contains group names
$groups = Get-Content C:\temp\grouplist.txt
$UserProperties = 'GivenName', 'Surname', 'Name'

$resultsarray =@()

foreach ($group in $groups){

    $resultsarray += Get-ADGroupMember -Id $group -Server Server01.domain.com | Get-ADUser -Properties $UserProperties | select (@{n='FirstName';e={$_.GivenName}},
    @{n='LastName';e={$_.Surname}},'Name',@{e={$group};Label="GroupName"})

}
$resultsarray | Export-csv -path C:\temp\Groups_$(get-date -Format yyyy-MM-dd).csv -NTI

2 个答案:

答案 0 :(得分:0)

我还没有尝试过,但是可能是

In [256]: b = np.array([(2,b'xxx'),(34,b'xyz')], dtype=a.dtype)                                      
In [257]: b                                                                                          
Out[257]: array([( 2, b'xxx'), (34, b'xyz')], dtype=[('f0', '<u4'), ('f1', 'S10')])

答案 1 :(得分:0)

经过一番评论,我想我现在已经明白了何时应该为空的问题。
由于Get-ADGroupMember可以返回类型为'user','group'或'computer'的对象,因此代码需要处理该问题并根据每个成员的objectClass输出不同的结果。 但是,由于要将结果作为CSV文件,因此需要注意所有对象都返回相同的结构化对象。 以下应满足您的要求:

Import-Module ActiveDirectory
#specify the file that contains group names
$groups = Get-Content C:\temp\grouplist.txt
$UserProperties = 'GivenName', 'Surname', 'Name'

# loop through the groups and collect group member info in a variable '$result'
$result = foreach ($group in $groups){
    # get all members of the group and force it to be an array
    $groupMembers = @(Get-ADGroupMember -Identity $group -Server 'Server01.domain.com')
    if ($groupMembers.Count) {
        foreach ($member in $groupMembers) {
            switch ($member.objectClass) {
                'user' {
                    # emit an object with the following user properties
                    $member | Get-ADUser -Properties $UserProperties | 
                              Select-Object @{Name = 'FirstName'; Expression = {$_.GivenName}},
                                            @{Name = 'LastName'; Expression = {$_.Surname}},
                                            'Name',
                                            @{Name = 'MemberType'; Expression = {$member.objectClass}},
                                            @{Name = 'GroupName'; Expression = {$group}},
                                            @{Name = 'GroupIsEmpty'; Expression = {$false}}
                }
                default {
                    # for other objects like groups or computers, keep the same structure
                    # and only fill in the Name, MemberType and GroupName properties
                    '' | Select-Object 'FirstName','LastName',
                                        @{Name = 'Name'; Expression = {$member.Name}},
                                        @{Name = 'MemberType'; Expression = {$member.objectClass}},
                                        @{Name = 'GroupName'; Expression = {$group}},
                                        @{Name = 'GroupIsEmpty'; Expression = {$false}}
                }
            }
        }
    }
    else {
        # the group is empty. Again: keep the same structure as above
        '' | Select-Object 'FirstName','LastName','Name','MemberType',
                            @{Name = 'GroupName'; Expression = {$group}}, 
                            @{Name = 'GroupIsEmpty'; Expression = {$true}}
    }
}

$outFile = 'C:\temp\Groups_{0:yyyy-MM-dd}.csv' -f (Get-Date)
$result | Export-csv -path $outFile -NoTypeInformation -Force

注意:在这种情况下,您甚至不需要在-Properties cmdlet上使用Get-ADUser参数,因为所需的所有属性($UserProperties = 'GivenName', 'Surname', 'Name')都由默认