我正在创建一个脚本,以便从文本文件中读取一组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
}
答案 0 :(得分:3)
使用<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="fill"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.joker.charsoo.Main"
>
<view
android:id="@+id/view1"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="ScrollView" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:orientation="vertical" >
<ImageView
android:id="@+id/main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="117dp"
android:layout_marginTop="88dp"
android:src="@drawable/ic_launcher" />
</LinearLayout>
和ForEach-Object
的管道是更好的方法:
Export-Csv
如果您需要文件和控制台上的数据,可以使用$now = Get-Date
Get-Content -Path 'C:\temp\ps\pc.txt' | ForEach-Object {
$wmi = Get-WmiObject -ComputerName $_ -Class Win32_OperatingSystem
$boottime = $wmi.ConvertToDateTime($wmi.LastBootUpTime)
$uptime = $now - $boottime
New-Object -Type PSObject -Property @{
'ComputerName' = $_
'Uptime' = '{0} Days {1} Hours {2} Min {3} Sec' -f $uptime.Days,
$uptime.Hours, $uptime.Minutes, $uptime.Seconds
'LastReboot' = $boottime
}
} | Export-Csv -Path 'C:\temp\ps\output.txt' -NoType
和ConvertTo-Csv
:
Tee-Object
答案 1 :(得分:0)
试试这个:
$computers = Get-Content "c:\temp\ps\pc.txt"
#Create a report variable as an array to hold all our data
$report = @();
#No longer necessary
#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
#Add the computer to the report array
$report += $Computerobj
}
#Uncomment this if you need to see the report as well as write it to a file
#Write-Output $report
out-file -FilePath "C:\temp\ps\output.txt" -in $report
现在,您可以整体处理报告,因此您甚至可以在最后添加内容,例如$report = $report | Sort-Object -Property ComputerName
,以便按计算机名称对报告进行排序,或者使用Where-Object
过滤报告。