【问题标题】:Trying to figure out website failure rate and last date of failure with PowerShell on CSV尝试在 CSV 上使用 PowerShell 计算网站故障率和上次故障日期
【发布时间】:2019-12-24 22:41:07
【问题描述】:

我希望能够根据 .CSV 文档中包含的信息计算网站登录的成功率。我遇到的问题是我想单独获得每个网站的结果。现在我只能获得所有三个网站作为一个整体的结果。我能得到一些帮助吗?再次感谢!

.CSV 文件包含以下信息。

A Date Column with a Timestamp,  
A Status Column that says Success or Failure, and  
A column that has the websites. 

读取堆栈溢出和其他 PowerShell 资源


$sites = "Site 1", "Site 2", "Site 3"

foreach ($site in $sites) {
    # Total count of websites
    $total = $site.Column3.Count

    #Looking for websites that where sucessful based off of sucess in second column
    $Sucess = $total | Where-Object { $PSItem.Status -like "*Sucess*" } 

    # Calculating the percent of websites 
    $percent = [math]::Round($failed.Count / $total.count * 100, 1)

    #Going through CSV File to figure out last date of failure for wach website
    $date = $failed | Select date -Last 1 | Format-Table -HideTableHeaders | Out-String
    Write-Host ("{0} failed {1} % of the time on {2}" -f $site, $percent, $date)



I am trying to create a script with the following output as a .csv file: 
•   Website 1 was successfully able to connect %xx of the time the last failure date is (Date Goes here)
•   Website 2 was successfully able to connect %xx of the time the last failure date is (Date Goes here)  
•   Website 3 was successfully able to connect %xx of the time the last failure date is (Date Goes here)  

it now say: 
Website 1 Succeeded  0 % of the time on 
Website 2 Succeeded 0 % of the time on 
Website 3  Succeeded 0 % of the time on


A sample of the data looks like this: 
date                    status    website
Friday 08/02/2019 13:15 Success Website 1
Friday 08/02/2019 13:15 Failed  Website 2
Friday 08/02/2019 13:15 Success Website 3
Friday 08/02/2019 13:21 Success Website 1

【问题讨论】:

  • 这里绝对不需要Write-Host。我会删除它。
  • [1] 您对输出的评论...非常奇特。那不是 CSV 文件 - 它只是一个文本文件。 [grin] ///// [2] 请将示例输入数据添加到您的原始帖子中,以便人们可以看到您的代码为何如此编写......以及如何改进或修复它。
  • $PSItem.Status -like "*Sucess*" 拼写错误为 Success。因此,如果 CSV 拼写正确,则匹配结果为 0。 $total 只计算一个字段中一行的内容,因为您使用的是$site.Column3.count。如果该列不包含集合,那么您的意思可能是$total = $sites.Column3.Count,因此它不需要在循环中。还有一个变量$failed在示例中没有定义。
  • ATM 你的 foreach 迭代一个不能有任何属性的静态数组,那么数据应该从哪里来?
  • 这是我的表格数据示例。我不知道正确格式化表格

标签: powershell csv if-statement automation


【解决方案1】:

在这一行中,您调用 $failed.Count 而不定义 $failed 变量。

$percent = [math]::Round($failed.Count / $total.count * 100, 1)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-08
    • 1970-01-01
    • 2011-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多