【问题标题】:Filtering Json data, no results过滤Json数据,没有结果
【发布时间】:2021-11-15 04:31:40
【问题描述】:

我的 JSON 文件 export.json 有这个 content

[
  {
    "Workspace Name": "IAP IPP DW - PRD",
    "Workspace Allowance": "130",
    "Workspace Usage": "108.9413",
    "Workspace Size Free": "21.1",
    "Percentage Utilization": "0.83801",
    "Predicted Usage": "146.9888958"
  },
  {
    "Workspace Name": "Connected planning Facilities-PRD",
    "Workspace Allowance": "130",
    "Workspace Usage": "81.2462",
    "Workspace Size Free": "48.8",
    "Percentage Utilization": "0.624970769",
    "Predicted Usage": "85.79967522"
  },
  {
    "Workspace Name": "Lubes SDA - PROD",
    "Workspace Allowance": "100",
    "Workspace Usage": "78.5773",
    "Workspace Size Free": "21.4",
    "Percentage Utilization": "0.785773",
    "Predicted Usage": "87.03502547"
  },
  {
    "Workspace Name": "IAP IPP DW - ACC",
    "Workspace Allowance": "100",
    "Workspace Usage": "56.272",
    "Workspace Size Free": "43.7",
    "Percentage Utilization": "0.56272",
    "Predicted Usage": "95.58173647"
  },
  {
    "Workspace Name": "ONE Forecast UPD - PRD",
    "Workspace Allowance": "100",
    "Workspace Usage": "42.1855",
    "Workspace Size Free": "57.8",
    "Percentage Utilization": "0.421855",
    "Predicted Usage": "51.76880067"
  },
  {
    "Workspace Name": "IAP IPP DW - DEV",
    "Workspace Allowance": "50",
    "Workspace Usage": "38.7354",
    "Workspace Size Free": "11.3",
    "Percentage Utilization": "0.774708",
    "Predicted Usage": "33.98943696"
  }
]

当我在 PowerShell 中编写以下代码时,它会显示 Connected planning Facilities-PRDLubes SDA - PROD 的数据集,但不会显示 IAP IPP DW - PRD 的数据集,因为该模型的 Workspace Usage 也是 108,所以理想情况下应该显示。

$variable =Get-Content "export.json" | ConvertFrom-Json
# Write-Output $variable
$logs=$variable | Where-Object { $_.'Workspace Usage' -ge "75" } 
Write-Output $logs

【问题讨论】:

  • 使用$_.'Workspace Name'
  • @SiddharthaDas 你确定吗? $variables| Where-Object { $_.'Workspace Name' -eq "IAP IPP DW - PRD" } 向我展示了预期的项目
  • 是的,它现在按预期工作,谢谢。但是我已经编辑了这个问题,请你帮忙。
  • 我已经发布了您问题的答案。请在下方查看
  • 在修改后的问题中,使用Where-Object { [double]$_.'Workspace Usage' -ge 75 }

标签: json powershell


【解决方案1】:

function HtmlTable
{
     [OutputType([System.Object])]

    Param (
        
        [Parameter(Mandatory=$True)]
        [String[]]
        $tableRows,
        [Parameter(Mandatory=$True)]
        [String[]] 
        $tableHeaders,
        [Parameter(Mandatory=$True)] 
        $tableWidthPercentage
    )        
    $htmTable = "<html>
                <style>
                body {font-family:Segoe UI, Helvetica Neue, Helvetica, Arial, Verdana;font-size:12px;}
                table{width: $tableWidthPercentage;border-collapse: collapse;}
                th{background-color:#106ebe; color:white;font-family:Segoe UI, Helvetica Neue, Helvetica, Arial, Verdana;font-size:12px;border: 1px solid black;padding: 4px;text-align: left;border: 1px solid #ddd;}
                tr:nth-child(even){background-color: #f2f2f2;}
                td{font-family:Segoe UI, Helvetica Neue, Helvetica, Arial, Verdana;font-size:12px;border: 1px solid #ddd;padding: 4px;}
                </style>
                <table>
                <tr>"
    Foreach ($header in $tableHeaders)
    {
        $htmTable +=  "<th>$header</th>"
    }
   
    $htmTable +="</tr>
                $tableRows
                </table>"
    return $htmTable
}


$variable =Get-Content -raw "export.json" | ConvertFrom-Json
#$logs=$variable | Where-Object { $_.'Workspace Name' -eq "IAP IPP DW - PRD" -and  $_.'Workspace Usage' -gt 75} 
$logs=$variable | Where-Object { $_.'Workspace Usage' -gt 75 } 

foreach($item in $logs)
{
        
    $WorkspaceName =$item.'Workspace Name'
    $WorkspaceAllowance=$item.'Workspace Allowance'
    $WorkspaceUsage=$item.'Workspace Usage '
    $WorkspaceSizeFree=$item.'Workspace Size Free'
    $PercentageUtilization=$item.'Percentage Utilization'
    $PredictedUsage=$item.'Predicted Usage'
    
    $resultRow = "
                <tr>
                <td>$WorkspaceName</td>
                <td>$WorkspaceAllowance</td>
                <td>$WorkspaceUsage</td>
                <td>$WorkspaceSizeFree</td>
                <td>$PercentageUtilization</td>
                <td>$PredictedUsage</td>
                </tr>
                "
    $table_content += $resultRow
}


$body = HtmlTable -tableWidthPercentage 90% -tableRows $table_content -tableHeaders WorkspaceName,WorkspaceAllowance,WorkspaceUsage,WorkspaceSizeFree,PercentageUtilization,PredictedUsage


Send-MailMessage -From <from address> -To <to address> -Cc <cclist> -SmtpServer <smtp server address> -Subject "testing script" -Body $body -BodyAsHtml
Clear-Variable -Name "logs"
Write-Host "Done Sending Email"



<#
Please update the below details from the PowerShell script
Send-MailMessage -From <from address> -To <to address> -Cc <cclist> -SmtpServer <smtp server address> -Subject "testing script" -Body $body -BodyAsHtml
#>


***PFA for sample output. Also please mark this as answer if you feel this is an answer***

【讨论】:

  • 非常感谢您的帮助。它完美地工作。唯一需要担心的是主题行是“不受信任的消息:TEST EMAIL”,尽管我只给出了 TEST EMAIL 作为主题
【解决方案2】:

问题在于所有的 JSON 反序列化值都是字符串。您打算进行数值比较,并且必须确保所比较的所有数据都是数值类型。

$variable | Where { [double]$_.'Workspace Usage' -ge 75 }

输出:

Workspace Name         : IAP IPP DW - PRD
Workspace Allowance    : 130
Workspace Usage        : 108.9413
Workspace Size Free    : 21.1
Percentage Utilization : 0.83801
Predicted Usage        : 146.9888958

Workspace Name         : Connected planning Facilities-PRD
Workspace Allowance    : 130
Workspace Usage        : 81.2462
Workspace Size Free    : 48.8
Percentage Utilization : 0.624970769
Predicted Usage        : 85.79967522

Workspace Name         : Lubes SDA - PROD
Workspace Allowance    : 100
Workspace Usage        : 78.5773
Workspace Size Free    : 21.4
Percentage Utilization : 0.785773
Predicted Usage        : 87.03502547

运算符的左侧 (LHS) 会影响运算符的 RHS 的类型如果可以在 RHS 上进行类型转换。因此,通过将 LHS 转换为 [double],RHS 上的数字 (75) 也将转换为 [double]。然后比较按预期执行。

【讨论】:

  • 非常感谢。
  • 当我发送工作空间轴承使用 -ge 75 时,我在收件箱中收到一封非常未格式化的邮件,就像附在这篇文章中一样。 (邮件片段)。我希望它采用如下有组织的格式 工作空间名称:Lubes SDA - PROD 工作空间津贴:100 工作空间使用量:78.5773 工作空间大小免费:21.4 百分比利用率:0.785773 预测使用量:87.03502547
  • 我正在使用此代码发送邮件。尝试 { Send-MailMessage @smtpsettings -Body $messagebody -BodyAsHtml -Encoding utf8 -verbose -ErrorAction Stop } catch { Write-Warning $_.Exception.Message }
  • 这是因为您的电子邮件正文不是字符串。它是一个对象数组,将被转换为-Body 参数的字符串。您需要先根据自己的喜好构建自己的字符串。你可以做类似-Body ($messagebody | Out-String)
  • 如何使用值“工作区名称”更新 excel/csv 的单元格 A1
【解决方案3】:
$variable = Get-Content -raw "export.json" | ConvertFrom-Json
$logs = $variable | Where-Object { $_.'Workspace Usage' -gt 75 } 

Write-Output $logs

输出

Workspace Name         : Connected planning Facilities-PRD
Workspace Allowance    : 130
Workspace Usage        : 81.2462
Workspace Size Free    : 48.8
Percentage Utilization : 0.624970769
Predicted Usage        : 85.79967522

Workspace Name         : Lubes SDA - PROD
Workspace Allowance    : 100
Workspace Usage        : 78.5773
Workspace Size Free    : 21.4
Percentage Utilization : 0.785773
Predicted Usage        : 87.03502547

【讨论】:

  • 我无法仅过滤掉工作空间使用率大于等于 75 的工作空间名称。我已将输出屏幕截图作为 OUTPUT FILE 附加到我的帖子中。
  • $variable =Get-Content -raw "export.json" | ConvertFrom-Json $logs=$variable | Where-Object { $_.'Workspace Usage' -gt 75 } 写入输出 $logs
  • 它也不起作用。具有 9.81 值的工作区使用率也出现了。
  • 我已经把输出粘贴给你了
  • 我也没有从上述输入中看到任何 $_.Workspace Usage 值为 9.81
猜你喜欢
  • 1970-01-01
  • 2019-10-18
  • 2015-02-04
  • 2017-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-26
  • 1970-01-01
相关资源
最近更新 更多