?
Solved

Powershell function

Posted on 2013-06-05
2
Medium Priority
?
515 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 2000 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

Four New Appliances. Same Industry-leading Speeds.

But don't take it from us.  The Firebox M370 is Miercom tested and Miercom approved, outperforming its competitors for stateless and stateful traffic throughput scenarios.  Learn more about the M370, M470, M570 and M670 and find the right solution for your organization today!

Question has a verified solution.

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

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

771 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