将PowerShell对象导出到文件

c9qzyr3d  于 2023-08-05  发布在  Shell
关注(0)|答案(2)|浏览(156)

我写了一个简单的PowerShell代码,并想导出结果到一个文件,我得到的结果正确输出到控制台,但只有得到最后的结果时,我试图导出到一个文件或CSV。你能帮我解决我哪里出了问题吗?

$files = Get-ChildItem -Path C:\inetpub\SigningWebAPI\Logs\ | Sort-Object lastwritetime
$qapattern = '[NGI][QA]'
$prodpattern = '[NGI][PROD]'

foreach ($file in $files) {
    $Totalrequests = (Get-Content -Path $file.FullName | Select-String "started signing process")
    $QArequests    = (Get-Content -Path $file.FullName | Select-String -SimpleMatch $qapattern)
    $Prodrequests  = (Get-Content -Path $file.FullName | Select-String -SimpleMatch $prodpattern)

    $object = New-Object –TypeName PSObject
    $object | Add-Member –MemberType NoteProperty –Name Date –Value ($file.LastWriteTimeUtc.Date).ToShortDateString() -PassThru
    $object | Add-Member –MemberType NoteProperty –Name QAKeyrequests –Value $QArequests.count -PassThru
    $object | Add-Member –MemberType NoteProperty –Name ProdKeyrequests –Value $Prodrequests.count -PassThru
    $object | Add-Member –MemberType NoteProperty –Name TotalRequestsMade –Value $Totalrequests.count -PassThru
}
$object | Export-Csv -Path C:\temp\results.csv

字符串

编辑

在应用Ansgar Wiechers的评论后,这是代码的修改版本,它可以工作,但输出重复了4次。请看下面。我想知道代码在哪里迭代,以便显示结果 * 4

$files = Get-ChildItem -Path C:\inetpub\SigningWebAPI\Logs\ | Sort-Object lastwritetime
$qapattern = '[NGI][QA]'
$prodpattern = '[NGI][PROD]'

foreach ($file in $files) {
    $Totalrequests = (Get-Content -Path $file.FullName | Select-String "started signing process")
    $QArequests    = (Get-Content -Path $file.FullName | Select-String -SimpleMatch $qapattern)
    $Prodrequests  = (Get-Content -Path $file.FullName | Select-String -SimpleMatch $prodpattern)

    $object = New-Object –TypeName PSObject
    $object | Add-Member –MemberType NoteProperty –Name Date –Value ($file.LastWriteTimeUtc.Date).ToShortDateString() -PassThru
    $object | Add-Member –MemberType NoteProperty –Name QAKeyrequests –Value $QArequests.count -PassThru
    $object | Add-Member –MemberType NoteProperty –Name ProdKeyrequests –Value $Prodrequests.count -PassThru
    $object | Add-Member –MemberType NoteProperty –Name TotalRequestsMade –Value $Totalrequests.count -PassThru
}
$object | Export-Csv -Path C:\temp\results.csv

##output:
Date                       QAKeyrequests     ProdKeyrequests   TotalRequestsMade
----                       -------------     ---------------   -----------------
31/10/2017                             0                   0                   0
31/10/2017                             0                   0                   0
31/10/2017                             0                   0                   0
31/10/2017                             0                   0                   0
03/11/2017                             0                   0                   7
03/11/2017                             0                   0                   7
03/11/2017                             0                   0                   7
03/11/2017                             0                   0                   7
04/11/2017                             0                   0                   0
04/11/2017                             0                   0                   0
04/11/2017                             0                   0                   0
04/11/2017                             0                   0                   0
05/11/2017                             0                   0                   0
05/11/2017                             0                   0                   0
05/11/2017                             0                   0                   0
05/11/2017                             0                   0                   0
06/11/2017                             0                   0                  34
06/11/2017                             0                   0                  34
06/11/2017                             0                   0                  34
06/11/2017                             0                   0                  34

a8jjtwal

a8jjtwal1#

正如veefu已经指出的那样,每次迭代都要替换$object,所以在循环结束后,最终只得到最后一个对象。只需创建并输出循环中的对象,并将整个循环输出收集到一个变量中。我会避免Add-Member。你可以通过传递一个哈希表来直接创建具有属性的对象:

$object = foreach ($file in $files) {
    # ...

    New-Object –TypeName PSObject -Property @{
        'Date'              = ($file.LastWriteTimeUtc.Date).ToShortDateString()
        'QAKeyrequests'     = $QArequests.count
        'ProdKeyrequests'   = $Prodrequests.count
        'TotalRequestsMade' = $Totalrequests.count
    }
}

字符串
如果你有PowerShell v3或更高版本,你也可以使用[PSCustomObject]类型的加速器,而不是New-Object

$object = foreach ($file in $files) {
    # ...

    [PSCustomObject]@{
        'Date'              = ($file.LastWriteTimeUtc.Date).ToShortDateString()
        'QAKeyrequests'     = $QArequests.count
        'ProdKeyrequests'   = $Prodrequests.count
        'TotalRequestsMade' = $Totalrequests.count
    }
}

vyswwuz2

vyswwuz22#

问题是你在循环中一遍又一遍地写$object。您没有累积循环输出的代码。这里有一个愚蠢的例子,你可以如何积累:

$inputThings = @("oneThing","twoThing","threeThing","fourThing")

$outputThings = foreach($thing in $inputThings)  {
    $outputThing = [psobject]::new()
    $outputThing | Add-Member -membertype NoteProperty -name OutputThing -value $thing
    Write-Output $outputThing
}
$outputThings |ConvertTo-Csv

字符串

相关问题