{"id":7866,"date":"2014-10-02T06:00:00","date_gmt":"2014-10-02T13:00:00","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/?p=7866"},"modified":"2014-10-01T17:35:03","modified_gmt":"2014-10-02T00:35:03","slug":"adding-parameter-sets-to-a-function","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2014\/10\/02\/adding-parameter-sets-to-a-function\/","title":{"rendered":"Adding Parameter Sets to a Function"},"content":{"rendered":"<p>Like many of you, I really live at the command line, because automation is more efficient than single actions. But, as the command line becomes more complex, we need tools to restore our efficiency. The complex syntax of parameter sets in functions is one of those cases. In this post, I learn to use the new Function Editor in PowerShell Studio 2014. I\u2019m not just playing in a GUI. I\u2019ve automated the command line.<\/p>\n<p>&#8212;&#8212;<\/p>\n<p>When I write functions, I often start with the basic command \u2013 the one that makes the function work \u2013 and then build in features. I might even start with fixed values to test the concept and, after testing, convert them to parameters. Next, I add error handling. And, thinking about errors and the user experience often leads me to add parameter sets.<\/p>\n<p>The syntax for parameter sets and a default parameter set, which requires the CmdletBinding attribute, is tricky. I wrote the about_Function help topics that explain the syntax of these elements, but I still never remember them. Typically, I use snippets for the syntax, or search for an old function to use as a template (dir *ps1 | Select-String DefaultParameterSetName | Select FileName \u2013Unique).<\/p>\n<p>But, in PowerShell Studio 2014, I don\u2019t have to remember any of it. The new Function Editor handles all of the syntax so I can concentrate on my function. Here\u2019s how it works.<\/p>\n<p>I\u2019ll start with a very simple function that gets the value of the HelpUri property of a command. The HelpUri value is the online location of a help topic. It\u2019s actually stored in one of two places, but I want to keep this very simple.<\/p>\n<pre><span style=\"color: #0000ff;\">function<\/span> <span style=\"color: #008080;\">Get-HelpUri<\/span><span style=\"color: #000000;\"> (<\/span><span style=\"color: #8b0000;\">$Command<\/span><span style=\"color: #000000;\">)\r\n{\r\n    <\/span><span style=\"color: #8b0000;\">$cmd<\/span> <span style=\"color: #0000ff;\">=<\/span> <span style=\"font-weight: bold; color: #0000ff;\">Get-Command<\/span> <span style=\"color: #8b0000;\">$Command<\/span> <span style=\"color: #3399ff;\">-ErrorAction<\/span><span style=\"color: #000000;\"> SilentlyContinue\r\n    <\/span><span style=\"color: #0000ff;\">if<\/span><span style=\"color: #000000;\"> (<\/span><span style=\"color: #0000ff;\">!<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #8b0000;\">$cmd<\/span><span style=\"color: #000000;\">))\r\n    {\r\n        <\/span><span style=\"color: #0000ff;\">throw<\/span> <span style=\"color: #ff0000;\">\"Cannot find $Command\"<\/span><span style=\"color: #000000;\">\r\n    }\r\n    <\/span><span style=\"color: #8b0000;\">$cmd<\/span><span style=\"color: #000000;\">.HelpUri\r\n}<\/span><\/pre>\n<p>I still have a lot to do to make this function work correctly, but at this point, I realize that users might want the HelpUri values of all commands in a module. So, I need to add a <strong>Module<\/strong> parameter. The <strong>Command<\/strong> and <strong>Module<\/strong> parameters are exclusive, so I need two parameter sets. But, instead of looking up the syntax for parameter sets, I use the automation in PowerShell Studio 2014.<\/p>\n<p>Here\u2019s the magic. I right click my function and then click <strong>Edit Function<\/strong>.<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/09\/EditFunction1.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border-width: 0px;\" title=\"EditFunction1\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/09\/EditFunction1_thumb.png\" alt=\"EditFunction1\" width=\"396\" height=\"220\" border=\"0\" \/><\/a><\/p>\n<p>The Function Builder parses the simple function. I don\u2019t need to enter anything that I\u2019ve already defined.<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/09\/EditFunction2.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border-width: 0px;\" title=\"EditFunction2\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/09\/EditFunction2_thumb.png\" alt=\"EditFunction2\" width=\"482\" height=\"287\" border=\"0\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>I click <strong>Enable Cmdlet Binding<\/strong>, add a Module parameter, set the types for my parameters, and make them mandatory. I don\u2019t need to think about syntax, just about my function.<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/09\/AddParameter.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border-width: 0px;\" title=\"AddParameter\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/09\/AddParameter_thumb.png\" alt=\"AddParameter\" width=\"533\" height=\"306\" border=\"0\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>Next, I add the parameter sets: CmdletSet for the Command parameter and ModuleSet for the Module parameter. I verify that the CmdletSet is selected as the default parameter set. Then, in the Parameters section, I assign each parameter to a parameter set.<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/09\/AddParmSets.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border-width: 0px;\" title=\"AddParmSets\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/09\/AddParmSets_thumb.png\" alt=\"AddParmSets\" width=\"530\" height=\"315\" border=\"0\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>Finally, I add the starter help content and output type. (I can do this later, but I like to get a head start.) Then, I click <strong>OK<\/strong>.<\/p>\n<p>Wowee!<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/09\/Result.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border-width: 0px;\" title=\"Result\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/09\/Result_thumb.png\" alt=\"Result\" width=\"479\" height=\"341\" border=\"0\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>I haven\u2019t spent a minute fooling with syntax. I didn\u2019t have to worry about square brackets, matching parentheses, the exact names of the attributes (is that ParameterSet or ParameterSetName?), or anything other than my function.<\/p>\n<p>Now, I can work on the implementation of my new <strong>Module<\/strong> parameter and other details of my function. Later, when I decide to make the parameters take input from the pipeline, I don\u2019t need to worry about the syntax of the ValueFromPipeline parameter attribute. I just right-click my function and click <strong>Edit Function<\/strong> again.<\/p>\n<p>The Function Editor is a real boost to my productivity. In a way, the Function Editor does for Windows PowerShell what Windows PowerShell did for the Windows UI. The result is uniformity, repeatability, efficiency, and fewer errors. That\u2019s just what I need.<\/p>\n<p><i>June Blender is a technology evangelist at SAPIEN Technologies, Inc. You can reach her at <a href=\"mailto:juneb@sapien.com\">juneb@sapien.com<\/a> or follow her on Twitter at <a href=\"https:\/\/twitter.com\/juneb_get_help\">@juneb_get_help<\/a>.<\/i><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Like many of you, I really live at the command line, because automation is more efficient than single actions. But, as the command line becomes more complex, we need tools to restore our efficiency. The complex syntax of parameter sets in functions is one of those cases. In this post, I learn to use the [&hellip;]<\/p>\n","protected":false},"author":31,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[703,25],"tags":[909,282,934,28,874],"class_list":["post-7866","post","type-post","status-publish","format-standard","hentry","category-powershell-studio","category-windows-powershell","tag-function-builder","tag-functions","tag-juneb","tag-powershell","tag-powershell-studio-2014"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/7866","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/31"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=7866"}],"version-history":[{"count":8,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/7866\/revisions"}],"predecessor-version":[{"id":7876,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/7866\/revisions\/7876"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=7866"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=7866"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=7866"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}