在 Powershell 中添加到 HTML 输出的链接
我编写了一个 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>
I have wrote a PowerShell script that catches all of the running IIS worker processes on a remote server and returns back diagnostic information (appppolname, cpu, virtual memory etc). It is then converted to HTML and displayed to the user.
$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
}
I am looking for a way to format the HTML-output to convert the AppPoolName output of the table into a link where it would use the AppPoolName and PID within the link itself (ex. http://powershell.com/requests.ps1x?PID=$PID&AppPoolName=$AppPoolName
If any one has any thoughts or ideas that could solve and point me in the right direction, it would be much appreciated.
Thanks!
-----------Update-------------------
Here is the example output:
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你可以用这个替换 AppPoolName (在 ConvertTo-HTML 中)...
这只是我的想法,未经测试...:-)
I think you could replace the AppPoolName (in ConvertTo-HTML) with this...
That's just off the top of me head and not tested... :-)