Export computer name from after .ntSecurityDescriptor output

Viewed 39

I have the following code, I'm just trying to export what security groups are attached to each Domain Controller.

I have so far made this, however! I cannot seem to export the computer names along side the Groups in the output! Can someone potentially help! I've been going round in circles for hours

$ou = "OU=Domain Controllers,DC=example,DC=com" 
$user = Get-ADComputer -SearchBase $ou -Filter *  -properties * | Select ntSecurityDescriptor, Name
    $user2 = $user.ntSecurityDescriptor.Access |
     select-object -expandproperty IdentityReference |
     sort-object -unique   
    if ($user2-eq "example\user") {
                #They Match 
                Write-Host "Oh Sugar"
                
                $user.ntSecurityDescriptor.Access |
                Select-Object -expandproperty IdentityReference |
                 sort-object -unique 
                
            } else {
            Write-Host "Phew"
            
            } 

Update 20th August. I updated my code to loop around and show the computer name in the output.

$ou = "OU=Domain Controllers,DC=example,DC=com" 
$computers = Get-ADComputer -SearchBase $ou -Filter *  -properties ntSecurityDescriptor, Name

$code = foreach ($computer in $computers)
{
   Get-ADComputer $computer  -properties ntSecurityDescriptor
  $owner = $computer.ntSecurityDescriptor.Access |
            Select-Object -expandproperty IdentityReference |
           sort-object -unique 
$export  =   if ($owner-eq "example\user")  {
            #Problem found
         $computer.Name
 
            
        } else {
        
        
        }  
        
$export | Out-File 'C:\Temp\output.csv' -Append
   
}
 
0 Answers
Related