2
我想創建一個腳本,該腳本將訪問.txt文件中的計算機並從每個計算機獲取最新的日誌文件,並將文件和計算機名稱輸出到新文檔中。我四處搜尋,覺得自信可以通過以某種方式結合以下兩個示例來實現這一目標,但我並不完全知道如何。 要在多臺計算機上的相同位置獲得文件:從多臺計算機(powershell)獲取最新日誌文件
$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