尝试使用Powershell从csv导入信息,但不幸的是某些结果出了错 [英] Trying to use Powershell to import information from a csv but unfortunately some of the results come out wrong

查看:164
本文介绍了尝试使用Powershell从csv导入信息,但不幸的是某些结果出了错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$AuditSuccess = Import-Csv -Path G:\LabLog.csv | Where-Object { $_.Keywords -like "Audit Success" } | Measure-Object | Select-Object count 

$AuditFailure = Import-Csv -Path G:\LabLog.csv | Where-Object { $_.Keywords -like "Audit Failure" } | Measure-Object | Select-Object count

$AuditTotal = $AuditSuccess + $AuditFailure

$EventID1 = Import-Csv -Path G:\LabLog.csv | sort | group Keywords | sort $_.EventID | select EventID -last 1

$EventID2 = Import-Csv -Path G:\LabLog.csv | sort | group Keywords | sort $_.EventID | select EventID -last 1


Write-Host "Number of Audit Failures:" $AuditFailure "failures of" $AuditTotal "entries"
Write-Host "Most Common Event ID:" $EventID1
Write-Host "Number of Audit Successes:" $AuditSuccess "successes of" $AuditTotal "entries"
Write-Host "Most Common Event ID:" $EventID2 

我刚接触Powershell,并尝试将其用于分配任务,因此我需要导入一个csv日志,然后从中提取特定信息,在这种情况下,所有日志中失败和成功的次数最多,失败和成功中的常见事件ID.

I'm fairly new to Powershell and attempting to use it for an assignment I need to import a csv log and then draw out specific information from it in this case the number of failures and successes out of all the logs and the most common event ID from the failures and from the successes.

该代码的AuditFailure和AuditSuccess部分在某种程度上起作用,尽管结果显示为{count = ##},而不是数字.真正的问题是AuditTotal和EventID在合计的情况下没有产生任何结果,在EventID的情况下给出的结果是空白.

The AuditFailure and AuditSuccess sections of the of the code are working somewhat although the results come out as {count = ##} as opposed to just numbers. The real issue is with the AuditTotal and the EventID which either aren't producing any result in the case of the total or giving a result that's blank in the case of the EventID.

我不知道这些命令是否是用于此目的的最佳命令,并且对解决此问题是否有任何帮助开放.

I don't know if these are the best commands to use for this and am open to any help in figuring this out.

Method invocation failed because [System.Management.Automation.PSObject] does 
not contain a method named 'op_Addition'.
At line:5 char:1
+ $AuditTotal = $AuditSuccess + $AuditFailure
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (op_Addition:String) [], 
RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

Number of Audit Failures: @{Count=13} failures of  entries
Most Common Event ID: @{EventID=}
Number of Audit Successes: @{Count=6480} successes of  entries
Most Common Event ID: @{EventID=}

对不起,这是错误输出

Number of Audit Failures: 2469 failures of 19247 entries                               
  Most common Event ID: 5038     
Number of Audit Successes: 16778 successes of 19247 entries                               
  Most common Event ID: 4624

这是应该看起来的样子,尽管数字本来应该是不同的

This is what it should look like although the numbers are meant to be different

推荐答案

这是另一种避免多次读取CSV文件的方法.它还避免了如此频繁地通过管道相当发送信息. [咧嘴]

here's another way that avoids re-reading the CSV file so many times. it also avoids sending things thru the pipeline quite so often. [grin]

# fake reading in a CSV file
#    in real life, use Import-CSV
$InStuff = @'
EventID, Keywords
1001, Audit Success; SomeOtherWord
1001, Audit Success
2002, NothingRightNow
3003, Audit Failure
4004, Audit Success
5005, IgnoreThisOne
6006, Audit Success
7007, Audit Failure
7007, Audit Failure
'@ | ConvertFrom-Csv

$SuccessList = $InStuff.Where({$_.Keywords -match 'success'})
$SuccessCount = $SuccessList.Count
$SL_MostFrequentEventID = ($SuccessList |
    Group-Object -Property EventID |
    Sort-Object -Property Count)[-1].Name


$FailureList = $InStuff.Where({$_.Keywords -match 'failure'})
$FailureCount = $FailureList.Count
$FL_MostFrequentEventID = ($FailureList |
    Group-Object -Property EventID |
    Sort-Object -Property Count)[-1].Name

$FS_TotalCount = $FailureCount + $SuccessCount

Write-Host ''
Write-Host ('Number of Audit Failures {0} out of {1} entries.' -f $FailureCount, $FS_TotalCount)
Write-Host ('    Most Common Failure Event ID = {0}' -f $FL_MostFrequentEventID)
Write-Host ('Number of Audit Successes {0} out of {1} entries.' -f $SuccessCount, $FS_TotalCount)
Write-Host ('    Most Common Success Event ID = {0}' -f $SL_MostFrequentEventID)

输出...

Number of Audit Failures 3 out of 7 entries.
    Most Common Failure Event ID = 7007
Number of Audit Successes 4 out of 7 entries.
    Most Common Success Event ID = 1001

这篇关于尝试使用Powershell从csv导入信息,但不幸的是某些结果出了错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆