[英]Searching contents of text files on remote computers and exporting to excel file using powershell and export-excel
我正在嘗試從computers.txt
搜索遠程計算機上的文本文件的內容,其中包括
Pc1
Pc2
Pc3
Pc4
並使用以下代碼使用export-excel
PowerShell 模塊導出它:
$directory = $PSScriptRoot
$computers = Get-Content -Path $directory\computers.txt
$searchwords = 'word1','word2','word3'
Foreach ($computer in $computers) {
$path = "\\$computer\C$\test\logs"
Foreach ($sw in $searchwords) {
$excel = Get-Childitem -path $path -recurse -Include "*.txt" |
Select-string -pattern "$sw" |
Select-object pattern, linenumber, line, path |
Export-excel $file -autosize -startrow 1 -tablename pattern -worksheetname "errors" -passthru
$ws = $excel.workbook.worksheets['errors']
$excel.save()
}
}
問題是它只會導出computers.txt
列表中最后一個pc4
的內容。
提前致謝
在export-excel
上添加-append
開關將使此工作正常。
它作為 10/30/2017 發布的一部分添加 - https://github.com/dfinke/ImportExcel#whats-new-in-release-52
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.