I've got a script to audit local user accounts. Everything works, except exporting to CSV. The output file is blank, except for the column headers:
Computer | Name | Enabled | PasswordChangeableDate | PasswordExpires | UserMayChangePassword | PasswordRequired | PasswordLastSet | LastLogon |
---|
What am I doing wrong here? The export portion is towards the bottom.
$Result = New-Object System.Collections.Generic.List[System.Object] #store local user data
$Errors = New-Object System.Collections.Generic.List[System.Object] #store any devices that could not be reached
$devices = Get-Content "list.txt" #read in all devices to check
#for progress bar - store current # and total
$length = $devices.Count
$i = 1
$displayOutputReport = $true
foreach ($device in $devices){
Write-Progress -Activity "Retrieving data from $device ($i of $length)" -percentComplete ($i++*100/$length) -status Running
if (Test-Connection -ComputerName $device -Count 1 -Quiet){
#Get account properties and add to list
$temp = Invoke-Command -ComputerName $device -ScriptBlock {
Get-LocalUser | Select-Object -Property @{N="Computer"; E={$env:COMPUTERNAME}}, Name, Enabled, PasswordChangeableDate, PasswordExpires, UserMayChangePassword, PasswordRequired, PasswordLastSet, LastLogon
}
$Result.Add($temp)
}else{
$errors.Add($device)
Write-Host "Cannot connect to $device, skipping." -ForegroundColor Red
}
}
#display results
if ($displayOutputReport){
if ($Result.Count -gt 0){
$Result | Format-Table Computer,Name, Enabled, PasswordChangeableDate, PasswordExpires, UserMayChangePassword, PasswordRequired, PasswordLastSet, LastLogon -AutoSize
}
Write-Host ""
if ($Errors.Count -gt 0){
Write-Host "Errors:" -ForegroundColor Red
$Errors
Write-Host ""
}
}
#export to CSV
$ScriptPath = $pwd.Path
$ReportDate = (Get-Date).ToString('MM-dd-yyyy hh-mm-ss tt')
if($Result.Count -gt 0){
$reportFile = $ScriptPath + "\LocalUserAudit $ReportDate.csv"
$Result | Select-Object Computer, Name, Enabled, PasswordChangeableDate, PasswordExpires, UserMayChangePassword, PasswordRequired, PasswordLastSet, LastLogon | Export-Csv $reportFile -NotypeInformation
Write-Host "Report saved to $reportFile"
}
if ($Errors.Count -gt 0){
$errorFile = $ScriptPath + "\LocalUserAudit ERRORS $ReportDate.txt"
$Errors | Out-File $errorFile
Write-Host "Errors saved to $errorFile"
}