Home > front end >  No output to CSV using PowerShell psobject in Active Directory
No output to CSV using PowerShell psobject in Active Directory

Time:11-02

I have this portion of code here that has worked in the past in multiple AD environments, however after testing within a new AD environment, I am getting no output to CSV or any errors being thrown. The size of the CSV file is always zero.


if (![string]::IsNullOrEmpty($searchbase))
    {
    $ADComputers = get-adcomputer -searchBase $searchbase -filter * -properties * -ResultPageSize $resultpagesize
    }
    else
    {
    $ADComputers=Get-ADComputer -Filter * -Properties * -ResultPageSize $resultpagesize
    }

$data = @()
foreach ($computer in $ADComputers) {
    $computer.member| foreach-object {$members  = $_}
    $computer.memberof | foreach-object {$memberof  = $_}
    $memstr = ($members -join ";")
    $memstr2 = ($memberof -join ";")

        $ADcomp = Get-ADComputer $computer -properties logonCount, ManagedBy | select-object logonCount, ManagedBy
    
        $row = New-Object -TypeName psobject -Property @{
        PrincipalID = $script:ctr;
        logonCount=$ADcomp.logonCount;
        ManagedBy=$ADcomp.ManagedBy;
        
        }

    $data  = $row
    $script:ctr  
}
$data | Export-Csv "ADComputers.csv" -NoTypeInformation

I'm not sure exactly where to go from here because I have tested multiple different options, so any help would be greatly appreciated!

CodePudding user response:

The only reason you get no output is that $ADComputers has no elements. This may be related to a value in the variable $searchbase that does not exist or simply has no computer accounts in it.

But here are some general recommendations:

you do:

if (![string]::IsNullOrEmpty($searchbase))

you could also do:

If ($searchbase)

In principle if you have different scenarios to cover and so the parameter may change take a look at splatting.

Then you query all computers with all available properties but later in the loop you query the specific computer again which is not necessary. Also you should avoid adding elements to an array by using =, this causes to rebuild the array each time which is slow.

Furthermore $computer.memberof is already an array holding the information but you pipe it to foreach and build a new array holding the identical information only to join it later to a string.

If this is not part of function I don't know why you raise the scope of the variable $ctr from local to script think this is not necessary.

Putting this all together you could do:

#Define HashTable for splatting
$parametersHt = @{
    filer='*'
    properties='*'
    resultsetpagesize=$resultpagesize
}
#If searchbase is specified add parameter to splatting HashTable
If ($searchbase){
    $parametersHt.add('Searchbase',$searchbase)
}
#Get computers
$ADComputers = get-adcomputer @parametersHt
#Set Counter
$ctr = 0
$data = @(
    foreach ($computer in $ADComputers){
        $ctr  
        [PSCustomObject]@{
            PrincipalId = $ctr #Really the counter here - not $computer.samaccountname?
            logonCount=$computer.logonCount
            manageddBy=$computer.ManagedBy
            memberof=($computer.memberof -join ";") #added this, but in your code sample you don't return this value, if not needed remove. btw. a computer can be memberof a group but it can't have members
        }
    }
)
$data | Export-Csv -path ".\ADComputers.csv" -NoTypeInformation 
  • Related