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

Ping remote computers & Email Alert
Last Post 13 Nov 2007 09:39 PM by bsonposh. 43 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Page 2 of 2 << < 12
Author Messages
aalborzUser is Offline
New Member
New Member
Posts:56
Avatar

--
12 Nov 2007 09:47 PM  
Tried both, but no luck.

Scheduled Tasks shows task's status as running and PowerShell.exe process is running, but I don't get an email.
bsonposhUser is Offline
Basic Member
Basic Member
Posts:392
Avatar

--
12 Nov 2007 09:50 PM  
doh!... its not c:\powershell (I should have seen that)
powershell.exe is C:\Windows\System32\WindowsPowerShell\v1.0
Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
Blog: http://www.bsonposh.com
aalborzUser is Offline
New Member
New Member
Posts:56
Avatar

--
12 Nov 2007 10:11 PM  
Yep that's what I found out in the mean time.

Thanks again!
aalborzUser is Offline
New Member
New Member
Posts:56
Avatar

--
13 Nov 2007 05:17 PM  
I've run into couple of issues:

1- With Scheduled Tasks. When I ran the batch file I get the following error:

"Get-ItemProperty : Cannot find path 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Visu
alStudio\8.0' because it does not exist.
At C:\Program Files\PowerShell Community Extensions\Profile\Environment.VisualS
tudio2005.ps1:33 char:36
+ $VisualStudioKey = get-itemproperty  <<<< $regKeyPath
The term 'C:\#PERSONAL\My' is not recognized as a cmdlet, function, operable pr
ogram, or script file. Verify the term and try again.
At line:1 char:16
+ C:\#PERSONAL\My  <<<< Scripts\Scripts\PowerShell\Ping_Email_Alert.ps1 -list S
ervers-test.txt -verbose -NonInteractive -noprofile"

The script keeps running anyway, but it's a nuisance.
I think it has to do with my installing PowerShell Community Extensions 1.1.1, because I don't remember getting this error before.
Should I remove the extensions or is there a workaround?


2- How do I add another email recipient?  I tried adding another smtp address after the first one, separating them with a ";" or "," with or without space, but it didn't work.
I got the following error with ",":

"Exception calling "Add" with "1" argument(s): "The specified string is not in t
he form required for an e-mail address."
At C:\Ping_Email_Alert.ps1:15 char:24
+     $mailmessage.To.add( <<<< $to)
Exception calling "Send" with "1" argument(s): "A recipient must be specified."
At C:\Ping_Email_Alert.ps1:26 char:21
+     $smtpclient.Send( <<<< $mailmessage)"

Do I need to add a "CC" variable ($cc) to the "Send-Mail" function may be?


Thanks

ALEX
bsonposhUser is Offline
Basic Member
Basic Member
Posts:392
Avatar

--
13 Nov 2007 05:44 PM  
1) add the -noprofile switch to powershell

2) You can do either (cc) or just call the send mail twice
Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
Blog: http://www.bsonposh.com
aalborzUser is Offline
New Member
New Member
Posts:56
Avatar

--
13 Nov 2007 06:18 PM  
1- The "-noprofile" switch is already in the batch file:

C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe C:\Ping_Email_Alert.ps1 -list Servers-test.txt -verbose -NonInteractive -noprofile

2- I would rather use "CC" in one email, to cut down on email traffic. How would I add that functionality into the function? Like this:

Param($msg,$relay,$from,$to,$cc,$subject,[switch]$html)
.......
$mailmessage.To.add($cc)

OR

$mailmessage.CC.add($cc)

and then

Send-Mail -msg "Server $Server failed ping" -relay smtp.mycompany.com -from SrvrMonitor@mycompany.com -to aalborzfard@mycompany.com -cc helpdesk@mycompany.com, -subject "Server $Server is down"

What if there's a need to send email to more than 2 addresses?


bsonposhUser is Offline
Basic Member
Basic Member
Posts:392
Avatar

--
13 Nov 2007 06:30 PM  
1- C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -NonInteractive -noprofile -command C:\Ping_Email_Alert.ps1 -list Servers-test.txt -verbose
The noprofile and nointeractive are powershell switches not for your script

2) You could pass CC as an array like -cc @('myadd@domain.com','myadd1@domain.com','myadd2@domain.com')

Then in the send-mail function do this:


if($cc)
{
   foreach($c in $cc){$mailmessage.CC.add($c)}
}

Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
Blog: http://www.bsonposh.com
aalborzUser is Offline
New Member
New Member
Posts:56
Avatar

--
13 Nov 2007 07:10 PM  
So this is what it would like:

function Send-Mail{
    Param($msg,$relay,$from,$to,$c,$cc,$subject,[switch]$html)
    $SmtpClient = new-object system.net.mail.smtpClient
    $MailMessage = New-Object system.net.mail.mailmessage
    $SmtpClient.Host = $relay
    $mailmessage.from = $from
    $mailmessage.To.add($to)
if($cc) { foreach($c in $cc){$mailmessage.CC.add($c)} }
    $mailmessage.Subject = $subject
    if($html)
    {
        $mailmessage.IsBodyHtml = 1
        $mailmessage.Body = $msg
    }
    else
    {
        $mailmessage.Body = $msg
    }
    $smtpclient.Send($mailmessage)

...............

Send-Mail -msg "Server $Server failed ping" -relay smtp.mycompany.com -from SrvrMonitor@mycompany.com -to aalborzfard@mycompany.com, -cc @('myaddr1@mycompany.com','myaddr2@mycompany.com','myaddr3@mycompany.com') -subject "Server $Server is down"
bsonposhUser is Offline
Basic Member
Basic Member
Posts:392
Avatar

--
13 Nov 2007 07:16 PM  
that should work... you could probably do the same thing to 'To' too (couldnt resist)
Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
Blog: http://www.bsonposh.com
aalborzUser is Offline
New Member
New Member
Posts:56
Avatar

--
13 Nov 2007 08:27 PM  
Yep it did. Thanks again. Brandon, you're THE shell man indeed! :)
hoeserUser is Offline
New Member
New Member
Posts:1
Avatar

--
13 Nov 2007 09:31 PM  

I would like to expand on this concept a bit for a service audit script. I may need to incorporate this ping function first and then execute something like the following on only hosts that reply:

get-content "C:\Scripts\IBM\wasservers.txt" | foreach-object {gwmi win32_service -computername $_ | where {$_.Name -like "*IBM*"} | ft -auto Systemname,DisplayName} > "C:\Scripts\IBM\wasaudit.log"


Here is the basic output I am getting. While this is a good start, I am wondering how I can get an output such as "ping failed" or any other errors like RPC, Access Denied, etc., instead of them just in the powershell prompt?


Systemname DisplayName                                      
---------- -----------                                      
CYCLOPS    IBM HTTP Administration 6.1                      
CYCLOPS    IBM HTTP Server 6.1                              
CYCLOPS    IBM WebSphere Application Server V6.1 - dmgr     
CYCLOPS    IBM WebSphere Application Server V6.1 - Node Agent

Systemname DisplayName                                        
---------- -----------                                        
DRAX       IBM HTTP Administration 6.0                        
DRAX       IBM HTTP Server 6.0                                
DRAX       IBM WebSphere Application Server V6 - DRAX         
DRAX       IBM WebSphere Application Server V6 - DRAX_mgr     
DRAX       IBM WebSphere Application Server V6 - DRAX_nodeagent


2 Common Errors:

+ get-content "C:\Scripts\IBM\wasservers.txt" | foreach-object {gwmi  <<<< win32_service -computername $_ | where {$_.Name -like "*IBM*"} | ft -auto Systemname,Disp
layName} > "C:\Scripts\IBM\wasaudit.log"
Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At line:1 char:67

+ get-content "C:\Scripts\IBM\wasservers.txt" | foreach-object {gwmi  <<<< win32_service -computername $_ | where {$_.Name -like "*IBM*"} | ft -auto Systemname,Disp
layName} > "C:\Scripts\IBM\wasaudit.log"
Get-WmiObject : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))



Thanks in advance!!
-Rob Hoesly

bsonposhUser is Offline
Basic Member
Basic Member
Posts:392
Avatar

--
13 Nov 2007 09:37 PM  
use the $? variable to see if the last command worked or not

if(!$?){write-output "Error: $Error[0]"}
Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
Blog: http://www.bsonposh.com
bsonposhUser is Offline
Basic Member
Basic Member
Posts:392
Avatar

--
13 Nov 2007 09:38 PM  
that should be

if(!$?){write-output "Error: $($Error[0])"}
Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
Blog: http://www.bsonposh.com
bsonposhUser is Offline
Basic Member
Basic Member
Posts:392
Avatar

--
13 Nov 2007 09:39 PM  
darn it... apparently it does not like [ 0 ] it keeps converting it to Y
Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
Blog: http://www.bsonposh.com
You are not authorized to post a reply.
Page 2 of 2 << < 12


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