I am trying to get the last logonDate of all servers in OU with Powershell.
My script is able to collect the data but unable to write it to the csv report. (The current version populates only the Server name and not the OS and LogonDate fields in the csv file) Need help with adjusting the output and if you can suggest improvements would also be great
$allservers = get-adcomputer -filter * -searchbase "ou=important, ou=Member Servers, dc=contoso, dc=com" | select-object -expand name
$File = "$(Get-Date -Format ddMMyy_HHmmss)"
foreach ($server in $allservers) {
Get-ADComputer -Identity $server -Properties * | format-table Name, LastLogonDate, OperatingSystem
$Props = [ordered]@{
ServerName = $server
OperatingSystem = $server.OperatingSystem
LastLogonDate = $server.LastLogonDate
}
$Obj = New-Object psobject -Property $Props
$obj | Export-Csv -Path .\$File.csv -NoTypeInformation -Encoding UTF8 -Append
}