Mail alert for file copying failed

Hello Experts,

I am new to PowerShell, i need your support.

we have a requirement like file copying one server to another server. i have implemeted without mail alert. when files is not aexist, and password expiration… those cases… i tried for sending mail alert … but its failing…

can you suggest…

Requirement : 1 . validating network credentials
2. checking file exist or not ?
3. copying file from A server to B
4. copying server from B server to C
i tried but, if and else conditions are executing…

can you suggest how to implement exception and mail alert …

net use \abc\ /user:"$user" “$pass”

$dir=“A:\Backup”

if(Test-Path -Path $dir)
{

wrrite-host “”
}

Get-ChildItem $dir -File | Copy-Item -destination B:\Backup -Recurse -ErrorVariable err
elseif($err)
{
send-mailmessage -From $fromaddress -To $toaddress -Subject $sSubject -SMTPServer $smtpserver -Body $sBody err

}
elseif
{

send-mailmessage -From $fromaddress -To $toaddress -Subject $fSubject -SMTPServer $smtpserver -Body $fBody

}

net use \abc\ /user:"$user" “$pass”

$sdir=“B:\Backup”

Get-ChildItem $sdir -File | Copy-Item -destination C:\Backup -Recurse -ErrorVariable err
elseif($err)
{
send-mailmessage -From $fromaddress -To $toaddress -Subject $sSubject -SMTPServer $smtpserver -Body $sBody err

}
elseif
{

send-mailmessage -From $fromaddress -To $toaddress -Subject $fSubject -SMTPServer $smtpserver -Body $fBody

}

can you help on this…

Hello bsr,

Could you provide the error message ?
you said its failed, but what failed?
copy item filed or send mail failed?

RRRRR,
Welcome to the forum. :wave:t4:

You picked a slightly complex task and since your new to PowerShell I’d recommend to start with one single smaller part of it. :wink:

It is a really bad idea to have a password in a script. A better option would be to run the script with the credentials of the account you need to access the recoursses you’re after. That would even make your script much easier to write. You may start with hte check for availability of the source.

$SourcePath = 'A:\Backup'
if (Test-Path -Path $SourcePath) {
    Send-MailMessage -From $fromaddress -To $toaddress -Subject "'$($SourcePath)' available" -SmtpServer $smtpserver -Body "The source path '$($SourcePath)' is available"
}
else {
    Send-MailMessage -From $fromaddress -To $toaddress -Subject "'$($SourcePath)' not available" -SmtpServer $smtpserver -Body "The source path '$($SourcePath)' is not available"
}

If that works as desired you start to add functionality.

To make your code easier to read - especially for cmdlet with many parameters you should read about splatting:

And last but not least:
When you post code or error messages please format it as code using the preformatted text button ( </> ). Simply place the cursor on an empty line, click the button and paste your code.

Thanks in advance.

Hi chen,

Thank you for your reply.

I am getting failed and success mail at a time …

if possible, can you suggest how to write code, if test path false, mail need to trigger.
like one by one… all scenarios. finally we need a alert for every block… if failed. final one success.

Thanks,

Hi Olaf,
Thanks for your reply, sure i will follow your suggestions.

yes, if test path is true, how to execute copy-item block immediately.

Thanks,
Bsr

Did you try the code? Did you receive a mail that the source path is available? If not - try it.

You place the code for the copy job right inside the same script block where you have the code for sending the success mail.

I urgently recommend NOT to do second step before the first one. Take your time to play with the code.