0%

0/1 Lessons

Course Introduction

• 5min

0 / 2 lessons complete

Getting Started with Windows PowerShell

• 56min

0 / 8 lessons complete

Getting Help and Finding Commands

• 39min

0 / 6 lessons complete

PowerShell Command Syntax

• 33min

0 / 5 lessons complete

PowerShell Objects and Properties

• 35min

0 / 6 lessons complete

The PowerShell Pipeline

• 24min

0 / 2 lessons complete

PowerShell Providers

• 30min

0 / 5 lessons complete

PowerShell Arrays and Variables

• 28min

0 / 4 lessons complete

PowerShell Loops

• 19min

0 / 3 lessons complete

PowerShell Conditional Statements

• 11min

0 / 1 lessons complete

On Premises Lab Setup

• 36min

0 / 8 lessons complete

Basic Domain Administration with Windows PowerShell

• 2hr 27min

0 / 10 lessons complete

Send Emails with PowerShell

• 22min

0 / 2 lessons complete

PowerShell Desired State Configuration (DSC) Basics

• 1hr 48min

0 / 6 lessons complete

PowerShell Modules

• 58min

0 / 7 lessons complete

Powershell Challenges

• 1hr 55min

0 / 23 lessons complete

Course Conclusion

• 1min

0 / 1 lessons complete

Writing the Get-GreetingMessage Function

Instructions

Q&A (0)

Notes (0)

Resources (0)

Saving Progress...

Resources

There are no resources for this lesson.

Notes can be saved and accessed anywhere in the course. They also double as bookmarks so you can quickly review important lesson material.

Create note

You can expand the box below to see the full source code for this lesson:

Get-GreetingMessage.ps1 Full Source Code

In this lesson, we're going to be writing the get dash greeting message function inside of our PowerShell module. So to get started, let's explain what this function does.

It's going to take a user's name, so it's going to take a set of parameters, and I'll just kind of comment this here, so, get parameters, and this is going to be the user's name, and then we're going to get the initial greeting, which we'll use the message, you know, we'll write like a string, like hello, and then whatever the user's name then we'll get a random message from the new greeting function over here, so get a random message, and then we'll output that to the user, so output the messages to the user.

Alright, so to get started, we will start by defining some parameters. So, I'm going to say param; this is how you declare parameters for a function in PowerShell. And we're going to do an opening and closing brace. And this will, we'll spell out the word parameter, and then in parentheses, we're going to say mandatory equals and then dollar sign true.

This means you cannot run the function without specifying this parameter. All right, now we're going to do another opening and closing brace, and we're going to clear the type of parameter, which will be a string, and then the name of the variable. So I'm going to call it "$Name". Now you can put this on separate lines if you want.

Or you can leave it on the same line. Sometimes it's easier to do it like this. And what this does is when we call this function, it will require, because we set mandatory equals true, that we pass the dash name parameter. Alright, now let's go ahead and create the greeting. And the greeting will be equal to hello, comma, dollar sign name for the variable, and an exclamation mark.

Next, we'll get the random string from our New-Greeting PowerShell function, which will be greetingMessage = New-Greeting. Next, we're going to output that to the user. So write-output, and we're going to do in quotation marks greeting, and then we'll do space, and then we'll say greeting message.

Great, now let's press F5 and load this into memory. So I'm going to hit CLS to clear the screen. If I type in Get-GreetingMessage and press enter, it will force me to populate the name parameter because we set the parameter to be mandatory at true. So I can put in my name, Paul. And it says, hello, Paul, PowerShell modules are very cool.

So let's run it again. CLS, I'll press the up arrow until I get to Get-GreetingMessage. And this time I'll do dash, hit control space, and it lets me put in the name. So I can pass the name directly through the command line just like this. And this time, I'll say my name is Joe. It says, hello, Joe; I'm glad you are learning about PowerShell modules.

The first one I think we got was. PowerShell modules are very cool. Then we got this one. And if we do it again, we should get a different mod message, which is the third one.

Okay, so our functions are working like we expect them to be. This is actually all we need to do to get this function working. Great job getting through this one. I will see you in the next lecture.

Server Academy Members Only

Sorry, this lesson is only available to Server Academy Full Access members. Become a Full-Access Member now and you’ll get instant access to all of our courses.

0 0 votes
Lesson Rating
Subscribe
Notify of
profile avatar
0 Comments
Inline Feedbacks
View all comments