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 …
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.
$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.
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.