lundi 22 juin 2020

How to get the variable results with condition from invoke-command in Powershell?

I am trying to make a script in Powershell to get pending updates of the servers with the bootup time and put into a HTML report. Everything is working fine, except the results in the invoke-command. I tried several ways, but I can not find any other good option to show the updates for many servers, so I have to deal with the invoke-command. So if I call the results inside the invoke-command it puts extra blank cells to the report, if outside it looks good, but shows false results. Can anybody tell me what should be the solution? Or what i am missing from the code?

Thanks for everyone who reads and puts effort into my problem!

$array="list of the servers"

$total = $null
$html = @'
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
<title> Server Updates Report</title>
<STYLE TYPE="text/css">
<!--
td {
font-family: Tahoma;
font-size: 13px;
border-top: 1px solid #999999;
border-right: 1px solid #999999;
border-bottom: 1px solid #999999;
border-left: 1px solid #999999;
padding-top: 0px;
padding-right: 0px;
padding-bottom: 0px;
padding-left: 0px;
}
body {
margin-left: 5px;
margin-top: 5px;
margin-right: 0px;
margin-bottom: 10px;

table {
border: thin solid #000000;
}
-->
</style>
</head>
<body>
<table width='100%'>
<tr bgcolor='#CCCCCC'>
<td colspan='7' height='25' align='center'><strong><font color="#003399" size="4"  face="tahoma">Server Patch Report </font></strong></td>
</tr>
</table>
<table width='100%'><tbody>
<tr bgcolor=#CCCCCC>
<td width='20%' height='15' align='center'> <strong> <font color="#003399" size="2" face="tahoma" >Server Name</font></strong></td>
<td width='40%' height='15' align='center'> <strong> <font color="#003399" size="2" face="tahoma" >Patching Information</font></strong></td>
<td width='10%' height='15' align='center'> <strong> <font color="#003399" size="2" face="tahoma" >Number of pending updates</font></strong></td>
<td width='30%' height='15' align='center'> <strong> <font color="#003399" size="2" face="tahoma" >Last Bootup Time</font></strong></td>
</tr></tr>
</table>
<table width='100%'><tbody>
'@


foreach($servername in $array){


$updatetest=Invoke-Command -ComputerName $servername -ScriptBlock {

$Searcher = New-Object -ComObject Microsoft.Update.Searcher
$results = $searcher.search("Type='software' AND IsInstalled = 0")
$results.Updates | 
     ForEach-Object { $update+= ($_.Title)+"`r`n"  #, $_.Identity.UpdateID )
                      #$updatetest
                      }}
                     
      if ($updatetest.count -gt 0){$updateout="Update is available!" ; $updatecount=$updatetest.count}
      else{ $updateout="Not found update"; $updatecount="0"}
  

$LastBootUpTime=$null
$OS = Get-WmiObject Win32_OperatingSystem -ComputerName $servername
$LastBootUpTime = [System.Management.ManagementDateTimeConverter]::ToDateTime("$($OS.LastBootUpTime)")


$current = " <tr>
<td width='20%' align='center'>$servername</td>
<td width='40%' align='center'>$updateout</td>
<td width='10%' align='center'>$updatecount</td>
<td width='30%' align='center'>$LastBootUptime</td>
</tr> "
$total += $current
}


$HTMLEnd = @"
</div>
</body>
</html>
"@

$MainHtml= $html + $total + $HTMLEnd

$MainHtml | Out-File "\\c.\...\WSUSReport.html"

Aucun commentaire:

Enregistrer un commentaire