Works with a script that will find files and then email them to different users.
This works with the exception that when I want to use a csv file containing several servers, only one email from one server is sent to the others and I can not find where I am doing wrong.
Someone who could be kind and help me on the right path?
#Import data from file
$servers = Import-Csv -Path C:\Scripts\Program\Servers.csv -Delimiter ";" -Encoding UTF8
foreach ($server in $servers) {
Invoke-Command -ComputerName $server.Name {
Set-Location \\$env:COMPUTERNAME\d$\Integrations
$SMTPServer = 'smtp.mygroup.com'
$EmailFrom = "test@mygroup.com"
$files = Get-ChildItem -Path .\ -Recurse -Filter *.xml -Force
$ExpiredBody = @(
$files |
Where-Object {$_.Directory -like "*Error*"} |
Select-Object PsPath, LastWriteTime |
Sort-Object PsPath
)
$head = @'
<style>
TABLE{width: 100%;border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
TH{border-width: 1px;padding: 1px;border-style: solid;border-color: black;}
TD{border-width: 1px;padding: 1px;border-style: solid;border-color: black;}
</style>
'@
$ExtendExpiredBody = $ExpiredBody | ConvertTo-HTML -Head $head
$EmailExpiredBody = "Listed below are files that are in a state of Error $($env:COMPUTERNAME)"
if ($ExpiredBody) {
$EmailExpiredBody = "<br> Listed below are Error files that have been reported $($env:COMPUTERNAME)"
$EmailBody = $EmailExpiredBody + $ExtendExpiredBody
$EmailBody = $EmailBody + ($env:userdomain, $env:username, $env:COMPUTERNAME)
$EmailSubject = "Files that is reporting error in computer $($env:COMPUTERNAME)"
}
if ($ExpiredBody.Count -ge 1) {
$SendMailMessageProps = @{
From = $EmailFrom
To = $server.mejl
Subject = $EmailSubject
Body = $EmailBody
BodyAsHtml = $true
SmtpServer = $SMTPServer
}
Send-MailMessage @SendMailMessageProps
}
}
}
Hm I have tried that with sam result.
Still got mail from one server and if I move the two last brackets to an uper location then I got one mail but only one not from all the servers in the csv file.
Totaly stuck with this I have been reading this so much so I got blind
After a minor adjustment, it works almost as intended. The information is coming out as it should now. Just need to arrange so that the sender name is also correct with the server name.
Many thanks for the help.