Solved

Powershell function

Posted on 2013-06-05
2
495 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
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Why would I want to create a function for tracking messages? I am glad you asked. As with most monotonous/routine tasks, human error tends to creep in after doing the same task over and over again. By creating a function, you load the function once…
This article shows how a content item can be identified directly or through translation of a navigation type. It then shows how this information can be used to create a menu for further navigation.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now