在 Powershell 中添加到 HTML 输出的链接

发布于 2024-12-06 03:46:47 字数 3066 浏览 0 评论 0原文

我编写了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北方。的韩爷 2024-12-13 03:46:47

我认为你可以用这个替换 AppPoolName (在 ConvertTo-HTML 中)...

@{Label="AppPoolName";Expression={ '<a href="http://powershell.com/requests.ps1x?PID=$($_.PID)&AppPoolName=$($_.AppPoolName)">$($_.AppPoolName)</a>' }}

这只是我的想法,未经测试...:-)

I think you could replace the AppPoolName (in ConvertTo-HTML) with this...

@{Label="AppPoolName";Expression={ '<a href="http://powershell.com/requests.ps1x?PID=$($_.PID)&AppPoolName=$($_.AppPoolName)">$($_.AppPoolName)</a>' }}

That's just off the top of me head and not tested... :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文