locked
User Ids status not coming in output file RRS feed

  • Question

  • HI 

    I need one help on below powerhsell script fo output. I am not getting samaccountname in output file and if user already disabled, that status also not coming in outupt.

    $attr65= Import-Csv "D:\temp\users_to_be_deprovisioned.csv" | Select-Object -ExpandProperty Name
    $date = (Get-Date).ToString("M-d-yyyy")
    
    foreach ($attr65a in $attr65)
    {
    
    $user = @{}
    $user=get-aduser -Filter '(cAHAttribute65 -eq $attr65a)' -SearchBase "DC=controso, dc=com" -Server "NF01.controso.com" -Properties samaccountname,enabled | Select samaccountname,enabled | Disable-ADAccount -ErrorAction SilentlyContinue
    
    #Set-ADUser -Identity $user.samaccountname -Description "Disabled on $date by the automated deprovisioning script"
    
    if($user.enabled -eq '$True') {$result = "Account not disabled"}
    
    else
    
    {$result = "Account successfully Disabled"}
    
    
    $array =@{}
    $array.add("samaccountname",$user.samaccountname)
    $array.add("enabled",$user.enabled)
    $array.add("Status", $result)
    $out = New-object psobject -property $array | select-object @{N="Samaccountname";E={$_.samaccountname}},@{N="Enabled";E={$_.enabled}},@{N="Status";E={$result}} | Export-Csv .\out.csv -nti
    }
    

    "Samaccountname","Enabled","Status"
    ,,"Account successfully Disabled"

    Thursday, July 23, 2020 8:36 PM

All replies

  • if($user = get-aduser -Filter "cAHAttribute65 -eq '$attr65a'" -SearchBase 'DC=controso, dc=com' -Server NF01.controso.com){
        Write-Host "User $attr65a found."
        if($user.Count -gt 1){
            Write-Host 'Too many users found'
            # skip user
        }else{
            # process user here
        }
    }else{
        Write-Host "User $attr65a NOT found." -fore Red
        # skip user
    }


    \_(ツ)_/


    • Edited by jrv Thursday, July 23, 2020 8:50 PM
    Thursday, July 23, 2020 8:49 PM
  • Thank JRV

    it's helped me a lot but in result in output.csv I am getting for account disable for all the users..

    Friday, July 24, 2020 10:14 AM