【问题标题】:Local script output is saved on remote server if using invoke-command如果使用调用命令,本地脚本输出将保存在远程服务器上
【发布时间】: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


【解决方案1】:

在我看来,我永远不会在远程服务器上构建 HTML 报告。但是按照您的示例代码并给出您的“问题”(我想将 HTML 结果保存在我运行脚本的本地计算机中。)。这就是我要做的,我也对你的代码做了一些改进。

在远程服务器上调用命令时也不需要使用foreach 循环,除非有特殊需要。在您的示例中,我看不到一次对每台服务器进行任何特定或特定的需要。 Powershell 允许您在一组服务器上调用相同的命令,这与使用-AsJob 开关后跟Wait-Job 完成所有调用的情况非常相似。

$style = @"
<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;
    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>
"@

$servers = Get-Content -Path Server_list.text|?{$_}|%{$_.trim()}

$session=@()

$servers|%{
    if(Test-Connection $_ -Quiet -Count 1)
    {
        $session+=New-PSSession $_
    }
}

$sblock={

$header=$using: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>"
$processReport = ConvertTo-HTML -Body " $ComputerName $ProcessInfo" -Title "Informations sur les processus Genetec" -PostContent "<p>Date de creation : $(Get-Date)<p>"

#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>"
$firefoxReport = ConvertTo-HTML -Body " $ComputerName $ProcessInfo_firefox" -Title "Informations sur les processus Firefox" -PostContent "<p>Date de creation : $(Get-Date)<p>"

#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>"
$firefoxPlus200Report = 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>"

[PSCustomObject]@{
    processReport = $processReport
    firefoxReport = $firefoxReport
    firefoxPlus200Report = $firefoxPlus200Report
}

}

$results=invoke-command -Session $session -ScriptBlock $sblock

Remove-PSSession $session

#Here you have the report of your first server
$results[0].processReport
$results[0].firefoxReport
$results[0].firefoxPlus200Report

#The Results variable will contain the reports of all servers, you can use this variable to loop over each server

【讨论】:

  • 你好圣地亚哥,首先非常感谢你的帮助我真的很感激,只是我想了解负责在本地服务器中构建结果(HTML)的代码部分?
  • 此代码(您的代码)正在远程服务器中构建 HTML 报告。您通常希望从报告服务器中获取所有结果,并在本地构建 HTML。我个人不使用ConvertTo-HTML,我在这里使用字符串并在我需要的地方插入通常是HTML表格的对象。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-26
  • 2017-01-21
  • 1970-01-01
  • 2011-03-08
相关资源
最近更新 更多