使用 powershell 和 export-excel 搜索远程计算机上文本文件的内容并导出到 excel 文件
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
开关将使其正常工作。
它是作为 2017 年 10 月 30 日发布的一部分添加的 - https://github.com/dfinke/ImportExcel#whats-new-in-release-52
我正在尝试从 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
开关将使其正常工作。
它是作为 2017 年 10 月 30 日发布的一部分添加的 - https://github.com/dfinke/ImportExcel#whats-new-in-release-52