Написание сценария состояния сервера - PullRequest
0 голосов
/ 30 января 2020

Итак, я пишу PowerShell на html плату состояния сети, но у нее есть проблема, когда, если что-то не работает, таблица мигает и не загружает остальную часть таблицы. Я попытался изменить html refre sh с meta http-экв = 'refre sh' content = '5' на метатег при загрузке, но тогда он вообще не работал. Я все еще хак в PowerShell, но мне интересно, есть ли способ сделать это лучше. Заранее спасибо за любую помощь.

start .\updown.html
#timer loop
$timeout = new-timespan -Hours 12
$sw = [diagnostics.stopwatch]::StartNew()
while ($sw.elapsed -lt $timeout){
#timer loop
$WorkStations = Get-Content ".\data_files\WorkStations.txt"
$Network = Get-Content ".\data_files\Network.txt"
$Servers = Get-Content ".\data_files\Servers.txt"
$ESXi = Get-Content ".\data_files\ESXi.txt"
$AppServers = Get-Content ".\data_files\AppServers.txt"
$bla = @{}
$outFile = "updown.html"
if (Test-Path $outFile){del $outFile}
$header = "
<HTML>
<HEAD>
<meta http-equiv='refresh' content='5'>
</HEAD>
"
$style = "
<style>
* {
  box-sizing: border-box;
}
body {
  background-color: black;
}
table {
  color : white;
  border-collapse: collapse;
  width: 33%;
}
td, tr {
  color : white;
  border: 1px solid #dddddd;
  text-align: left;
  padding: 8px;
}
th {
  background-color: #696969;
  color : white;
  border: 1px solid #dddddd;
  text-align: center;
  padding: 8px;
}
/* Style the header */
.header {
  background-color: #f1f1f1;
  padding: 30px;
  text-align: center;
  font-size: 35px;
}
/* Create two equal columns that floats next to each other */
.column {
  float: left;
  padding: 10px;
}
/* Left and right column */
.column.side {
  width: 50%;
}
/* Clear floats after the columns */
.row:after {
  content: '';
  display: table;
  clear: both;
}
/* Responsive layout - makes the three columns stack on top of each other instead of next to each other */
@media (max-width: 600px) {
  .column.side {
    width: 100%;
  }
}
</style>   
"
$bodyOpen = "
<BODY>
<div class='header'> 
 DATA MANAGMENT SYSTEM
</div>
<div class='row'>
  <div class='column side'>
<table>
"
$bodyClose = "
</table>
</div>
<div class='column side' style='color:yellow;'>
  <h2><p id='demo'></p></h2>
<script>
var d = new Date();
document.getElementById('demo').innerHTML = d;
</script>
<img src='.\data_files\logo.jpg'>
  </div>
</div>
</BODY>
</HTML>
"
#generate HTML 
Write-Output $header $style $bodyOpen | Out-File -Append $outFile
# Workstation loop 
Write-output "<table style='float: left; width: 33.33%; padding: 50px;'><th>WorkStations</th>" | Out-File -Append $outFile
$WorkStations | ForEach-Object {

  $ip = "$_"

  if (Test-Connection -Count 1 -Quiet -ComputerName $ip) {
    $bla[$_] =  "UP"
    Write-output  "<tr><td><h2 style='color : green; textweight : bold;'> $ip UP </h1></tr></td>" | out-file -Append $outFile
  }
  else {
    $bla[$_] = "DOWN"
    Write-output "<tr><td><h2 style='color : red; textweight : bold;'> $ip DOWN </h1></tr></td>" | out-file -Append $outFile
  } 
}
#end loop
# Network loop 
Write-output "<th>Network</th>" | Out-File -Append $outFile
$Network | ForEach-Object {
  $ip = "$_"

  if (Test-Connection -Count 1 -Quiet -ComputerName $ip) {
    $bla[$_] =  "UP"
    Write-output  "<tr><td><h2 style='color : green; textweight : bold;'> $ip UP </h1></tr></td>" | out-file -Append $outFile
  }
  else {
    $bla[$_] = "DOWN"
    Write-output "<tr><td><h2 style='color : red; textweight : bold;'> $ip DOWN </h1></tr></td>" | out-file -Append $outFile
  } 
}
#end loop
# Servers loop 
Write-output "</table><table style='float: left; width: 33.33%; padding: 50px;'><th>Servers</th>" | Out-File -Append $outFile
$Servers | ForEach-Object {
  $ip = "$_"

  if (Test-Connection -Count 1 -Quiet -ComputerName $ip) {
    $bla[$_] =  "UP"
    Write-output  "<tr><td><h2 style='color : green; textweight : bold;'> $ip UP </h1></tr></td>" | out-file -Append $outFile
  }
  else {
    $bla[$_] = "DOWN"
    Write-output "<tr><td><h2 style='color : red; textweight : bold;'> $ip DOWN </h1></tr></td>" | out-file -Append $outFile
  } 
}
#end loop
# ESXi loop 
Write-output "<th>ESXi</th>" | Out-File -Append $outFile
$ESXi | ForEach-Object {
  $ip = "$_"

  if (Test-Connection -Count 1 -Quiet -ComputerName $ip) {
    $bla[$_] =  "UP"
    Write-output  "<tr><td><h2 style='color : green; textweight : bold;'> $ip UP </h1></tr></td>" | out-file -Append $outFile
  }
  else {
    $bla[$_] = "DOWN"
    Write-output "<tr><td><h2 style='color : red; textweight : bold;'> $ip DOWN </h1></tr></td>" | out-file -Append $outFile
  } 
}
#end loop
# AppServers loop 
Write-output "</table><table style='float: left; width: 33.33%; padding: 50px;'><th>AppServers</th>" | Out-File -Append $outFile
$AppServers | ForEach-Object {
  $ip = "$_"

  if (Test-Connection -Count 1 -Quiet -ComputerName $ip) {
    $bla[$_] =  "UP"
    Write-output  "<tr><td><h2 style='color : green; textweight : bold;'> $ip UP </h1></tr></td>" | out-file -Append $outFile
  }
  else {
    $bla[$_] = "DOWN"
    Write-output "<tr><td><h2 style='color : red; textweight : bold;'> $ip DOWN </h1></tr></td>" | out-file -Append $outFile
  } 
}
#end loop
#close HTML
Write-Output $bodyClose | Out-File -Append $outFile

#timer loop
start-sleep -seconds 10
}

write-host "stop"

1 Ответ

0 голосов
/ 03 февраля 2020

Хорошо, разобрался, вместо того, чтобы писать, просто переписать.

$WorkStationsKP = Get-Content ".\data_files\WorkStationsKP.txt"
$Network = Get-Content ".\data_files\Network.txt"
$Servers = Get-Content ".\data_files\Servers.txt"
$ESXi = Get-Content ".\data_files\ESXi.txt"
$AppServers = Get-Content ".\data_files\AppServers.txt"
$bla = @{}
$outFile = "updown.html"
$update = $false
$updateTimer = 0


start .\updown.html

$timeout = new-timespan -Hours 12
$sw = [diagnostics.stopwatch]::StartNew()
#timer loop
while ($sw.elapsed -lt $timeout){
$updateTimer
if ($updateTimer -eq 5) {$update = $true}


# if (Test-Path $outFile){
#    Write-Output $holderHTML | Out-File $outFile
# }


$header = "
<html>
<head>
<meta http-equiv='refresh' content='5' >
<p>Updating every 5</p>
</head>
"

$style = "
<style>
* {
  box-sizing: border-box;
}
body {
  background-color: black;
}
table {
  color : white;
  border-collapse: collapse;
  margin-left: auto;
  margin-right: auto;
  text-align:center;
}
td, tr {
  color : white;
  border: 1px solid #dddddd;
  text-align: left;
  padding: 8px;
}

th {
  background-color: #696969;
  color : white;
  border: 1px solid #dddddd;
  text-align: center;
  padding: 8px;
}
/* Style the header */
.header {
  background-color: #f1f1f1;
  padding: 3px;
  text-align: center;
  font-size: font-size:10vw;;
}
/* Create two equal columns that floats next to each other */
.column {
  float: left;
  padding: 10px;
}
/* Left and right column */
.column.side {
  float: left; padding: 50p;
}
/* Clear floats after the columns */
.row:after {
  content: '';
  display: table;
  clear: both;
}
img {
Width: auto;
height: auto;
}
/* Responsive layout - makes the three columns stack on top of each other instead of next to each other */
@media (max-width: 600px) {
  .column.side {
    width: 90%;
  }
}
</style>   
"
$bodyOpen = "
<BODY>
<div class='header'> 
<img src='.\data_files\logo1.jpg' style='float: left'>
 SYSTEM
<img src='.\data_files\logo2.jpg' style='float: right'>
</div>
<h2><p id='demo' style='color:yellow;'></p></h2>
<script>

var d = new Date();
document.getElementById('demo').innerHTML = d;

</script>

<div class='row'>
  <div class='column side'>
    <table>
"

$bodyClose = "
</table>
</div>
  <h2><p id='demo' style='color:yellow;'></p></h2>
</div>
</BODY>
</HTML>
"

#generate HTML 
$holderHTML = ""
#Site up loops
  if (Test-Connection -Count 1 -Quiet -TimeToLive 4 -ComputerName 127.0.0.0 -ErrorAction Continue) { 
    $holderHTML += Write-Output  "<nobr><h2 style='color : #00ffff; textweight : bold;'> RT UP </h1></nobr>"
  }
  else {    
    $holderHTML += Write-Output "<nobr><h2 style='color : #ff81dc; textweight : bold;'> RT DOWN </h1></nobr>"
  }
  if (Test-Connection -Count 1 -Quiet -TimeToLive 4 -ComputerName 127.0.0.0 -ErrorAction Continue) { 
    $holderHTML += Write-Output  "<nobr><h2 style='color : #00ffff; textweight : bold;'> RT UP </h1></nobr>"
  }
  else {    
    $holderHTML += Write-Output "<nobr><h2 style='color : #ff81dc; textweight : bold;;'>RT DOWN </h1></nobr>"
  }
    if (Test-Connection -Count 1 -Quiet -TimeToLive 4 -ComputerName 127.0.0.0 -ErrorAction Continue) { 
    $holderHTML += Write-Output  "<nobr><h2 style='color : #00ffff; textweight : bold;'> RT UP </h1></nobr>"
  }
  else {    
    $holderHTML += Write-Output "<nobr><h2 style='color : #ff81dc; textweight : bold;'> RT DOWN </h1></nobr>"
  }
#end loop
$holderHTML += Write-Output $header $style $bodyOpen

<# Ranging example loop 

  if(Test-Path .\Ranging.txt) {$holderHTML += Write-Output "<h2 style='color : orange;'>They </h2>`n"}
  else {$holderHTML += Write-Output "<h2 style='color : yellow;'>Does not appear to be Ranging call to confirm</h2>`n" }

end loop
#>

# Workstation loop 
$holderHTML += Write-Output "<table style='float: left; width: 20%; padding: 50px; white-space:nowrap;'><th>WorkStations </th>`n"
$WorkStationsKP | ForEach-Object {
  $ip = "$_"
 if($ip-like $Null){
    $holderHTML += Write-Output "<tr><td style='color : blue;'>Error blank data in txt</tr></td>`n"
}
else {
  if (Test-Connection -Count 1 -Quiet -TimeToLive 4 -ComputerName $ip -ErrorAction Continue) {

    $holderHTML += Write-Output  "<tr><td><h2 style='color : green; textweight : bold;'> $ip UP </h1></tr></td>`n"
  }
  else {

    $holderHTML += Write-Output "<tr><td><h2 style='color : red; textweight : bold;'> $ip DOWN </h1></tr></td>`n"
  } }
}
#end loop
# Servers loop 
$holderHTML += Write-Output "</table><table style='float: left; width: 20%; padding: 50px;'><th>Servers</th>`n"
$Servers | ForEach-Object {
  $ip = "$_"
 if($ip-like $Null){
    $holderHTML += Write-Output "<tr><td style='color : blue; white-space:nowrap;'>Error blank data in txt</tr></td>`n"
}
else {
  if (Test-Connection -Count 1 -Quiet -TimeToLive 4 -ComputerName $ip -ErrorAction Continue) {

    $holderHTML += Write-Output  "<tr><td><h2 style='color : green; textweight : bold; white-space:nowrap;'> $ip UP </h1></tr></td>`n"
  }
  else {

    $holderHTML += Write-Output "<tr><td><h2 style='color : red; textweight : bold; white-space:nowrap;'> $ip DOWN </h1></tr></td>`n"
  } }
}
#end loop
# ESXi loop 
$holderHTML += Write-Output "<th>ESXi</th>`n"
$ESXi | ForEach-Object {
  $ip = "$_"
 if($ip-like $Null){
    $holderHTML += Write-Output "<tr><td style='color : blue; white-space:nowrap;'>Error blank data in txt</tr></td>`n"
}
else {
  if (Test-Connection -Count 1 -Quiet -TimeToLive 4 -ComputerName $ip -ErrorAction Continue) {

    $holderHTML += Write-Output  "<tr><td><h2 style='color : green; textweight : bold; white-space:nowrap;'> $ip UP </h1></tr></td>`n"
  }
  else {

    $holderHTML += Write-Output "<tr><td><h2 style='color : red; textweight : bold; white-space:nowrap;'> $ip DOWN </h1></tr></td>`n"
  } }
}
#end loop
# AppServers loop 
$holderHTML += Write-Output "</table><table style='float: left; width: 20%; padding: 5px;'><th>AppServers</th>`n"
$AppServers | ForEach-Object {
  $ip = "$_"
if($ip-like $Null){
    $holderHTML += Write-Output "<tr><td style='color : blue; white-space:nowrap;'>Error blank data in txt</tr></td>`n"
}
else {
  if  (Test-Connection -Count 1 -Quiet -TimeToLive 4 -ComputerName $ip -ErrorAction Continue) {

    $holderHTML += Write-Output  "<tr><td><h2 style='color : green; textweight : bold; white-space:nowrap;'> $ip UP </h2></tr></td>`n"
  }
  else {

    $holderHTML += Write-Output "<tr><td><h2 style='color : red; textweight : bold; white-space:nowrap;'> $ip DOWN </h2></tr></td>`n"
  } 
    }
}

#end loop
#close HTML
Write-Output $holderHTML $bodyClose | Out-File $outFile



$updateTimer ++
if ($updateTimer -gt 5){$updateTimer = 0}
#end timer loop
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...