![](/img/trans.png)
[英]Running a script off local machine to remote using Invoke-Command
[英]Local script output is saved on remote server if using invoke-command
大家好,感謝您在這里提供的幫助,
我有下面的腳本,他正在檢查進程列表和 memory 然后創建一個不錯的 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 文件中,非常感謝。
在我看來,我永遠不會在遠程服務器上構建 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.