Solved

Powershell function

Posted on 2013-06-05
2
496 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
Synchronize a new Active Directory domain with an existing Office 365 tenant
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

910 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

24 Experts available now in Live!

Get 1:1 Help Now