Hi, I’m working on a script to check the patch level of all the servers in our environment and notify the correct server application team via email, if they have a server that hasn’t been updated in the last 90 days (We have multiple app teams that are set as server owners).
It’s still a little rough but it seems to work. Right now I’m just exporting the results to a CSV and catching the errors in a hash table.
What I want to do is actually avoid exporting the results to any file and store them in a way I can format them in a table and send it. Unfortunately I’m a little bit stuck with what would be the best way to do this and looking for some advice from the forum.
In summary I want to achieve the following.
- Store the results of $obj $team $team_email $lastdate in a way that can be formatted nicely in a table of the body of an email sent with Send-MailMessage.
- Be able to sort this table by the email value, group and send the list of servers with the same email owner's individually formatted tables with only their servers in the body of the email.
[pre]
$token = xxxxxx
$headers = xxxxxx
$servers = Get-ADComputer -Filter {(Name -like “srv”) -and (Enabled -eq $true) -and (Operatingsystem -like “server”)}
$RPCerror = [ordered]@{}
Foreach ($server in $servers)
{
Get info from the IPAM portal
$obj = ($server.Name).ToLower()
$res = (Invoke-RestMethod -Uri “https://IPAMPORTAL.local” -Headers $headers).results
$team = $res.custom_fields.responsible_team
$team_email = $res.custom_fields.team_dist_list
$datesub90 = (Get-date).adddays(-90)
try{
$lastdate = ((Get-HotFix -ComputerName $obj | Sort-Object -Property InstalledOn)[-1] | Where-Object InstalledOn -LT $datesub90 | Select-Object -First 1).InstalledOn
if ($lastdate -notlike $null)
{
$response = $obj + ", " + $team + ", " + $team_email + “,” + $lastdate
Out-File -FilePath “.\result.csv” -InputObject $response -Append
Write-Output $response
}
}
catch {
$RPCerror.Add($obj,($Error[0].Exception.Message))
}
}
[/pre]