如果使用 invoke-command,本地脚本输出将保存在远程服务器上

Local script output is saved on remote server if using invoke-command

大家好,感谢您在这里提供的帮助,

我有下面的脚本,它正在检查进程和内存列表,然后创建一个很好的 HTML 文件作为报告,一旦我添加了一个循环 + 调用,这个脚本在我的本地机器上运行良好-命令和 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 文件中而不是分开,非常感谢。

在我看来,我绝不会在远程服务器上构建 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