我正在創建一個腳本來獲取從文本文件讀入的一組PC的正常運行時間/最後一次重新啓動。格式化輸出文件
我可以輸出結果到文件,但它不斷重複我不想要的列標題。基本上我想輸出頭的第一行,然後每行輸出數據下面的數據。下面的代碼重複了輸出文件的列標題,並且行之間有很多空格。輸出到CSV會更容易處理?
這是我的代碼。第一個Out-File
命令是覆蓋文件,如果它存在,基本上清理文件。
$computers = Get-Content "c:\temp\ps\pc.txt"
out-file -FilePath "C:\temp\ps\output.txt"
foreach ($computer in $computers)
{
$Computerobj = "" | select ComputerName, Uptime, LastReboot
$wmi = Get-WmiObject -ComputerName $computer -Query "SELECT LastBootUpTime FROM Win32_OperatingSystem"
$now = Get-Date
$boottime = $wmi.ConvertToDateTime($wmi.LastBootUpTime)
$uptime = $now - $boottime
$d =$uptime.days
$h =$uptime.hours
$m =$uptime.Minutes
$s = $uptime.Seconds
$Computerobj.ComputerName = $computer
$Computerobj.Uptime = "$d Days $h Hours $m Min $s Sec"
$Computerobj.LastReboot = $boottime
$Computerobj
out-file -FilePath "C:\temp\ps\output.txt" -in $computerobj -append
}
這也很好。愛你的燻肉片! – user3364233