• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1166
  • Last Modified:

Sending an alert using a Powershell Script?

I've worked with an excellent expert earlier on creating a script that will generate a folder and copy items into that folder.

Now I am hoping to make the script a little more intelligent if possible.

What I want to do is this:
Once the 'attached code snippet' is executed I'd like to get some type of report that indicates that the job completed successfullly or that it failed.  It would be especially nice if this could be sent via SMTP to a recipient such as the network admin.

Additionally if the notice could include perhaps a verbose text file, or if that text could be entered into the main body of the SMTP message this could be reviewed &  perhaps circumvent a "Pass or Fail" message.
$SourceDir = "\\Server\Program Files\Appname\Programs\data\"
Get-ChildItem "$SourceDir*" | `
  Where-Object { $_.PsIsContainer -eq $False } | ForEach-Object {`
  Copy-Item $_.FullName -Destination $Directory.FullName -WhatIf }

Open in new window

0
alexianit
Asked:
alexianit
  • 4
  • 2
1 Solution
 
Chris DentPowerShell DeveloperCommented:

It depends a bit on how you define success and failure. Does it throw errors for you occasionally? If so, what kind of errors are you getting?

Chris
0
 
alexianitAuthor Commented:
No, I'm not seeing any errors thrown.  Thus far it has worked flawlessly.  This is a task that is being completed twice a week and its one way in which we are backing up this data.  So we wish to monitor it, and although I can navigate to the target directory to verify that the data is there, the sheer volume of files being copied prohibits easily seeing anything that might have been missed.

When running this with the -WhatIf paramater I was able to see the result of each item's copy attempt and it would be nice to get an e-mail with the same kind of messages I saw when including -WhatIf.

Then I can then review and adjust for any errors as needed, or simply say 'Yep!  We're good!"
0
 
Chris DentPowerShell DeveloperCommented:

There are a few things we can do then...

We can add the -PassThru parameter to the Copy-Item command, that makes it return information on the copied item.

Like:

$SourceDir = "\\Server\Program Files\Appname\Programs\data\"
Get-ChildItem "$SourceDir*" | `
  Where-Object { $_.PsIsContainer -eq $False } | ForEach-Object {`
  Copy-Item $_.FullName -Destination $Directory.FullName -PassThru }

What would you like it to say about the copy, just details of the file that was copied?

We can also verify and compare the contents of the two directories on completion. Perhaps like this:

$SourceList = Get-ChildItem "$SourceDir*" | ?{ !($_.PsIsContainer) } | Select-Object Name, Length
$DestinationList = Get-ChildItem $Directory.FullName | ?{ !($_.PsIsContainer) } | Select-Object Name, Length

If (Compare-Object $SourceList $DestinationList)
{
  Write-Host "Verify failed"
}
Else
{
  Write-Host "Verify success"
}

I haven't added the bit to send as mail yet. Just want to generate something that's appropriate first :)

Chris
0
A Cyber Security RX to Protect Your Organization

Join us on December 13th for a webinar to learn how medical providers can defend against malware with a cyber security "Rx" that supports a healthy technology adoption plan for every healthcare organization.

 
alexianitAuthor Commented:
Well, I'm uncertain.

On one hand if all is well, a simple 'Verify success' is adequate
But if Verify failed, I'd like to know what or where it failed if possible.

Strictly speaking I don't need 100 messages saying OK, but it could be  useful to get a few that say something like "Could not copy XYZ because blahblahblah!"
0
 
alexianitAuthor Commented:
Perhaps I'm making this more complicated than it needs to be.

While a simple folder comparison could work for my purposes its a bit too simple in reporting the results of pass/fail.

I think I'd much prefer to get a log of what occurred during the copy.  If this could report to me that the copy attempt of ea. item was successful or not, then I could simply go through the log in a text editor looking for failures.
0
 
alexianitAuthor Commented:
I'm still a novice at powershell, unfortunately in my position I haven't the time to further pursue this particular issue.
Chris-Dent has been most helpful in regards to this script and other questions I've posted.  Thank you much!
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now