Functioning like an Adult. (In the Shell)

functioning like an adult

I would like to propose we should all function as adults at some point in life. maybe not 24 hours a day but enough to take care of ourselves.

PowerShell is an area that need you to be functioning to preserve your sanity and time, I will cover simple & advanced functions.

A function can save you a ton of typing and it could do quite a bit and be called using the infamous tab completion!

Simple Function Example:

Function Test-Google
{
Try{ $result=Test-Connection google.com
if ($result -ne $null){ write-host "Google is up and reachable" -ForegroundColor Green} 
}
Catch{write-host "Google Error Occured" -ForegroundColor Red}

}

 

Advanced Function Example:

Function Test-NetworkConnectivity
{
[cmdletbinding()]
param(
[parameter(ValueFromPipelineByPropertyName,ValueFromPipeline)][Object[]]$Website =@( "google.com","Bing.com")
)
Begin{}

Process{
FOREACH($SITE IN $Website)
{
Try{ $result=Test-Connection -ComputerName $site
if ($result -ne $null){ write-host "$site is up and reachable" -ForegroundColor Green} 
}
Catch{write-host "$site Error Occured" -ForegroundColor Red}
}
}
}

Take the time to explore tab completion.  this advanced function has way more capability and any questions?

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.