Solved

Powershell function

Posted on 2013-06-05
2
500 Views
Last Modified: 2013-06-05
Im trying to use Powershell to ping an IP address, and if failed send an email, so Ive written the following code:-

tryPing "Test IP 1" "192.168.2.254"

Function tryPing($serverName, $serverIP){
    $server = $serverName
    $ip = $serverIP

    $ping = new-object System.Net.NetworkInformation.Ping
    $rslt = $ping.send($ip)
        if ($rslt.status.tostring() –eq "Success") {
            write-host ping worked on $server with $ip -ForegroundColor Green}
        else {
            write-host ping failed on $server with $ip -ForegroundColor Red
        
            # Send E-Mail
            $msg = new-object Net.Mail.MailMessage
            $smtp = new-object Net.Mail.SmtpClient("smtpserver")
    
            $msg.From = "pingchecker@mydomain.co.uk"
            $msg.To.Add("myemail1")
            $msg.To.Add("myemail2")
            $msg.Subject = "Warning: ping failed on $server"
            $msg.Body = "The server $server with ip adres $ip does not reply!"
                
            $smtp.Send($msg)
            }
	}

Open in new window


However I cannot seem to get the function call working correctly, as I keep getting an error saying:-
The term 'tryPing' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

I am a kinda beginner with Powershell, and cant figure out whats wrong, any assistance would be appriciated.

Thank you
0
Comment
Question by:tonelm54
2 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 39221777
change the order of the call and function declaration:
Function tryPing($serverName, $serverIP){
    $server = $serverName
    $ip = $serverIP

    $ping = new-object System.Net.NetworkInformation.Ping
    $rslt = $ping.send($ip)
        if ($rslt.status.tostring() –eq "Success") {
            write-host ping worked on $server with $ip -ForegroundColor Green}
        else {
            write-host ping failed on $server with $ip -ForegroundColor Red
        
            # Send E-Mail
            $msg = new-object Net.Mail.MailMessage
            $smtp = new-object Net.Mail.SmtpClient("smtpserver")
    
            $msg.From = "pingchecker@mydomain.co.uk"
            $msg.To.Add("myemail1")
            $msg.To.Add("myemail2")
            $msg.Subject = "Warning: ping failed on $server"
            $msg.Body = "The server $server with ip adres $ip does not reply!"
                
            $smtp.Send($msg)
            }
	}

tryPing "Test IP 1" "192.168.2.254"

Open in new window

0
 
LVL 40

Expert Comment

by:Subsun
ID: 39221784
Copy paste the code to PowerShell console and then press enter, once it return the prompt test your function..

tryPing "Test IP 1" "192.168.2.254"
                                           
Or save the function code to a .ps1 file and load script file with cmdlet definition. You can do this by typing a Dot, a space, and then the path to the file. There has to be a space between the dot and the file name, otherwise it wont work.

PS>. C:\tryPing.ps1
PS>tryPing "Test IP 1" "192.168.2.254"
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question