对于我的生活,我无法弄清楚我在这里做错了什么。我确定我没有正确理解包含的最后一个elseif语句,但我读过的每个例子都告诉我,我做得对。
我写的这个脚本首先检查父组是否包含子组,如果没有,它会添加组并进行最终检查并输出"是"或"否"取决于组嵌套是否发生。
但是无论出于什么原因,输出总是" No",即使已经发生了组嵌套。当我在AD中查看父组时,我看到添加了子组,当我查看user3.txt时,我看到子组名称" bathroom"在文本文件中..我做错了什么?
$group = "bathroom"
$validate = Get-ADGroupMember -identity BuildingGroups | format-table -HideTableHeaders "name" | out-file .\bathroom\user.txt -Append
$validate2 = get-content .\bathroom\user.txt
$Dest = ".\bathroom\user.txt"
$childgroup = Get-ADGroup $childgroupOU
$parentgroup = Get-ADGroup $parentgroupOU
If (Select-String -Path $Dest -Pattern $group) {
Write-Host "$group is already a member of BuildingGroups."
}
elseif ($validate2 -notcontains $group) {
try {
Add-ADGroupMember $parentgroup –Member $childgroup –Server "$prefix.$root.ca"
Get-ADGroupMember -identity BuildingGroups | format-table -HideTableHeaders "name" | out-file .\bathroom\user2.txt -Append
}
catch {
$_.Exception | out-file .\bathroom\addgroup_error.txt -Append
}
}
(gc .\bathroom\user2.txt) | ? {$_.trim() -ne "" } | set-content .\bathroom\user3.txt
$validate4 = Get-Content .\bathroom\user3.txt
If ($validate4 -contains $group) {
Write-Host "Yes"
}
elseif ($validate4 -notcontains $group) {
Write-Warning "No"
}
答案 0 :(得分:0)
$group = "bathroom"
$validate = Get-ADGroupMember -identity BuildingGroups | format-table -HideTableHeaders "name" | out-file .\bathroom\user.txt -Append
$validate2 = get-content .\bathroom\user.txt
$Dest = ".\bathroom\user.txt"
$childgroup = Get-ADGroup $childgroupOU
$parentgroup = Get-ADGroup $parentgroupOU
If (Select-String -Path $Dest -Pattern $group) {
Write-Host "$group is already a member of BuildingGroups."
}
elseif ($validate2 -notcontains $group) {
try {
Add-ADGroupMember $parentgroup –Member $childgroup –Server "$prefix.$root.ca"
Get-ADGroupMember -identity BuildingGroups | format-table -HideTableHeaders "name" | out-file .\bathroom\user2.txt -Append
}
catch {
$_.Exception | out-file .\bathroom\addgroup_error.txt -Append
}
}
$containsWord = Get-Content .\bathroom\user2.txt | %{$_ -match $group}
If ($containsWord -contains $true) {
Write-Host "Yes"
}
elseif ($validate4 -notcontains $group) {
Write-Warning "No"
}