【发布时间】:2021-02-26 18:48:28
【问题描述】:
我有一个脚本来审核本地用户帐户。一切正常,除了导出到 CSV。输出文件为空白,列标题除外:
| Computer | Name | Enabled | PasswordChangeableDate | PasswordExpires | UserMayChangePassword | PasswordRequired | PasswordLastSet | LastLogon |
|---|
我在这里做错了什么?导出部分朝向底部。
$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"
}
【问题讨论】:
-
如果你只是
Out-File它,你会得到所有行吗? -
不走运。它垂直重复列标题 4 次(因为这是我的测试 list.txt 中的设备数)
标签: powershell