我编写了一个PowerShell脚本,它捕获远程服务器上所有正在运行的IIS工作进程,并返回诊断信息(appppolname,cpu,虚拟内存等)。然后将其转换为HTML并显示给用户。
$server = Read-Host Enter server name...
if (!(test-connection -computername $server -quiet))
{
Write-Host $server is not pingable, does not exist, or timed out. Please try a different server.
}
else
{
$command =
{add-pssnapin WebAdministration
function get-iiswp([string]$name="*")
{
$list = get-process w3wp -ea SilentlyContinue
if ($error)
{
Write-Host There are no IIS worker processes currently running. Error Message: $error
}
else
{
foreach($p in $list)
{
$filter = "Handle='" + $p.Id + "'"
$wmip = get-WmiObject Win32_Process -filter $filter
if($wmip.CommandLine -match "-ap `"(.+)`"")
{
$appName = $matches[1]
$p | add-member NoteProperty AppPoolName $appName
}
}
$list | where { $_.AppPoolName -like $name }
}
}
get-iiswp | select-object -property *
}
invoke-command -computername $server -scriptblock $command | ConvertTo-HTML ID, AppPoolName, @{Label="CPU(s)";Expression={if ($_.CPU -ne $()) {$_.CPU.ToString("N")}}}, @{Label="Virtual Memory";Expression={[int]($_.VM/1MB)}}, @{Label="Physical Memory";Expression={[int]($_.WS/1024)}} | Out-file D:/test.html
}
我正在寻找一种格式化HTML输出的方法,将表的AppPoolName输出转换为链接,在链接中它将使用链接本身内的AppPoolName和PID(例如http://powershell.com/requests.ps1x?PID= $ PID& AppPoolName = $ AppPoolName
如果任何人有任何想法或想法可以解决并指出我正确的方向,那将非常感激。
谢谢!
-----------更新-------------------
以下是示例输出:
<tr><tr><tr>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>HTML TABLE</title>
</head><body>
<h2>IIS Worker Process Diagnostic</h2>
<table>
<colgroup>
<col/>
<col/>
<col/>
<col/>
<col/>
</colgroup>
<tr><th>ID</th><th>AppPoolName</th><th>CPU(s)</th><th>Virtual Memory</th><th>Physical Memory</th></tr>
<tr><td>3820</td><td>AppPool1</td><td>4.92</td><td>355</td><td>74200</td></tr>
<tr><td>4108</td><td>AppPool2</td><td>0.23</td><td>106</td><td>21380</td></tr>
<tr><td>4940</td><td>AppPool3</td><td>0.20</td><td>590</td><td>39444</td></tr>
<tr><td>7244</td><td>AppPool4</td><td>0.33</td><td>596</td><td>42556</td></tr>
</table>
</body></html>
答案 0 :(得分:1)
我认为你可以用这个替换AppPoolName(在ConvertTo-HTML中)......
@{Label="AppPoolName";Expression={ '<a href="http://powershell.com/requests.ps1x?PID=$($_.PID)&AppPoolName=$($_.AppPoolName)">$($_.AppPoolName)</a>' }}
这只是我的头顶而没有经过测试......: - )