微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在远程计算机上搜索文本文件的内容并使用 powershell 和 export-excel 导出为 excel 文件

如何解决在远程计算机上搜索文本文件的内容并使用 powershell 和 export-excel 导出为 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()
    }
}

问题在于它只会导出 pc4 列表中最后一个 computers.txt内容

提前致谢

解决方法

-append 上添加 export-excel 开关将使其正常工作。

它是在 2017 年 10 月 30 日发布的版本中添加的 - https://github.com/dfinke/ImportExcel#whats-new-in-release-52

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。