When complete, your assignment should look like: $tweet = ::ArcaneCode When you have entered the second colon, you should see a list of the enumerated values in VSCode. Here I’ll just assign it to a variable, but we could also pass an enumerated value into a function.īegin typing out the following code sample, and note what happens when you hit the second colon. We’re now ready to use our enum in our script. Now we have our enum, and know what the values are. Then you can run the line of code below.GetEnumNames()Īs you can see, it simply returns a list of the values that we declared in the Enum. First, make sure you have executed the code above by highlighting it and using F8 (VSCode) or F5 (PowerShell IDE) to get the Enum into memory. Hopefully you’ve provided documentation, but it’s also easy for a user to have PowerShell return a list of values. How can they retrieve a list of valid values? You’ve now created an Enum, perhaps you’ve included it within a module you are providing to other programmers in your company. Arcane_Code would be a valid value for an enum. Note that Enums cannot have spaces in their names, although you could use separators such as an underscore. I don’t post a lot, I use it mostly to read through the latest news in the ham radio world. N4IXT is my FCC assigned amateur radio call sign I use to identify myself on the air. One of my hobbies is amateur radio, also known as ham radio. I use it primarily to announce new Pluralsight courses, so it doesn’t see a huge amount of use. The second is the one I use for my company ArcaneTC (short for Arcane Training and Consulting). The first is the main one I use, ArcaneCode. Here I’ve listed three of my Twitter accounts. In the squiggly braces you list the valid values. You simply use the keyword Enum followed by what you wish to name it. Enum MyTwittersĪs you can see, the basic declaration is very simple. As mentioned in the intro, it is a set of predetermined values that will ensure users of your script select a valid value required by your PowerShell code. Basic Enum DeclarationĮnum is short for enumerated value. You can display the contents of any variable by highlighting it and using F8/F5. To run a snippet of code highlight the lines you want to execute, then in VSCode press F8 or in the IDE F5. The examples should work in PowerShell 5.1 in the PowerShell IDE, although they’ve not been tested there. In this article I’ll be using PowerShell Core, 7.2, and VSCode. We’ll take a deeper look in a moment, but first let me mention that for all of the examples we’ll display the code, then under it the result of our code. This allows the user to pick from a finite list, and assure a value being passed into a function or class will be valid. As a first step, we will cover the use of an Enum, as enums are frequently used in combination with classes.Īn Enum is a way to provide a set of predetermined values to the end user. This post begins a series on using Classes in PowerShell.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |