[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Powershell function

Posted on 2013-06-05
2
Medium Priority
?
547 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:
Meir Rivkin 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

Are You Ready for GDPR?

With the GDPR deadline set for May 25, 2018, many organizations are ill-prepared due to uncertainty about the criteria for compliance. According to a recent WatchGuard survey, a staggering 37% of respondents don't even know if their organization needs to comply with GDPR. Do you?

Question has a verified solution.

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

My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
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…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

649 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