0

我需要一些帮助来编写脚本,因为我正在努力理解逻辑。我基本上有一个用户 ID 列表,我需要检查他们是否有两个特定的 AD 组。如果有,则需要将它们输出到 csv 并突出显示。

任何人都可以帮助我开始吗?我需要使用 Quest Powershell cmdlet

这是代码

$textFileContents = Get-Content C:\temp\powershell\users.txt
$results = @()
foreach($username in $textFileContents){
$groups = get-qaduser $username |select -expand memberof 
if ($groups -match "grpuip1" -and $groups -match "group2"){
    echo $group
}
} 
4

2 回答 2

0

检查这个开始:

"user1","user2" | foreach { 
    $groups = get-qaduser $_ |select -expand memberof 
    if ($groups -match "GROUP1" -and $groups -match "GROUP2"){
        echo $_
    }
} 
于 2014-01-31T09:08:07.063 回答
0

我会使用 cmdlet Get-QADMemberOf 而不是 Get-QADUser。您正在做的事情并没有错,但它检索的信息超出了您的需要。

试试这个开始:

$textFileContents = Get-Content C:\temp\powershell\users.txt

# Rather than initializing the array, and adding new elements, 
# just output each element of the loop to the pipeline, and
# assign the results of the whole pipeline to the variable.
# This is *much* faster than adding to an array    
$results = $textFileContents | ForEach-Object {
  $userGroups = Get-QADMemberOf $_
  if ($userGroups -contains "group1" -and $userGroups -contains "group2") {
    New-Object -TypeName PSObject -Property @{"UserName" = $_; "Groups" = ($userGroups -join ",");}
  }
}

$results | ConvertTo-Csv -NoTypeInformation | Set-Content C:\Filename.txt
于 2014-01-31T17:12:12.340 回答