Mail from powershell

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
}
}
}

With

Set-Location \\$env:COMPUTERNAME\d$\Integrations

you set the current working directory to the drive D: on the local computer this script runs on. You may change this to

Set-Location \\$($server.Name)\d$\Integrations

A better option would be to provide the desired path to the according cmdlets like Get-ChildItem and not working with the working directory. :wink:

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 :joy:

Hmmm … if I got everything right this should be enough:

$serverList = Import-Csv -Path C:\Scripts\Program\Servers.csv -Delimiter ';' -Encoding UTF8
$SMTPServer = 'smtp.mygroup.com'
$EmailFrom  = 'test@mygroup.com'
$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>
'@

foreach ($server in $serverList) {
    $Result = 
    Invoke-Command -ComputerName $($server.Name) {
        Get-ChildItem -Path 'd:\Integrations' -Filter *.xml -Recurse -Force |
        Where-Object { $_.Directory -like "*Error*" } 
    }
    if ($Result) {
        $ExtendExpiredBody = @(
            $Result | 
                Select-Object -Property PSComputerName, LastWriteTime, Fullname | 
                    Sort-Object -Property FullName | 
                        ConvertTo-HTML -Head $head
        )
        $EmailExpiredBody = "<br> Listed below are Error files that have been reported <br>" 
        $EmailBody = $EmailExpiredBody + $ExtendExpiredBody

        $SendMailMessageProps = @{
            From       = $EmailFrom
            To         = $server.mejl
            Subject    = "Files that is reporting error in computer $($Result[0].PSComputerName)"
            Body       = $EmailBody
            BodyAsHtml = $true
            SmtpServer = $SMTPServer
        }
        Send-MailMessage @SendMailMessageProps
    }
}
1 Like

I wonder if this is the double-hop problem?

You can’t authenticate against the mail server, and the servers that aren’t working are not in the list of servers that are allowed to relay.

@Olaf’s solution to move the sending of the mail to the machine you’re running the script from is what I would do too.

This seems good.
I can see the gain in reorder sending mail. I will remote and try this right away and the I get back here.

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.