我需要一些帮助来编写脚本,因为我正在努力理解逻辑。 我基本上有一个用户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
}
}
答案 0 :(得分:0)
选中此项以开始:
"user1","user2" | foreach {
$groups = get-qaduser $_ |select -expand memberof
if ($groups -match "GROUP1" -and $groups -match "GROUP2"){
echo $_
}
}
答案 1 :(得分: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