header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

function send email
Last Post 26 Nov 2008 10:51 PM by khiko. 3 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
khikoUser is Offline
New Member
New Member
Posts:12
Avatar

--
21 Nov 2008 04:36 AM  
hi i cant seem to make this work...

i have created a script that gets the status of replication and send emails with different subject. but it does not seem to work. so basically i did this

function Pass()
{
$smtpServer = “server”
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = “test@test.local”
$msg.To.Add(”emailaddress”)
$msg.Subject = “TEST - PASS”
$msg.Body = $results
$msg.IsBodyHtml = 1
$smtp.Send($msg)
}
function Fail()
{
$smtpServer = “server”
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = “test@test.local”
$msg.To.Add(”emailaddress”)
$msg.Subject = “TEST - Fail”
$msg.Body = $results
$msg.IsBodyHtml = 1
$smtp.Send($msg)
}

$results=get-content c:\test.html
if
($results -match "pass") {PASS}
else {fail}


my script always send pass email even if pass is not in the content...hope this make sense i am new with scripting so i kinda admit I am not very good at it.

thanks in advance.

jonobleUser is Offline
New Member
New Member
Posts:6
Avatar

--
21 Nov 2008 11:02 AM  
I think I'd probably suggest using only one function since the only difference between them appears to be the message subject and you could alter that based on the result of your test. I'd probably look to do that check using the Select-String cmdlet. If you use the -quiet switch you'll get a boolean value back for the presence (or lack) of "pass" in the file. So your if statement becomes:

if (select-string "pass" c:\test.html -quiet) {$msg.Subject = "TEST - PASS"}
else {$msg.Subject = "TEST - Fail"}
...all the rest is the same, but you only need it once (except you now don't need $results if you set $msg.Body = get-content c:\test.html)

Having said all that, I'm not sure why you're always hitting the pass condition if "pass" isn't in test.html. Could you post the content of that file?
PoshoholicUser is Offline
PowerShell MVP
New Member
New Member
Posts:38
Avatar

--
21 Nov 2008 05:52 PM  
You should be aware that '-match "pass"' is usng a regular expression substring search in the entire contents of the html file. That means it will find matches if the file contains password, impass, passage, underpass, passthrough, etc. It is likely that your html file contains something with 'pass' as a substring.

Whenever you are dealing with a condition being met and wondering why it is being met, your best troubleshooting step is to see the results of that condition directly in PowerShell. In your specific example, you should run this:

$results=get-content c:\test.html
$results -match "pass"

That will return to you every line in c:\test.html that contains pass as a substring.

To avoid the matches you don't want, you will likely want to tweak your search string so that it only matches exactly what you are looking for. Regular expressions can be tricky, and I have no idea what is matching the 'pass' regular expression in your file, but it might be as simple as changing "pass" to "\bpass\b" so that it only finds the string pass when it is not adjacent to other alphanumeric characters (i.e. when it is on a word boundary).

To visualize this a little better, compare this:

'password' -match 'pass'

to this:

'password' -match '\bpass\b'

The second one fails to find a match because you have specified that there can be no alphanumeric characters adjacent to the string 'pass'. It might be worth replacing your "pass" string with "\bpass\b" and seeing if that gives you the results you are looking for.

--
Kirk Munro [MVP]
Poshoholic
http://poshoholic.com
khikoUser is Offline
New Member
New Member
Posts:12
Avatar

--
26 Nov 2008 10:51 PM  
Thanks jonoble and posholic and for explaining select-string with more detail. my script would not work because there are 2 instances of the word "pass". i have change the condition of select-string and now it all works :). thanks!
You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer