将PowerShell对象导出到文件

时间:2018-01-26 12:03:15

标签: powershell

我编写了一个简单的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



Edit:

After applying the comments from Ansgar Wiechers this is the modified version of the code, that works but the output is duplicated 4 times. please see below. wondering where the code is iterating for the result to be displayed times * 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

2 个答案:

答案 0 :(得分:1)

问题是你在循环中一遍又一遍地写$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

答案 1 :(得分:1)

正如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
    }
}