I want to create a script that will access computers on a .txt file and grab the latest log file from each and output the file and computer name into a new document. I've searched around feel confident that I can achieve this by combining the following two examples in some way, but I'm not entirely sure how. To get files from the same location on multiple computers:
$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
}
}
To get the latest file in a directory
$dir = "C:\test_code"
$latest = Get-ChildItem -Path $dir | Sort-Object LastAccessTime -Descending
| Select-Object -First 1
$latest.name