首页 > 解决方案 > 使用 PowerShell 识别以 RD* 开头的实例名称

问题描述

我正在尝试查找所有超出我的阈值限制的 Azure 实例。我的系统检测到了这些实例,现在我想从文本中提取所有机器并采取行动。我只有 PowerShell 作为选项来识别以下文本中以 RD* 开头的所有实例。

@{DescriptionEntryId=343578460; 
 Issue:
 </td><td>
 An xxxxxx has been triggered for 59 distinct instances of this xxxxxxx
 </td></tr><tr><td>Description:</td><td>setting up this to identify issue</td></tr><tr><td>Severity:</td><td>Warning</td></tr><tr><td colspan="2"><dl><dt>RDxxxxxxxxx</dt><dd>The threshold (2) was met for 15 of 15 minutes. The data points during the evaluation window ranged between 2.393 and 6.054.</dd><dt>RDxxxxxxxxx</dt><dd>The threshold (2) was met for 15 of 15 minutes. The data points during the evaluation window ranged between 2.722 and 6.813.</dd><dt>RDxxxxxxxxxxxx</dt><dd>The threshold (2) was met for 15 of 15 minutes. The data points during the evaluation window ranged between 2.481 and 5.909.</dd><dt>RDxxxxxxxxxx</dt><dd>The threshold (2) was met for 14 of 15 minutes. The data points during the evaluation window ranged between 1.412 and 6.588.</dd><dt>RDxxxxxxxxxxxx</dt><dd>The threshold (2) was met for 15 of 15 minutes. The data points during the evaluation window ranged between 3.375 and 6.24.</dd><dt>RDxxxxxxxxxx</dt><dd>The threshold (2) was met for 15 of 15 minutes. The data points during the evaluation window ranged between 2.382 and 6.863.</dd><dt>RDxxxxxxxxxxxx</dt><dd>The threshold (2) was met for 14 of 15 minutes. The data points during the evaluation window ranged between 0.418 and 11.</dd><dt>RDxxxxxxxxxxxx</dt><dd>The threshold (2) was met for 15 of 15 minutes. The data points during the evaluation window ranged between 3.059 and 6.667.</dd><dt>RDxxxxxxxxxxxx</dt><dd>The threshold (2) was met for 15 of 15 minutes. The data points during the evaluation window ranged between 2.255 and 7.5.</dd><dt>RDxxxxxxxxxxxx</dt><dd>The threshold (2) was met for 15 of 15 minutes. The data points during the evaluation window ranged between 2.545 and 5.291.</dd><dt>RDxxxxxxxxxx</dt><dd>The threshold (2) was met for 14 of 15 minutes. The data points during the evaluation window ranged between 1.691 and 5.6.</dd><dt>RDxxxxxxxxxxxx</dt><dd>The threshold (2) was met for 14 of 15 minutes. The data points during the evaluation window ranged between 2 and 6.755.</dd><dt>RDxxxxxxxxxxx</dt><dd>The threshold (2) was met for 15 of 15 minutes</div><div>
 View additional information about why the xxxx fired
 </div></td></tr><tr><td>&nbsp;</td></tr><tr><td><div><a 
 </div></td></tr><tr><td>&nbsp;</td></tr><tr><td><div><a href="xxxxxxxxxxxxxxx" target="_blank">Suppress (Snooze) </a></div><div>
 </div></td></tr><tr><td>&nbsp;</td></tr><tr><td><div><a 
 </div></td></tr><tr><td>&nbsp;</td></tr></tbody></table></div></div></span>; RenderType=Html; Initials=; SubmittedByDisplayName=}

标签: powershellazure-powershell

解决方案


您似乎正在显示某种 HTML 报告,并希望使用 Powershell 对其进行解析。对于此解决方案,您需要将报告保存在 .html 文件中。

假设您这样做并将报告保存到磁盘,D:\Report.html 然后您可以执行以下操作:

# read the report file as string
$htmlReport = Get-Content -Path 'D:\Report.html' -Raw

# create a Regular Expression object to capture the RD machines and their issues
$regex = [regex] '<dt>(?<Name>RD[^<]*)</dt><dd>(?<Issue>[^<]+)</dd>'
$matches = $regex.Match($htmlReport)
while ($matches.Success) {
    New-Object -TypeName PSObject -Property ([ordered]@{ Name = $matches.Groups['Name'].Value; Issue = $matches.Groups['Issue'].Value })
    $matches = $matches.NextMatch()
} 

使用您提供的报告,这将产生

Name           Issue                                                                                                                       
----           -----                                                                                                                       
RDxxxxxxxxx    The threshold (2) was met for 15 of 15 minutes. The data points during the evaluation window ranged between 2.393 and 6.054.
RDxxxxxxxxx    The threshold (2) was met for 15 of 15 minutes. The data points during the evaluation window ranged between 2.722 and 6.813.
RDxxxxxxxxxxxx The threshold (2) was met for 15 of 15 minutes. The data points during the evaluation window ranged between 2.481 and 5.909.
RDxxxxxxxxxx   The threshold (2) was met for 14 of 15 minutes. The data points during the evaluation window ranged between 1.412 and 6.588.
RDxxxxxxxxxxxx The threshold (2) was met for 15 of 15 minutes. The data points during the evaluation window ranged between 3.375 and 6.24. 
RDxxxxxxxxxx   The threshold (2) was met for 15 of 15 minutes. The data points during the evaluation window ranged between 2.382 and 6.863.
RDxxxxxxxxxxxx The threshold (2) was met for 14 of 15 minutes. The data points during the evaluation window ranged between 0.418 and 11.   
RDxxxxxxxxxxxx The threshold (2) was met for 15 of 15 minutes. The data points during the evaluation window ranged between 3.059 and 6.667.
RDxxxxxxxxxxxx The threshold (2) was met for 15 of 15 minutes. The data points during the evaluation window ranged between 2.255 and 7.5.  
RDxxxxxxxxxxxx The threshold (2) was met for 15 of 15 minutes. The data points during the evaluation window ranged between 2.545 and 5.291.
RDxxxxxxxxxx   The threshold (2) was met for 14 of 15 minutes. The data points during the evaluation window ranged between 1.691 and 5.6.  
RDxxxxxxxxxxxx The threshold (2) was met for 14 of 15 minutes. The data points during the evaluation window ranged between 2 and 6.755. 

推荐阅读