0

我的pscode有问题。

Import-Module ActiveDirectory

配置

$counter = 0
$allready_members = 0
$allready_members_file = "c:\temp\allready_members.txt"
$new_members = 0
$new_members_file = "c:\temp\new_members.txt"
$groupname = "EU-GPO-CTXPVS.MinimumIdleSessionTime.Users"
$fnc_accounts = @()
$allready_member_accounts = @()

开始

Write-Host "-----------------------------------------------------------------"
Write-Host "- querying ActiveDirectory for FNC-accounts..."
Write-Host "-----------------------------------------------------------------"

Clear-Content $new_members_file
Clear-Content $allready_members_file

$fnc_accounts = Get-ADUser -Properties MemberOf, DisplayName, samAccountName, PasswordExpired -Filter  {(Enabled -eq "True") -and (PasswordNeverExpires -eq "True")} | ? { ($_.distinguishedname -like '*OU=FNC*') } | select Displayname, samAccountName, PasswordExpired 

$allready_member_accounts = Get-ADGroupMember $groupname -Recursive | select samAccountName

主要的

foreach($fncaccount in $fnc_accounts) {

    if (($allready_member_accounts -notcontains $fncaccount.samAccountName)){
        #Add-ADGroupMember -Identity $groupname -Members $fncaccount.sAMAccountName
        Write-Host "+ User "$fncaccount.SamAccountName" added to "$groupname
        #Add-Content -Path $new_members_file $fncaccount.samAccountName
        $new_members++
    } else {    
        Write-Host "- User "$fncaccount.SamAccountName" is already a member of "$groupname
        #Add-Content -Path $allready_members_file $fncaccount.samAccountName
        $allready_members++
    }

$counter++

}

结尾

Write-Host "-----------------------------------------------------------------" 
Write-host "- # FNC count          : "$fnc_accounts.Count
Write-Host "- # allready count     : "$allready_member_accounts.Count
Write-host "- # allready counter   : "$allready_members
Write-host "- # new members counter: "$new_members
Write-host "- # items processed    : "$counter
Write-Host "-----------------------------------------------------------------" 

我的 ps 脚本似乎没有在我的数组中找到匹配项。有任何想法吗 ?我已经使用计数器查看我在 foreach 中的检查是否正确。

4

1 回答 1

0

我已经通过使用 Compare-Object CmdLet 解决了这个问题

$new_members_array = Compare-Object (Get-Content $path_fncaccounts_file) (Get-Content $path_already_members_file) | where {$_.SideIndicator -eq "<="} 

最终我的问题解决了!

于 2013-11-18T14:39:59.353 回答