'Only One result per server
Hi I have been struggling to solve it.
No matter how many true results exist, I get only 1 folder name and 1 server on the email report. I reckon I missed something with Foreach. manually verified each server have at least 5,6 folder where last write time or creation is less then 10 hours, but email report would only show 1 server and 1 folder
$emailto =''
$emailFrom =""
$smtpserver =''
$Thistime = [datetime]::Today.AddHours(-10)
$ServerList = 'Server1','Server2'
$Result = @()
Foreach($ServerName in $ServerList)
{
$check = Get-ChildItem \\$ServerName\c$\inetpub\logs\LogFiles -Recurse -Filter *.log
if(-not($check.CreationTime.Date -gt $Thistime))
{
$status ='LogsMissing'
}
Else{}
$Result+= New-Object PSObject -Property @{
ServerName = $ServerName
Folder = $($check.fullname).split('\')[-2]
Status = 'LogsMissing'
}
}
if($Result -ne $null)
{
$HTML = '<style type="text/css">
#Header{font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;width:100%;border-collapse:collapse;}
#Header td, #Header th {font-size:14px;border:1px solid #FFF;padding:3px 7px 2px 7px;text-align:center;}
#Header th {font-size:14px;text-align:center;padding-top:5px;padding-bottom:4px;background-color:#A7C942;color:#fff;}
#Header tr.alt td {color:#000;background-color:#EAF2D3;}
</Style>'
$HTML += "<HTML><BODY><Table border=1 cellpadding=0 cellspacing=0 id=Header>
<TR>
<TH><B>ServerName</B></TH>
<TH><B>Folder</B></TH>
<TH><B>Status</B></TH>
</TR>"
Foreach($Entry in $Result)
{
$HTML += "<TR bgColor=Red>"
$HTML += "
<TD>$($Entry.ServerName)</TD>
<TD>$($Entry.Folder)</TD>
<TD>$($Entry.Status)</TD>
</TR>"
}
$HTML += "</Table></BODY></HTML>"
}
$MailMessage = @{
To = $emailto
From = $emailFrom
Subject = "IIS logs Creation Status"
Body = $HTML|Out-String
Smtpserver = $smtpserver
}
Send-MailMessage @MailMessage -BodyAsHtml
Solution 1:[1]
Note exactly sure what your desired results here is or what the source data looks like, but it sounds like you want to enumerate (1) each server and (2) a list of folders. Your main logic for enumeration a.t.m is the ForEach loop that you process on the list of servers. Thus your ForEach loop will only process twice (server1 and then server2).
If you want to process per server, per folder you will require two ForEach Loops.
Example:
- Get list of servers and perform a ForEach
- Within each server ForEach, get a list of folders and do another ForEach
I hope I understood correctly. If not, give some more context of what you are trying to achieve and maybe a layout of the server/folders.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Almero |
