Riffing on the answer by @josh morrish , here's another way to do this and only get the information from the AD once. It also exports the information about the computers that satisfy the conditions to a CSV file with just on row per computer.
$CsvFilePath = "c:\junk\InterestingComputers.csv"
$Who = [ordered]@{ComputerName="";Everyone=""; 'Domain Users'=""; Users=""} # template
# Check each computer
Get-ADComputer -Filter "Enabled -eq 'True'" -Properties nTSecurityDescriptor |
ForEach-Object{
$ShouldWrite = $false # not every active computer will be written to the output
$SDInfo = $who.Clone # make a fresh copy of the hash template
$SDInfo.ComputerName = $_.Name # fill in the name of the computer
# Check if 'Everyone', 'Domain Users', or 'Users' have Allow WriteAccountRestrictions
$securityDescriptor = ($_ | Select-Object -Expand nTSecurityDescriptor).Access
foreach($sd in $securityDescriptor){
if( $sd.ActiveDirectoryRights -eq 'WriteAccountRestrictions' -AND $sd.AccessControlType -eq 'Allow'){
if ($sd.IdentityReference -in 'Everyone', 'Domain Users', 'Users'){
$SDInfo[$sd.IdentityReference = "Y"
$ShouldWrite = $true # there's at least one condition satisfied, so export the info
}
}
}
if ($ShouldWrite){ # only computers that satisfy the condition(s) will be exported
[PSCustomObject]$SDInfo
}
} | Export-Csv -NoTypeInformation -Path $CsvFilePath