簡體   English   中英

在遠程計算機上搜索文本文件的內容並使用 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開關將使此工作正常。

它作為 10/30/2017 發布的一部分添加 - https://github.com/dfinke/ImportExcel#whats-new-in-release-52

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM