Hello,
Please help, I don't know what's causing this - It's producing an empty csv file.
$computers = Get-ADComputer -Filter *
ForEach ($computer in $computers){
$Result = New-Object PSObject;
$Result | Add-Member NoteProperty -Name ComputerName -Value $Null;
$Result | Add-Member NoteProperty -Name UserName -Value $Null;
$Results = New-Object System.Collections.Generic.List[System.Object];
$TestResult = Test-Connection -CN $computer -Count 1 -BufferSize 16 -Quiet -ErrorAction Stop
if ($TestResult -eq $True){
try {
$Process = Get-WmiObject win32_process -computer $computer -Filter "Name = 'explorer.exe'"
ForEach ($p in $Process){
$username = ($p.GetOwner()).User
}
}
catch{
Write-Host = "PC is offline or RPC error."
}
$ResultEntry = $Result | Select-Object *
$Result.ComputerName = $computer;
$Result.UserName = $username;
[Void]$Results.Add($Result);
}
}
$Results | Export-Csv -Path C:\Users\samc\Documents\Test\test.csv -Append -NoTypeInformationPlease give help or advise.
Sam.