【发布时间】:2021-06-18 10:11:17
【问题描述】:
大家好,感谢您在这里提供的帮助,
我有下面的脚本,他正在检查进程和内存列表,然后创建一个漂亮的 HTML 文件作为报告,这个脚本在我的本地机器上运行良好,一旦我添加了一个循环 + Invoke-Command 和一个 list_server。 txt 以获取列表中每个服务器的进程信息,我发现 html 文件保存在列表中每个服务器的 C:/$server?documents 中。我想将 HTML 结果保存在我运行脚本的本地机器中。
这是我的脚本:
$serversname = Get-Content -Path Server_list.text
Foreach ($servername in $serversname)
{
invoke-command -scriptblock {
$Header = @"
<style>
h1 {
font-family: Arial, Helvetica, sans-serif;
color: #e68a00;
font-size: 28px;
text-align:center;
margin: 0 auto;
}
h2 {
font-family: Arial, Helvetica, sans-serif;
color: #000099;
font-size: 16px;
text-align:center;
margin: 100 auto;
}
table {
width:50%;
margin-left:auto;
margin-right:auto;
}
table {
font-size: 12px;
border: 0px;
font-family: Arial, Helvetica, sans-serif;
}
td {
padding: 4px;
margin: 0px;
border: 0;
}
th {
background: #395870;
background: linear-gradient(#49708f, #293f50);
color: #fff;
font-size: 11px;
text-transform: uppercase;
padding: 10px 15px;
vertical-align: middle;
}
tbody tr:nth-child(even) {
background: #f0f0f2;
}
p {
font-family: Arial, Helvetica, sans-serif;
color: #ff3300;
font-size: 12px;
text-align:center;
margin: 0 auto;
}
</style>
"@
$properties=@(
@{Name="Process Name"; Expression = {$_.name}},
@{Name="Memory (MB)"; Expression = {[Math]::Round(($_.WorkingSet / 1mb ),2)}}
)
#Getting all process
$ComputerName = "<h1>Nom du serveur : $env:computername</h1>"
$ProcessInfo = Get-process | Select-Object $properties | Sort-Object -Property "Memory (MB)" -Descending | ConvertTo-Html -Head $Header -PreContent "<h2>Informations sur les processus</h2>"
$Report = ConvertTo-HTML -Body " $ComputerName $ProcessInfo" -Title "Informations sur les processus Genetec" -PostContent "<p>Date de creation : $(Get-Date)<p>"
$Report | Out-File .\log.html
#Getting only Firefox process
$ProcessInfo_firefox = Get-process firefox | Select-Object $properties | Sort-Object -Property "Memory (MB)" -Descending | ConvertTo-Html -Head $Header -PreContent "<h2>Informations sur les processus Firefox</h2>"
$Report = ConvertTo-HTML -Body " $ComputerName $ProcessInfo_firefox" -Title "Informations sur les processus Firefox" -PostContent "<p>Date de creation : $(Get-Date)<p>"
$Report | Out-File .\log_firefox.html
#Getting only Firefox process with more than 200Mb memory
$ProcessInfo_firefox_plus200 = Get-Process firefox | Select-Object @{Name="Process Name"; Expression = {$_.name}},@{Name="Memory (MB)"; Expression = {[Math]::Round(($_.WorkingSet / 1mb ),2)}} | where {$_.'Memory (MB)' -gt 300} | ConvertTo-Html -Head $Header -PreContent "<h2>Informations sur les processus Firefox qui utilise plus que 200mb de RAM</h2>"
$Report = ConvertTo-HTML -Body " $ComputerName $ProcessInfo_firefox_plus200" -Title "Informations sur les processus Firefox qui utilise plus que 200mb de RAM" -PostContent "<p>Date de creation : $(Get-Date)<p>"
$Report | Out-File .\log_firefox_plus200.html
} -computername $servername
}
另外,我还有第二个问题,正如您所见,我的脚本正在为每个服务器生成一个 HTML 文件,我能否以某种方式将所有进程表不单独放在一个 HTML 文件中,非常感谢。
【问题讨论】:
-
[1] 你的循环被危险地命名了。这 >>> Foreach ($servername in $serversname) I-C 调用中收集您的数据,将其发送回调用系统...然后在调用系统本地构建报告。
标签: powershell powershell-2.0 powershell-3.0 powershell-4.0 powershell-remoting