Writing the New-Greeting Function
Full-Access Members Only
Sorry, this lesson is only available to Server Academy Full-Access members. Become a Full-Access member now and get instant access to this and many more premium courses. Click the button below and get instant access now.
Instructions
Q&A (0)
Notes (0)
Resources (1)
Saving Progress...
Notes can be saved and accessed anywhere in the course. They also double as bookmarks so you can quickly review important lesson material.
Click the dropdown below to see the full source code of the New-Greeting.ps1 file.
New-Greeting.ps1 Source Code
All right. In this lesson, we're going to be writing the functions that we'll be using for our custom module. Now, if you remember the last lecture, we created these files new greeting. ps1 and get greetingmessage. ps1. Now, you'll notice there's some placeholder text in here. You can download this. Under the resources of this lesson, it's a file called greetme-starter.Zip. This kind of includes just a description, and all of these files have some type of starter text. So does this greetme. psd1, and most usefully, they have this link to the documentation as far as, like, creating a PowerShell module manifest.
So if you want to get that, you can if you don't, that's fine. You can just look at the screen and create the functions as you see them here. But the purpose of this function is going to return a random greeting message. That's what we're going to do with this function. And the function name is going to be new dash greeting.
Okay. So I'm going to go ahead and get started and again, we want to keep this example as simple as possible. So I don't want to overwhelm you with super complicated scripts or functions. All we're going to do is create three lines of text that one of them is going to be PowerShell modules are cool, and then another one's like, I'm glad you're learning PowerShell. And then PS modules are the best. There's going to be three separate lines of code or three separate strings of greetings. And then this function is just going to return one of them at random.
Alright, so let's get started by declaring a variable called greetings, and that's going to be equal to an array, so we're going to do the at sign, open and close parenthesis, and again with Visual Studio Code, I only need to hit open parenthesis, and it'll auto close the parenthesis.
So I'll press enter, and we're going to do three strings. So I'm going to do quotation marks, so I'll hit quote two times, I'm going to hit comma, and then some Visual Studio Code magic that I like to do, I'll hit shift, alt, and the down arrow two times. And that will give me three arrays. And I'll get rid of that last comma.
Because we don't need a comma for the last entry of an array. Alright, now let's go ahead and populate this. And I'm just going to say PowerShell modules are very cool. And you're free to change these however you want. There just needs to be a certain number of strings. You can add more if you want.
But this is just kind of what I'm going to put in. So, I'm, I'm glad you are learning. Oops, spell that right, learning about PowerShell modules. And then the last one will be PS modules are the best. Okay now let's go ahead and just make comments because we want to be a good coder and if someone comes up behind us, we want them to be able to understand what our code is doing.
So select a, or declare a message greeting.
And that should be declare message greetings with an S. Okay, and now let's say get a random message, and we're going to make a variable called greeting and greeting is going to be equal to greetings, the variable we declared above, and I'm going to pipe that to get random. And if I press F8, I'm going to get an error because I haven't declared this up here.
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.