# Mail alert for file copying failed

**URL:** <https://forums.powershell.org/t/mail-alert-for-file-copying-failed/17506>\
**Category:** PowerShell Help\
**Created:** [September 9, 2021, 5:19am UTC](https://forums.powershell.org/t/mail-alert-for-file-copying-failed/17506 "2021-09-09T05:19:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bsr](https://avatars.discourse-cdn.com/v4/letter/b/7ba0ec/32.png) [@bsr](https://forums.powershell.org/u/bsr)\
**Post date:** [September 9, 2021, 5:19am UTC](https://forums.powershell.org/t/mail-alert-for-file-copying-failed/17506/1 "2021-09-09T05:19:51Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![Chen.Chen](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/chen.chen/32/1689_2.png) [@Chen.Chen](https://forums.powershell.org/u/Chen.Chen)\
**Post date:** [September 9, 2021, 6:36am UTC](https://forums.powershell.org/t/mail-alert-for-file-copying-failed/17506/2 "2021-09-09T06:36:45Z")

</div>

Hello bsr,

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

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [September 9, 2021, 6:44am UTC](https://forums.powershell.org/t/mail-alert-for-file-copying-failed/17506/3 "2021-09-09T06:44:30Z")

</div>

RRRRR,  
Welcome to the forum. 👋🏽

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

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.

```auto
$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:

> **[about Splatting - PowerShell](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_splatting?view=powershell-7.2&viewFallbackFrom=powershell-7.1)**
>
> Describes how to use splatting to pass parameters to commands in PowerShell.

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.

---

<div class="post-metadata">

**Author:** ![bsr](https://avatars.discourse-cdn.com/v4/letter/b/7ba0ec/32.png) [@bsr](https://forums.powershell.org/u/bsr)\
**Post date:** [September 9, 2021, 6:46am UTC](https://forums.powershell.org/t/mail-alert-for-file-copying-failed/17506/4 "2021-09-09T06:46:40Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![bsr](https://avatars.discourse-cdn.com/v4/letter/b/7ba0ec/32.png) [@bsr](https://forums.powershell.org/u/bsr)\
**Post date:** [September 9, 2021, 6:53am UTC](https://forums.powershell.org/t/mail-alert-for-file-copying-failed/17506/5 "2021-09-09T06:53:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [September 9, 2021, 6:58am UTC](https://forums.powershell.org/t/mail-alert-for-file-copying-failed/17506/6 "2021-09-09T06:58:17Z")

</div>

> [@bsr](#):
>
> yes, if test path is true,

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

> [@bsr](#):
>
> how to execute copy-item block immediately.

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.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:21pm UTC](https://forums.powershell.org/t/mail-alert-for-file-copying-failed/17506/7 "2024-05-16T20:21:38Z")

</div>


