【发布时间】:2017-06-14 19:50:12
【问题描述】:
我想创建一个脚本,该脚本将访问 .txt 文件中的计算机,并从每个文件中获取最新的日志文件,并将文件和计算机名称输出到一个新文档中。我四处搜寻,确信我可以通过以某种方式结合以下两个示例来实现这一目标,但我不完全确定如何。 要从多台计算机上的同一位置获取文件:
$Computers = get-content "C:\Computers.txt"
$OutFile = "C:\Results.txt"
#Erase an existing output file so as not to duplicate data
out-file -filepath $OutFile
foreach ($Computer in $Computers)
{
if (test-path \\$computer\c$\temp\logfile.txt) #test to make sure the file
exists
{
#Get the CreationTime value from the file
$FileDate = (Get-ChildItem \\$computer\c$\temp\logfile.txt).CreationTime
#Write the computer name and File date separated by a unique character you
can open in Excel easy with"
"$Computer | $FileDate" | out-file -FilePath $OutFile -Append -Encoding
ascii
}
else
{
#File did not exist, write that to the log also
"$Computer | FILE NOT FOUND" | out-file -FilePath $OutFile -Append -Encoding
ascii
}
}
获取目录中的最新文件
$dir = "C:\test_code"
$latest = Get-ChildItem -Path $dir | Sort-Object LastAccessTime -Descending
| Select-Object -First 1
$latest.name
【问题讨论】:
标签: powershell