Functions

Using functions can save you so much time. Instead of typing the same code over and over you can make a function. Remember a function should only output 1 thing. Never make a function that outputs multiple things. For example, print to screen, write to file, and send email.
Cmdlets in PowerShell are just functions. There are a few types of functions (regular and advanced). The difference is advance function exposes more advance items such as -verbose.
Another thing to remember about a function is Parameters. What are parameters? A parameter is something you pass into a function to perform some type of operation. A parameter can be anything (integer, string, datetime, custom object).
Earlier I mentioned a function should only perform 1 operation. This is referring to what it outputs. Let’s see some examples.
Get-Service: List Services running on your machine and prints them to the screen
NOTE: This only prints services to the screen (1 operation)
If you needed this information in a CSV, you send it to another cmdlet (function).
Export-CSV: Again Export-CSV only does 1 operation sends data to a CSV file.
Alright let make a simple function for starters!

Our first function will be simple. We will call the function SayHi. It will take in 1 parameter we’ll call it name. The function will output a string of text. Hi <Name>! How are you today?

function  SayHi ($name) {
    write-output "Hi $name! How are you today?"
}

Now this isn’t a professional PowerShell function but it is a function. Just remember that any variables outside the function do NOT exist within the function unless you make them global or at the script level. We will go further into detail on global and script variables later. All you need to know now is if you want a function to know about a variable or some kind of data. You need to pass it as a parameter.

How about we make this into something that makes a little more send rather than just printing hi to the screen. How about we make a function for a character in a game to attack.

What parameters would we need? Hmm. So, If I were to attack something I need to know what my attack is? Also, what am I attacking? What’s their defense? These would all be parameters the function would require. How about we write a basic function.

# Parameters "Attack", "Defense", and EnemyType
function  Attack ($Attack, $Defense, $EnemyType) {
    write-output "You attack an $EnemyType"
    # generic formula to calculate damage
    [int]$damage = $Attack*(100/(100+$Defense))
    [int]$damage = Get-Random -Minimum ($damage - 15) -Maximum $damage
    Write-Output "You deal $damage damage to $EnemyType"
}

Now this is still generic code but hopefully you’re getting the point. Now we don’t have to write code over and over for attacking. We can reuse this function over for attacking. We could even build on to it where you can pass a weapon and also modify the health of the enemy.

TO BE CONTINUED. I wanted to get this post up even though it’s not finished. I’ll continue to work on it.

Leave a Reply

Your email address will not be published. Required fields are marked *

Close Bitnami banner
Bitnami