...Because someone's gotta tell the story...


To return to the main Blog List, click Full Blog Listing.


Site Updates
Visual Basic

How to make a simple Powershell Function

Thursday, March 28, 2013 in Powershell (Views: 3800)
There are a lot of blogs on how to do this, but here's a very simple function and how to call it. First, a function in PowerShell (like VB or VBScript) returns a value to the script or performs a type of Subroutine for those in the VB world.

Simply put, here is a function that does nothing more than concatenate a variable, which at the end will be dumped into the event log.

Function Add2Log {param ([string]$Text2Add)
write-host "Adding Text: "$text2add
$MyVariable=$MyVariable + $Text2Add + "`n"

Note the arguments are passed inside the parenthesis, and the function itself is in "curly brackets". Here is how to call the function:

Add2Log "Adding Line 1 to my log..."
Add2Log "Adding Line 2 to my log..."

The `n at the end serves as a carriage return for those who are trying to keep the lines nice and neat.

This is a very simple function to make your PowerShell life easier.


Related Blogs You May Be Interested In:

To leave a comment, please log in and/or register.